【问题标题】:How to call a python script from different python script?如何从不同的 python 脚本调用 python 脚本?
【发布时间】:2016-11-09 12:24:38
【问题描述】:

我有一个python“client.py”脚本如下:

    import sys
    import traceback
    import client_new
    import subprocess
    def main():
        print "inside client"
        subprocess.Popen('C:/client_new.py',shell=True)
        #execfile('client_new.py')
    if __name__ == "__main__":
        is_sys_exit = False
        ret_val = 0
        try:
            ret_val = main()
        except SystemExit:
            is_sys_exit = True
        except:
            if not is_sys_exit:
                print( traceback.format_exc() )
                if sys.version_info < ( 3,0 ):
                    raw_input( "Press return to continue" )
                else:
                    input( "Press return to continue" )
                sys.exit( 1 )
        if is_sys_exit:
            print( "SystemExit Exception was caught." )
        sys.exit( ret_val )

client_new.py 脚本如下:

    import traceback
    def main():
        print "inside client new"

    if __name__ == "__main__":
        is_sys_exit = False
        ret_val = 0
        try:
            ret_val = main()
        except SystemExit:
            is_sys_exit = True
        except:
            if not is_sys_exit:
                print( traceback.format_exc() )
                if sys.version_info < ( 3,0 ):
                    raw_input( "Press return to continue" )
                else:
                    input( "Press return to continue" )
                sys.exit( 1 )
        if is_sys_exit:
            print( "SystemExit Exception was caught." )
        sys.exit( ret_val )

所以,从 client.py 有另一个脚本 client_new.py 被使用子进程调用,但是当 client.py 被执行时,它只打印它的数据而不显示 client_new 的打印。因此,我不明白我在调用 client_new.py 时做错了什么。请帮助我缺少什么。

【问题讨论】:

  • 为什么要在子进程中调用另一个脚本,而不是简单地importing 并重用其功能?
  • @jonrsharpe 实际上我不知道该怎么做你能告诉我怎么做

标签: python python-2.7 subprocess


【解决方案1】:

如果它们都在同一个目录中(或者它们在 python 查找的目录中(我相信称为 PYTHONPATH)),则相对简单。要导入 python 文件,只需删除 .py 并导入。因此,您只需要以下代码。

import client_new
#Write rest of the code here
client_new.run_function()

您还需要在 client_new 中稍微更改您的代码,以便它可以工作。

import traceback
def main():
    print "inside client new"

def run_function():
    is_sys_exit = False
    ret_val = 0
    try:
        ret_val = main()
    except SystemExit:
        is_sys_exit = True
    except:
        if not is_sys_exit:
            print( traceback.format_exc() )
            if sys.version_info < ( 3,0 ):
                raw_input( "Press return to continue" )
            else:
                input( "Press return to continue" )
            sys.exit( 1 )
    if is_sys_exit:
        print( "SystemExit Exception was caught." )
    sys.exit( ret_val )

if __name__ == "__main__": #This is only run if called as an external script.
    run_function()

【讨论】:

【解决方案2】:

如果您可以控制 client_new.py 模块,我强烈建议您使用 A. N. Other's answer。如果不这样做,请将client.py 中的main() 函数更改为:

def main():
    print 'inside client' 
    proc = subprocess.Popen('C:/client_new.py', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout = proc.stdout.read()
    stderr = proc.stdout.read()
    print 'Got output from client_new:\n' + stdout
    if stderr:
        print 'Got error output from client_new:\n' + stderr

旁注:如果可以避免,请不要在子进程中使用shell=True。使用subprocess.Popen(['client_new.py'], ...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2018-03-25
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多