我寫過兩個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