How do i call a python program from another program?

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.

How do i call a python program from another program?

wj127

1181 silver badge12 bronze badges

asked Nov 2, 2011 at 1:29

How do i call a python program from another program?

Nathan TornquistNathan Tornquist

6,16810 gold badges46 silver badges72 bronze badges

4

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
  3. Spawn a shell process: os.system('python file.py'). Use when desperate.

answered Nov 2, 2011 at 3:27

20

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: How to import other Python files?

Jonathan

6,2977 gold badges46 silver badges66 bronze badges

answered Dec 8, 2013 at 18:14

How do i call a python program from another program?

Eric LeschinskiEric Leschinski

139k90 gold badges405 silver badges327 bronze badges

7

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

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

answered Jul 12, 2017 at 3:38

How do i call a python program from another program?

6

  • 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.

answered Jan 23, 2016 at 19:46

How do i call a python program from another program?

AyserAyser

94511 silver badges15 bronze badges

4

You could use this script:

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

Syntax:

run("file.py")

answered Apr 16, 2017 at 15:26

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

answered Nov 2, 2011 at 1:37

Adam ZalcmanAdam Zalcman

26.2k4 gold badges68 silver badges91 bronze badges

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'

How do i call a python program from another program?

Lioness100

8,0225 gold badges15 silver badges46 bronze badges

answered Jun 1, 2017 at 19:19

1

The following are steps to merge..
Open file1. txt and file2. txt in read mode..
Open file3. txt in write mode..
Read the data from file1 and add it in a string..
Read the data from file2 and concatenate the data of this file to the previous string..
Write the data from string to file3..
Close all the files..

How do I run a Python script from another directory?

To make Python scripts runnable from any location under Windows:.
Create directory to put all your python scripts in. ... .
Copy all your python scripts into this directory..
Add the path to this directory in Windows "PATH" system variable: ... .
Run or restart "Anaconda Prompt".
Type "your_script_name.py".

How do I use a function from another file in Python?

To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.

Can you call a Python script from Java?

The class can be used within Java code called from the Python interpreter or it can be used within Java to run Python embedded in Java. The CPython class binds the Python interpreter to the JVM and provides the ability to execute Python scripts.