【问题标题】:Run one python script from another python script? [duplicate]从另一个 python 脚本运行一个 python 脚本? [复制]
【发布时间】:2026-02-05 15:50:02
【问题描述】:

我已经尝试了所有的东西

if __name__ == "__main__":

os.system()

我已经查看了此处的所有其他类似问题并阅读了 Python 官方文档。

我看不到这个

import os

ask1 = raw_input("Create bid? ") 
create = "createbid.py %s" % ()
def questions():
    if ask1 == "yes":
        os.system(create)
    if ask1 == "no":
        quit()

question()

可靠地运行 ceatebid.py 文件。我可以使用它

if __name__ == "__main__":

但是如果我还想调用另一个脚本呢?

我想根据问题的回答方式调用不同的脚本。

【问题讨论】:

标签: python python-2.7


【解决方案1】:

我不确定你到底想做什么,但总的来说你应该能够做这样的事情。

import foo
import bar

ask = raw_input("Do something?")
if ask.lower() in ('yes', 'y'):
    foo.do_something()
else:
    bar.do_other()

【讨论】:

    【解决方案2】:

    使用os.system("python createbid.py")的关键是传入一个字符串格式的shell命令。

    如果您想与该脚本通信,您可能需要subprocess。 查看这个问题的答案:running bash commands in python

    【讨论】:

      【解决方案3】:

      这可能在这里得到了回答:What is the best way to call a Python script from another Python script?

      因此,您需要在 createbid.py(和其他脚本)中定义一些方法:

      def run()
          print 'running'
      

      然后在你的主脚本中,

      import createbid
      
      def questions():
          if ask1 == "yes":
              createbid.run()
          if ask1 == "no":
              quit()
      
      if __name__ == '__main__':
          questions()
      

      【讨论】:

      • 这在我使用另一个脚本时有效。如何从多个其他脚本中进行选择?
      • 您可以根据需要导入任意数量的模块,这正是@batman 建议的方式
      【解决方案4】:

      现在,启动其他进程的推荐方法是使用subprocess 模块。

      这相对容易做到。这是将其应用于您的问题的简单方法:

      import subprocess
      import sys
      
      create = [sys.executable, 'createbid.py']
      
      def question(ans):
          if ans == 'yes':
              subprocess.call(create)
          elif ans == 'no':
              quit()
      
      ask1 = raw_input('Create bid? ')
      question(ask1)
      print('done')
      

      注意:当createbid.py(或其他脚本)以这种方式执行时,
      __name__ == '__main__' True,不像它会如果是imported。

      【讨论】:

        【解决方案5】:

        或者,您可以使用exec(Python2 中的语句,Python3 中的函数)。

        假设您的脚本scriptA 存储在一个名为scriptA.py 的文件中。那么:

        scriptContent = open("scriptA.py", 'r').read()
        exec(scriptContent)
        

        这样做的好处是exec 允许您之前定义变量,并在脚本内部使用它们。

        因此,如果您要在运行脚本之前定义一些参数,您仍然可以在解决方案中调用它们:

        #Main script
        param1 = 12
        param2 = 23
        scriptContent = open("scriptA.py", 'r').read()
        exec(scriptContent)
        
        #scriptA.py
        print(param1 + param2)
        

        不过,这种方法更像是一种有趣的技巧,并且根据情况,应该有几种方法可以做得更好。

        【讨论】:

          【解决方案6】:

          感谢您的帮助!我结合了几个答案来让它发挥作用。

          这行得通:

          import seebid
          import createbid
          
          ask1 = raw_input("Create bid? ")
          ask2 = raw_input("View bid? ")
          create = createbid
          see = seebid
          
          def questions():
          
              if ask1.lower() in ('yes', 'y'):
                  create.createbd()
              elif ask1.lower() in ('no', 'n'):
                  ask2
          
              if ask2.lower() in ('yes', 'y'):
                  see.seebd()
          
          if __name__ == "__main__":
              questions()
          

          【讨论】: