如何让一个python文件运行另一个?(复制)

时间:2022-02-19 20:48:13

This question already has an answer here:

这个问题已经有了答案:

How can I make one python file to run another?

如何使一个python文件运行另一个python文件?

For example I have two .py files. I want one file to be run, and then have it run the other .py file.

例如,我有两个。py文件。我希望运行一个文件,然后让它运行另一个.py文件。

8 个解决方案

#1


260  

There are more than a few ways. I'll list them in order of inverted preference (i.e., best first, worst last):

有很多方法。我将按照倒向偏好(即。,最好的第一,最差的最后):

  1. Treat it like a module: import file. This is good because it's secure, fast, and maintainable. Code gets reused as it's supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.
  2. 将其视为模块:导入文件。这很好,因为它是安全、快速和可维护的。代码被重用,就像它应该做的那样。大多数Python库使用扩展到许多文件的多个方法来运行。强烈推荐。注意,如果您的文件被称为file。py,您的导入不应该在末尾包含.py扩展。
  3. The infamous (and unsafe) exec command: execfile('file.py'). Insecure, hacky, usually the wrong answer. Avoid where possible.
  4. 臭名昭著(且不安全)的执行命令:execfile(“file.py”)。不安全的,平庸的,通常是错误的答案。避免在可能的情况下。
  5. Spawn a shell process: os.system('python file.py'). Use when desperate.
  6. 生成shell进程:os。系统(“python file.py”)。绝望时使用。

#2


31  

Get one python file to run another, using python 2.7.3 and Ubuntu 12.10:

  1. Put this in main.py:

    把这个main.py:

    #!/usr/bin/python
    import yoursubfile
    
  2. Put this in yoursubfile.py

    把这个放在yoursubfile.py

    #!/usr/bin/python
    print "hello";
    
  3. Run it:

    运行该程序:

    python main.py 
    
  4. It prints:

    它打印:

    hello
    

Thus main.py runs yoursubfile.py

因此主要。py yoursubfile.py运行

There are 8 ways to answer this question, A more canonical answer is here: Python: How to import other Python files

有8种方法可以回答这个问题,一个更规范的答案是:Python:如何导入其他Python文件

#3


11  

  • you can run your .py file simply with this code:
  • 您可以使用以下代码运行.py文件:

import os 
os.system('python filename.py')

note: put the file in the same directory of your main python file.

注意:将文件放在主python文件的相同目录中。

#4


4  

You'd treat one of the files as a python module and make the other one import it (just as you import standard python modules). The latter can then refer to objects (including classes and functions) defined in the imported module. The module can also run whatever initialization code it needs. See http://docs.python.org/tutorial/modules.html

您将其中一个文件视为python模块,并让另一个文件导入它(就像您导入标准的python模块一样)。然后,后者可以引用导入模块中定义的对象(包括类和函数)。该模块还可以运行它需要的任何初始化代码。参见http://docs.python.org/tutorial/modules.html

#5


4  

I used subprocess.call it's almost same like subprocess.Popen

我使用的子流程。调用它就像子进程。popen

from subprocess import call
call(["python", "your_file.py"])

#6


1  

You could use this script:

你可以使用这个脚本:

def run(runfile):
  with open(runfile,"r") as rnf:
    exec(rnf.read())

Syntax:

语法:

run("file.py")

#7


1  

from subprocess import Popen

Popen('python filename.py')

or how-can-i-make-one-python-file-run-another-file

或how-can-i-make-one-python-file-run-another-file

#8


0  

It may be called aa1.py from main script as below,

它可以叫做aa1。py来自主脚本如下,

#!/usr/bin/python
import aa1
aa1

aa1.py may be something below,

aa1。py可能在下面,

print'abc'

#1


260  

There are more than a few ways. I'll list them in order of inverted preference (i.e., best first, worst last):

有很多方法。我将按照倒向偏好(即。,最好的第一,最差的最后):

  1. Treat it like a module: import file. This is good because it's secure, fast, and maintainable. Code gets reused as it's supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.
  2. 将其视为模块:导入文件。这很好,因为它是安全、快速和可维护的。代码被重用,就像它应该做的那样。大多数Python库使用扩展到许多文件的多个方法来运行。强烈推荐。注意,如果您的文件被称为file。py,您的导入不应该在末尾包含.py扩展。
  3. The infamous (and unsafe) exec command: execfile('file.py'). Insecure, hacky, usually the wrong answer. Avoid where possible.
  4. 臭名昭著(且不安全)的执行命令:execfile(“file.py”)。不安全的,平庸的,通常是错误的答案。避免在可能的情况下。
  5. Spawn a shell process: os.system('python file.py'). Use when desperate.
  6. 生成shell进程:os。系统(“python file.py”)。绝望时使用。

#2


31  

Get one python file to run another, using python 2.7.3 and Ubuntu 12.10:

  1. Put this in main.py:

    把这个main.py:

    #!/usr/bin/python
    import yoursubfile
    
  2. Put this in yoursubfile.py

    把这个放在yoursubfile.py

    #!/usr/bin/python
    print "hello";
    
  3. Run it:

    运行该程序:

    python main.py 
    
  4. It prints:

    它打印:

    hello
    

Thus main.py runs yoursubfile.py

因此主要。py yoursubfile.py运行

There are 8 ways to answer this question, A more canonical answer is here: Python: How to import other Python files

有8种方法可以回答这个问题,一个更规范的答案是:Python:如何导入其他Python文件

#3


11  

  • you can run your .py file simply with this code:
  • 您可以使用以下代码运行.py文件:

import os 
os.system('python filename.py')

note: put the file in the same directory of your main python file.

注意:将文件放在主python文件的相同目录中。

#4


4  

You'd treat one of the files as a python module and make the other one import it (just as you import standard python modules). The latter can then refer to objects (including classes and functions) defined in the imported module. The module can also run whatever initialization code it needs. See http://docs.python.org/tutorial/modules.html

您将其中一个文件视为python模块,并让另一个文件导入它(就像您导入标准的python模块一样)。然后,后者可以引用导入模块中定义的对象(包括类和函数)。该模块还可以运行它需要的任何初始化代码。参见http://docs.python.org/tutorial/modules.html

#5


4  

I used subprocess.call it's almost same like subprocess.Popen

我使用的子流程。调用它就像子进程。popen

from subprocess import call
call(["python", "your_file.py"])

#6


1  

You could use this script:

你可以使用这个脚本:

def run(runfile):
  with open(runfile,"r") as rnf:
    exec(rnf.read())

Syntax:

语法:

run("file.py")

#7


1  

from subprocess import Popen

Popen('python filename.py')

or how-can-i-make-one-python-file-run-another-file

或how-can-i-make-one-python-file-run-another-file

#8


0  

It may be called aa1.py from main script as below,

它可以叫做aa1。py来自主脚本如下,

#!/usr/bin/python
import aa1
aa1

aa1.py may be something below,

aa1。py可能在下面,

print'abc'