简体   繁体   English

python导入模块vs运行脚本作为subprocess.popen

[英]python import module vs running script as subprocess.popen

Suppose I have python script having having 4-5 functions all are called from single function in a script. 假设我有一个具有4-5个函数的python脚本,所有脚本都从单个函数中调用。 If I want to results after executing script( Use functions from another script) I can make script executable and use subprocess.popen and I can also import these functions in another script. 如果我想在执行脚本后得到结果(使用另一个脚本中的函数),则可以使脚本可执行并使用subprocess.popen,也可以将这些函数导入另一个脚本中。 Which is better way to do this? 哪种更好的方法呢?

Which is better way to do this? 哪种更好的方法呢?

Use import unless you have to use subprocess.Popen to run Python code. 除非必须使用subprocess.Popen来运行Python代码,否则请使用import

  • import uses sys.path to find the module; import使用sys.path查找模块; you don't need to specify the path explicitly 您不需要显式指定路径
  • usually, imported functions accept arguments, return results in the same process; 通常,导入的函数接受参数,并在同一过程中返回结果; you don't need to serialize Python objects into bytes to send them to another process 您无需将Python对象序列化为字节即可将其发送到另一个进程

If you want to use functions from an other script then you usually import the script. 如果要使用其他脚本中的功能,则通常会导入该脚本。

When the script is script.py you can write import script and use the functions that are defined in the script with script.function_in_the_script . 当脚本为script.py您可以编写import script ,并将脚本中定义的函数与script.function_in_the_script一起script.function_in_the_script

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

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