我写过两个python脚本script1.py和script2.py。我想从script2.py运行script1.py,并获取执行script1期间创建的script1的变量的内容。 Script1有几个功能,其中包括主要的变量。执行程序后导入python变量
谢谢你的答案。我已经检查了你的答案,但似乎并不奏效。 这里是我说的是有罪的脚本:
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()
我清理了你的代码;在这里发布时,您应该使用良好的格式。什么是错误?你写的东西应该可以工作。 – katrielalex 2010-08-17 13:02:50
如果这是'script2.py'的*全部*内容,它当然不起作用 - 你还没有包含'import script1'!正如我和其他几位人士所说的。 – katrielalex 2010-08-17 13:03:31
从你的文章中我得到的印象是你想运行script1,然后在将来有一段时间能够运行脚本二,仍然可以获取在script1中设置的值。那是对的吗? – 2010-08-17 15:20:42