2016-11-09 135 views
2

我有一個python 「client.py」 腳本如下:如何從不同的python腳本調用python腳本?

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的調用。請幫助我失蹤。

+3

爲什麼要在子進程中調用另一個腳本,而不是簡單地'導入'並重新使用它的功能? – jonrsharpe

+0

@jonrsharpe其實我不知道該怎麼做,請你告訴 – Learner

回答

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() 
+0

http://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files可能會有所幫助,另外... –

1

如果你有過client_new.py模塊控制,我強烈建議A. N.其他的答案。如果不這樣做,那麼改變你的main()功能client.py到:

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 

邊注:不subrocess使用shell=True如果能夠避免它。使用subprocess.Popen(['client_new.py'], ...)