script1.py と script2.py という 2 つの Python スクリプトを作成しました。script2.py から script1.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: 'module' オブジェクトに属性 '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()