在第二个python脚本暂停时,从另一个python脚本运行python脚本

在第二个python脚本暂停时,从另一个python脚本运行python脚本,python,Python,我有一个pythons脚本,需要运行另一个脚本 来自python\u脚本的流\u 1.py: run python_script_2.py pause till python_script_2.py is done continue python_script_1.py flow 谢谢通常您想做的事情与此类似: python_脚本_2.py python_脚本_1.py 输出 您要做的是在主模块内执行第二个Python脚本的一些代码。如果您的代码中没有任何多线程管理(因此“顺序执行”),您可以

我有一个pythons脚本,需要运行另一个脚本

来自python\u脚本的流\u 1.py:

run python_script_2.py
pause till python_script_2.py is done
continue python_script_1.py flow

谢谢

通常您想做的事情与此类似:

python_脚本_2.py python_脚本_1.py 输出


您要做的是在主模块内执行第二个Python脚本的一些代码。如果您的代码中没有任何多线程管理(因此“顺序执行”),您可以执行以下操作:

脚本1.py 脚本2.py 你可以试试

例如:

您有
script_1.py
script_2.py
并且希望从第一个运行最后一个,因此您可以:

# script_1.py

import subprocess

p = subprocess.Popen(
    ['python', 'script_2.py'],  # The command line.
    stderr = subprocess.PIPE,   # The error output pipe.
    stdout = subprocess.PIPE,   # The standar output pipe.
)

output, error = p.communicate() # This will block the execution(interpretation) of script_1 till 
                                # script_2 ends.

当然,如果由于某种原因,您无法从
script_2.py
导入代码,正如其他答案所示,那么这就是解决方案。

可能重复no-我需要在这里暂停调用脚本
import python_script_2

if __name__ == '__main__':
    print 'before'
    python_script_2.func()
    print 'after'
before
running python_script_2.func()
after
from script2 import my_process

if __name__ == '__main__':
    print("Be prepared to call stuff from script2")
    my_process()
    print("Ok, now script2 has finished, we are back in script1")
def my_process():
    # do Stuff
# script_1.py

import subprocess

p = subprocess.Popen(
    ['python', 'script_2.py'],  # The command line.
    stderr = subprocess.PIPE,   # The error output pipe.
    stdout = subprocess.PIPE,   # The standar output pipe.
)

output, error = p.communicate() # This will block the execution(interpretation) of script_1 till 
                                # script_2 ends.