. Advertisement .
..3..
. Advertisement .
..4..
How to run Python in another Python script? The tutorial offers three methods to do this:
How To Run A Python Script
Running a Python script will be a piece of cake if you use the command-line.
Use the python Command
Open the command-line and type in “python3” or “python”:
$ python3 hello.py
Hello World!
Press Enter to see the phrase on your screen.
Redirect The Output
Saving a script’s output for later analysis will save your time. Type in the code:
$ python3 hello.py > output.txt
Use The -m Option
The module-name should be the name of an object, instead of a string. This option can search sys.path for the name and run its content as _main_:
$ python3 -m hello
Hello World!
Use The Script Filename
If you have recent Windows versions, you can run Python scripts by entering the file’s name. This file should contain the code:
C:\devspace> hello.py
Hello World!
How To Run Python In Another Python Script
There are three methods to run Python in another Python script.
Method 1: Use The Import Statement
Syntax:
import script_name_to_call
Step 1: Place All Scripts In One Folder
Suppose that two scripts python_1 and python_2 are saved in the same folder. Now you have to run the latter script from the former one.
Step 2: Add the syntax
import python_2
print(‘what are you up to?’)
print(‘hello world’)
Step 3: RUn One Script From Another
Script1.py:
def func1():
print ("Function 1 is active")
if __name__ == '__main__':
# Script2.py executed as script
# do something
func1()
Output:
Function 1 is active
Script2.py:
import Script1.py
def func2():
print("Function 2 is active")
if __name__ == '__main__':
# Script2.py executed as script
# do something
func2()
Script1.func1()
Output:
Function 2 is active
Function 1 is active
Method 2: Use The execfile() Method
The execfile() function works in Python 2 while it exists in Python 3 under the name of exec() function. Here is how to use this function to run a Python script in another:
Script2.py:
# Python 2 code
execfile("Script1.py")
Output:
Function 1 is active
The same thing can be done in Python 3 by using:
Script2.py:
exec(open("Script1.py").read())
Output:
Function 1 is active
Method 3: Use The subprocess module
Here is the run() command’s syntax:
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
The running command will be described by arguments so wait for it to complete and return a CompletedProcess instance.
Not it is time to launch script to execute the Python script:
import subprocess
p = subprocess.run('python count.py', shell=True)
# or
#p = subprocess.run(['python', 'count.py'])
print('returncode', p.returncode)
print('EXIT')
After executing the commands, follow our code to run your Python script in another:
Script1.py:
def func1():
print ("Function 1 is active")
if __name__ == '__main__':
# Script2.py executed as script
# do something
func1()
Script2.py:
import subprocess
subprocess.call("Script1.py", shell=True)
Output:
Function 1 is active
Conclusion
How to run Python in another Python script? This tutorial has introduced three common and effective methods. The last one seems to have a superior advantage over the others.
It saves you time in editing a Python script and putting the code into a subroutine.
Leave a comment