简体   繁体   English

Python在子流程调用之前继续运行导入的脚本

[英]Python Keep running my imported scripts before subprocess call

Okay, newserial.py is the main script I execute, trackid.py is second script i want to execute through subprocess.call("trackid.py",shell=True) . 好的, newserial.py是我执行的主要脚本, trackid.py是我要通过subprocess.call("trackid.py",shell=True)执行的第二个脚本。 I placed __init__.py in and all files in the same folder. 我将__init__.py放入其中,所有文件都放在同一文件夹中。

Problem: However, when I use: import trackid , it automatically executes the script before it reaches the line: subprocess.call("trackid.py",shell=True) . 问题:但是,当我使用: import trackid ,它会在到达以下行之前自动执行脚本: subprocess.call("trackid.py",shell=True) I read tutorial saying you should place in my code: 我阅读了教程,说您应该放在我的代码中:

def main():
  if__name__=="__main__" 
  ...

Yet, it doesn't help! 但是,这没有帮助! I put def main(): in both scripts newserial.py and trackid.py 我在两个脚本中都放了def main(): newserial.pytrackid.py

Questions: 问题:
How do I stop this from happening? 如何阻止这种情况的发生?
How do I execute trackid.py as a subprocess and automatically end the script and continue with the remaining line of the main script newserial.py ? 如何将trackid.py作为子trackid.py执行,并自动结束脚本并继续执行主脚本newserial.py的其余行?
How do I prevent functions with the same name from clashing when I import? 导入时如何防止同名函数发生冲突?
What is the best when to use s ubprocess.call / subprocess.Popen what are all the pipes for? 什么是最好的时候使用s ubprocess.call / subprocess.Popen什么都管了?

What is meant by the hint that you've found already is that you should wrap all your code in trackid.py like this: 已经发现的提示的含义是,应将所有代码包装在trackid.py中,如下所示:

def mytrackid():
    all your code goes here
    bla print bla

if __name__ == '__main__':
    mytrackid()

All the code that is not within a function or class will be executed at the moment that a module is imported. 导入模块时,将执行不在函数或类中的所有代码。 With

if __name__ == '__main__':

you make sure that this part is only being executed when the module is actually the main module. 您确保仅当模块实际上是主模块时才执行此部分。

If you are only calling trackid.py via subprocess.call() , then you don't need to import it at all. 如果仅通过trackid.py subprocess.call()调用trackid.py ,则根本不需要导入它。

You import a module only if it contains functions that you wish to call in your script that is doing the importing, so 仅当模块包含要在执行导入的脚本中调用的函数时,才导入模块,因此

script1.py : script1.py

def myfun():
   do stuff

def myfun2():
   return(results)

script2.py : script2.py

import script1

script1.myfun()
print script1.myfun2()

When you import a module any code that is not in a function will be executed, hence the problem you are seeing. 导入模块时,将执行功能中未包含的任何代码,因此会出现问题。 The if __name__ == __main__ construct is used, normally, to add code that is run for testing purposes when the script is running standalone. 通常,如果脚本独立运行,则使用if __name__ == __main__构造来添加为测试目的而运行的代码。

In this case, putting your code from trackid.py in the if __name__... block will ensure it is only executed when you do your subprocess.call() and not when you import the module but, I'm not sure why you want to do this; 在这种情况下,将来自trackid.py的代码放在if __name__...块中将确保仅在执行if __name__... subprocess.call()时执行该代码, trackid.py在导入模块时执行,但是,我不确定为什么要这样做想要这样做; the subprocess module is generally used to execute non-python code. subprocess模块通常用于执行非python代码。

I think what you probably want to do is have trackid.py like my script1 , above where it contains function(s) which you can call from newserial.py after importing it. 我认为您可能想做的是拥有trackid.py如我的script1 ,在其上面包含可以在导入后从newserial.py调用的newserial.py

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

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