別のスクリプトからスクリプトを呼び出すにはどうすればいいですか? 質問する

別のスクリプトからスクリプトを呼び出すにはどうすればいいですか? 質問する

test1.pyモジュール内にない という名前のスクリプトがあります。スクリプト自体の実行時に実行されるコードだけがあります。関数、クラス、メソッドなどはありません。サービスとして実行される別のスクリプトがあります。test1.pyサービスとして実行されているスクリプトから を呼び出したいです。

例えば:

ファイルtest1.py

print "I am a test"
print "see! I do nothing productive."

ファイルservice.py

# Lots of stuff here
test1.py # do whatever is in test1.py

ベストアンサー1

これを行う通常の方法は次のようになります。

テスト1.py

def some_func():
    print 'in test 1, unproductive'

if __name__ == '__main__':
    # test1.py executed as script
    # do something
    some_func()

サービス.py

import test1

def service_func():
    print 'service func'

if __name__ == '__main__':
    # service.py executed as script
    # do something
    service_func()
    test1.some_func()

おすすめ記事