How can I make one python file run another?

Python

Python Problem Overview


How can I make one python file to run another?

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

Python Solutions


Solution 1 - Python

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. The infamous (and unsafe) exec command: Insecure, hacky, usually the wrong answer. Avoid where possible.
  • execfile('file.py') in Python 2
  • exec(open('file.py').read()) in Python 3
  1. Spawn a shell process: os.system('python file.py'). Use when desperate.

Solution 2 - Python

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

  1. Put this in main.py:

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

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

    python main.py 
    
  4. It prints:

    hello
    

Thus main.py runs yoursubfile.py

There are 8 ways to answer this question, A more canonical answer is here: https://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files/20749411#20749411

Solution 3 - Python

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

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

Solution 4 - Python

  • you can run your .py file simply with this code:

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

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

Solution 5 - Python

from subprocess import Popen

Popen('python filename.py')

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

Solution 6 - Python

You could use this script:

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

Syntax:

run("file.py")

Solution 7 - Python

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

Solution 8 - Python

It may be called abc.py from the main script as below:

#!/usr/bin/python
import abc

abc.py may be something like this:

print'abc'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan TornquistView Question on Stackoverflow
Solution 1 - PythonapcView Answer on Stackoverflow
Solution 2 - PythonEric LeschinskiView Answer on Stackoverflow
Solution 3 - PythonSamat SadvakasovView Answer on Stackoverflow
Solution 4 - PythonAyserView Answer on Stackoverflow
Solution 5 - PythonAxel DerView Answer on Stackoverflow
Solution 6 - Pythonxcrafter_40View Answer on Stackoverflow
Solution 7 - PythonAdam ZalcmanView Answer on Stackoverflow
Solution 8 - PythonAlphardView Answer on Stackoverflow