[英]run another .py file when condition is true
I'm very new to Python and trying to make a small project in Raspberry Pi2 with Python我对 Python 非常陌生,并试图用 Python 在 Raspberry Pi2 中制作一个小项目
Currently I have 2 code files run1.py
and run2.py
目前我有 2 个代码文件run1.py
和run2.py
I would like to write an if-else
condition in Project.py
but I'm not sure how to write the code properly....我想在Project.py
编写一个if-else
条件,但我不确定如何正确编写代码......
if (condition is true) ----> run the code from file "run1.py"
else ----> run the code from file "run2.py"
Is it about the topic of ' __main__
' or import os
?是关于' __main__
' 或import os
的主题吗? i'm trying to understand how it works too but not really understand yet.我也试图了解它是如何工作的,但还没有真正了解。
Thank you谢谢
If you just want to import one of the files, for example because both of them have a function called foo
and you want to pick one of them at runtime, you can do this:如果您只想导入其中一个文件,例如因为它们都有一个名为foo
的函数并且您想在运行时选择其中一个,您可以这样做:
if condition:
import fileA as file
else:
import fileB as file
file.foo()
If you actually need to start the files (they are independent programs), you can do this:如果您确实需要启动文件(它们是独立程序),您可以这样做:
import subprocess
if condition:
subprocess.call(['python', 'fileA.py'])
else:
subprocess.call(['python', 'fileB.py'])
I would recommend that way:我会推荐这种方式:
main.py主文件
import first, second
if __name__ == "__main__":
if foo:
first.main()
else:
second.main()
first.py:第一个.py:
def main():
do_something()
if __name__ == "__main__":
main()
second.py (just like first.py) second.py(就像first.py)
You can then call first.py / second.py from the command line and they run their code.然后您可以从命令行调用 first.py / second.py 并运行它们的代码。 If you import them ( import first, second
) they do nothing, but you can call their (main)methods, ie in your if-else-condition.如果您导入它们( import first, second
),它们什么都不做,但您可以调用它们的(主要)方法,即在您的 if-else-condition 中。
The __name__ == "__main__"
part prevents the code in the condition to run when its imported from another file, while it runs when the file is executed directly from the commandline. __name__ == "__main__"
部分防止条件中的代码在从另一个文件导入时运行,而在直接从命令行执行文件时运行。
I think runpy is better, imagine you have 25 classes, and they are significantly different from each other.我认为 runpy 更好,假设您有 25 个类,并且它们彼此显着不同。 It is not nice, to make 25 import statements around 25 if conditions on your code.在代码上的 25 个 if 条件附近创建 25 个 import 语句并不好。
Folder:.
preA.py
preB.py
preC.py
preD.py
test.py
You could use a function that saves you time, and preserves your clean code您可以使用一个可以节省时间并保留干净代码的函数
import runpy
def call_f(filename):
return runpy.run_path(filename , run_name='__main__')
#Call the function, since we are using run_path we have to specify the extension
#You can also use modules and you should use runpy.run_module
res = call_f('preA.py')
print(res['res'])
#>> 0
And the preprocessing file would look like this:预处理文件如下所示:
def preproc():
print('I am A preprocessing, different from any other type of preprocessing')
return 0
if __name__ == '__main__':
res = preproc()
if <condition>:
run1.py
else:
run2.py
If condition is true then run1.py will run.如果条件为真,则 run1.py 将运行。 Otherwise run2.py will run.否则 run2.py 将运行。
I hope I answer your question.我希望我能回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.