如何在另一个文件中运行一个python文件? [英] How to run one python file in another file?
本文介绍了如何在另一个文件中运行一个python文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import importlib
importlib.import_module('file.py')
error: ModuleNotFoundError: No module named 'file.py'; 'file' is not a package
这是在另一个文件中运行一个文件的好方法吗?如果没有,您能告诉我更好的方法吗?
Is this a good way of running one file in another? If not, could you please tell me a better way?
提前谢谢!
推荐答案
If you want to import it as a module you should listen to the comments and do what they say (visit How to import other Python files?). But if for some reason which I don't understand you wanted to run it as an independent file and not a module you could do the following:
import os
os.system('python3 yourPythonFile.py')
但是,我认为这不是一个好习惯,因为它会阻塞主脚本,直到"yourPythonFile.py"停止运行.
However, I think that this isn't a good practice as it blocks the main script until "yourPythonFile.py" stops running.
这篇关于如何在另一个文件中运行一个python文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文