나는 두 개의 python 스크립트 script1.py와 script2.py를 작성했습니다. script2.py에서 script1.py를 실행하고 script1을 실행하는 동안 생성 된 script1의 변수 내용을 가져 오려고합니다. Script1에는 주 변수를 포함하여 변수가 생성되는 몇 가지 함수가 있습니다.실행 후 프로그램의 파이썬 변수 가져 오기
답장을 보내 주셔서 감사합니다. 나는 당신의 대답을 조사했으나 효과가없는 것 같습니다. 다음은 제가 이야기 유죄 스크립트입니다 :
script1.py 내 실수
from Tkinter import *
class Application:
def __init__(self):
""" main window constructor """
self.root = Tk()
# I'd like to import here the variables of script1.py
self.root.title(script1.vroot_directory) ?
self.root.mainloop()
# Main program
f = Application()
죄송합니다
def main(argv):
"""Main of script 1
Due to the internal structure of the script this
main function must always be called with the flag -d
and a corresponding argument.
"""
global now
now = datetime.datetime.now()
global vroot_directory
vroot_directory = commands.getoutput("pwd")
global testcase_list_file
testcase_list_file = 'no_argument'
try:
opts, args = getopt.getopt(argv, "d:t:",
["directory_path=", "testcase_list="])
except getopt.GetoptError, err:
print command_syntax
sys.exit()
for opt, arg in opts:
if opt in ("-d", "--directory"):
vroot_directory = arg
if opt in ("-t", "--testcase"):
testcase_list_file = arg
def function1():
pass
def function2():
if testcase_list_file == 'no_argument':
function1()
else:
function2()
if __name__ == "__main__":
main(sys.argv[1:])
script2.py하고 관련 발언에 감사드립니다 . 나는 다음과 같은 오류 메시지가 있어요 :
"AttributeError : '모듈'개체 'vroot_directory'에는 속성이 없습니다"
나는 다음과 같은 것을 가지고 싶습니다 더 구체적으로 :
을from Tkinter import *
import script1
class Application:
def __init__(self):
""" main window constructor """
self.root = Tk()
script1.main(-d directory -t testcase_list_file) # to launch script1
self.root.title(script1.vroot_directory) # and after use its variables and functions
self.root.mainloop()
# Main program
f = Application()
코드를 정리했습니다. 여기에 게시 할 때 좋은 형식을 사용해야합니다. 오류가 무엇입니까? 당신이 작성한 것은 효과가 있습니다. – katrielalex
이것이'script2.py'의 * 전체 내용이라면, 그것은 효과가 없을 것입니다 - 당신은'import script1'을 포함하지 않았습니다! 나와 다른 몇몇 사람들이 아래에서 말했듯이. – katrielalex
귀하의 게시물에서 나는 당신이 script1을 실행하고자하는 인상을 받았고, 얼마 후에는 script2를 실행하고 여전히 script1에 설정된 값을 얻을 수 있어야합니다. 그 맞습니까? –