简体   繁体   English

Python 2.7在程序中运行外部.py文件

[英]Python 2.7 Running External .py Files in Program

I have a .py file and I want to make it so I can type it's name in another .py file and have it run all the code from the first file. 我有一个.py文件,我想制作它,以便可以在另一个.py文件中键入它的名称,并让它运行第一个文件中的所有代码。

Remember, this is in Python 2.7 on a Raspberry Pi 3. 请记住,这是在Raspberry Pi 3上的Python 2.7中。

Thank you! 谢谢!

Well you can use execfile() or os.system() to solve your problem. 好了,您可以使用execfile()或os.system()解决您的问题。 But I think, the correct way to tackle your problem is to import the file in your current script and call the imported file's functions or main function directly from your script. 但是我认为,解决问题的正确方法是在当前脚本中导入文件,然后直接从脚本中调用导入文件的函数或主函数。

调用os.system("second.py")或使用subprocess.Popen从你第一个脚本应该为你工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM