从另一个python脚本运行一个python脚本?
我已经尝试了所有的事情从另一个python脚本运行一个python脚本?,python,python-2.7,Python,Python 2.7,我已经尝试了所有的事情 if __name__ == "__main__": 到 我已经阅读了这里的所有其他类似问题,并阅读了官方Python文档 我搞不懂这个 import os ask1 = raw_input("Create bid? ") create = "createbid.py %s" % () def questions(): if ask1 == "yes": os.system(create) if ask1 == "no":
if __name__ == "__main__":
到
我已经阅读了这里的所有其他类似问题,并阅读了官方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__":
但是如果我还想调用另一个脚本呢
我想根据问题的答案调用不同的脚本。使用
os.system(“python createbid.py”)
的关键是以字符串格式传入shell命令
如果要与该脚本通信,可能需要子流程
。
请参阅此问题的答案:使用
os.system(“python createbid.py”)
的关键是以字符串格式传入shell命令
如果要与该脚本通信,可能需要子流程
。
请参阅此问题的答案:我不确定您到底想做什么,但一般来说,您应该能够做类似的事情
import foo
import bar
ask = raw_input("Do something?")
if ask.lower() in ('yes', 'y'):
foo.do_something()
else:
bar.do_other()
我不确定你到底想做什么,但总的来说,你应该能够做这样的事情
import foo
import bar
ask = raw_input("Do something?")
if ask.lower() in ('yes', 'y'):
foo.do_something()
else:
bar.do_other()
这里可能回答了这个问题: 因此,您需要在createbid.py(和其他脚本)中定义一些方法: 然后在你的主要剧本中
import createbid
def questions():
if ask1 == "yes":
createbid.run()
if ask1 == "no":
quit()
if __name__ == '__main__':
questions()
这里可能回答了这个问题: 因此,您需要在createbid.py(和其他脚本)中定义一些方法: 然后在你的主要剧本中
import createbid
def questions():
if ask1 == "yes":
createbid.run()
if ask1 == "no":
quit()
if __name__ == '__main__':
questions()
或者,您可以使用
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)
不过,这种方法更像是一个有趣的把戏,根据具体情况,应该有几种方法做得更好。或者,您可以使用
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)
不过,这种方法更像是一个有趣的把戏,根据具体情况,应该有几种方法做得更好。谢谢您的帮助!我把几个答案结合起来,使它起作用 这项工作:
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()
谢谢你的帮助!我把几个答案结合起来,使它起作用 这项工作:
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()
现在,启动其他进程的推荐方法是使用该模块 这相对容易做到。这里有一个简单的方法可以将其应用于您的问题:
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
(或其他脚本)时,\uuuu name\uuuu='\uuuuu main\uuuu'
将是真的
,与导入时不同。现在,启动其他进程的推荐方法是使用该模块
这相对容易做到。这里有一个简单的方法可以将其应用于您的问题:
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
(或其他脚本)时,\uuuu name\uuuu='\uuuu main\uuuuu'
将是True
,这与导入时的情况不同。您尝试过create=“python createbid.py%s%”而不是create=“createbid.py%s%”吗?我尝试过。两者都有相同的结果。您是否尝试了create=“python createbid.py%s”%()而不是create=“createbid.py%s”%()我试过了。这两个脚本都有相同的结果。当我使用另一个脚本时,这是有效的。如何从多个其他脚本中进行选择?您可以根据需要导入任意多个模块,这正是@batman在我使用其他脚本时的工作方式。如何从多个其他脚本中进行选择?您可以根据需要导入任意多个模块,这正是@batman所建议的