简体   繁体   English

当条件为真时运行另一个 .py 文件

[英]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.pyrun2.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.

相关问题 条件为真时如何自动运行脚本? - How to auto-run a script when a condition is true? 将文件重定向到另一个位置 incase IF 条件在 PYTHON 中为 TRUE - Redirect File to another location incase IF condition is TRUE in PYTHON 如果条件为真,如何运行 class? - How to run a class if a condition is true? 无法从另一个python .py文件运行进程 - Can't run process from another python .py file 如何同时运行 2 个文件(.py)并将变量更新到另一个文件? - How to run 2 files(.py) concurrently and update the variable to another file? 在 another.py 中导入模块时,返回 function 和直接运行不一样? - When importing a module in another .py , return of function is different to directly run it? 调用另一个文件时,subprocess.popen和subprocess.run。py在linux上导致导入错误。 的Python 3.6.4 - subprocess.popen and subprocess.run when calling another file.py gives import error on linux. Python 3.6.4 如何调用另一个 python 文件,运行它,并将 main.py 替换为 new.py 文件? - How to call another python file, run it, and replace main.py with the new .py file? 当我从批处理文件运行 .py 文件时打开 PyCharm - PyCharm opening when I run .py file from batch file 通过批处理文件运行时未执行py文件 - py file not executed when run through batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM