Я написал два скрипта Python script1.py и script2.py. Я хочу запустить script1.py из script2.py и получить содержимое переменных script1, созданных во время выполнения script1. Script1 имеет несколько функций, в которых создаются переменные, в том числе и в main.
Спасибо за все ваши ответы. Я изучил ваши ответы, и, похоже, это не работает. Вот виновные сценарии, о которых я говорю:
script1.py
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
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()
Извините за мои ошибки и спасибо за ваши уместные замечания. У меня есть следующее сообщение об ошибке:
" 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()
script2.py
, оно, конечно, не сработает — вы не включилиimport script1
! Как я и несколько других сказали ниже. - person Katriel   schedule 17.08.2010