Run One Python Script From Another in Python
Last Updated :
16 Sep, 2024
In Python, we can run one file from another using the import statement for integrating functions or modules, exec() function for dynamic code execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the same process. In this article, we will explore all the specified approaches to make one Python file run another.
Run One Python Script From Another in Python
Below are some of the ways by which we can make one Python file run another in Python:
Make One Python File Run Another Using Import Module
In this example, file_2.py imports functions from file_1.py using the import statement. By calling the imported function in file_2.py (file_1.printing()), the code executes the printing() function defined in file_1.py, allowing for the integration and execution of code from one file within another.
Python
# file_1.py
# creating function
def printing():
print("Hello world!")
Python
# file_2.py
# importing file_1
import file_1
# calling function of file_1.py form file_2.py
file_1.printing()
Output:

Run One Python Script From Another Using exec() Function
In this example, file_2.py opens and reads the file_1.py, by 'with' statement using the 'open' function it opens file_1.py, and the 'read' function reads the file_1.py. The code read from file_1.py will be executed by using the exec() function within file_2.py.
Python
# file_1.py
# printing hello world!
print("Hello world!")
Python
# file_2.py
# opening file_1.py and reading it with read() and executing if with exec()
with open("file_1.py") as file:
exec(file.read())
Output:

Python Run One Script From Another Using subprocess Module
In this example, file_2.py imports the subprocess module using the import statement. By using run() function Python script in "file_1.py" will be executed as a separate process within file_2.py.
Python
# file_1.py
# printing This is file_1 content.
print("This is file_1 content.")
Python
# file_2.py
# importing subprocess module
import subprocess
# running other file using run()
subprocess.run(["python", "file_1.py"])
Output:

Run One Python Script From Another Using os.system() Function
In this example, file_2.py imports the os module using the import statement. Then using os.system() to execute the command "python file_1.py" and file_1.py will run within the same process but in a separate system shell.
Python
# file_1.py
# printing This is file_1 content.
print("This is file_1 content.")
Python
# file_2.py
# importing os module
import os
# running other file using run()
os.system("python file_1.py")
Output:

Conclusion
In conclusion, understanding how to make one Python file run another is a useful skill for writing organized and modular code. Using the import statement and grasping the concept of modules allows for improved code reusability. Incorporating functions and classes enhances the structure and readability of your code. Remembering to organize files and follow naming conventions is essential for a collaborative development process. In essence, mastering these techniques helps you create well-structured and scalable Python applications.
Similar Reads
How to Run Another Python script with Arguments in Python Running a Python script from another script and passing arguments allows you to modularize code and enhance reusability. This process involves using a subprocess or os module to execute the external script, and passing arguments can be achieved by appending them to the command line. In this article,
3 min read
How Can I Make One Python File Run Another File? In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Ma
2 min read
Print Output from Os.System in Python In Python, the os.system() function is often used to execute shell commands from within a script. However, capturing and printing the output of these commands can be a bit tricky. This article will guide you through the process of executing a command using os.system() and printing the resulting valu
3 min read
How to Keep a Python Script Output Window Open? We have the task of how to keep a Python script output window open in Python. This article will show some generally used methods of how to keep a Python script output window open in Python. Keeping a Python script output window open after execution is a common challenge, especially when running scri
2 min read
How Use Linux Command In Python Using System.Os Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read
Running Python program in the background Let us see how to run a Python program or project in the background i.e. the program will start running from the moment device is on and stop at shut down or when you close it. Just run one time to assure the program is error-bug free One way is to use pythonw, pythonw is the concatenation of python
3 min read
How to clear screen in python? When working in the Python interactive shell or terminal (not a console), the screen can quickly become cluttered with output. To keep things organized, you might want to clear the screen. In an interactive shell/terminal, we can simply usectrl+lBut, if we want to clear the screen while running a py
4 min read
Os Module Vs. Sys Module In Python Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you
5 min read
How to Import Other Python Files? We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python's modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse
3 min read
Understanding the Execution of Python Program This article aims at providing a detailed insight into the execution of the Python program. Let's consider the below example. Example: Python3 a = 10 b = 10 print("Sum ", (a+b)) Output: Sum 20 Suppose the above python program is saved as first.py. Here first is the name and .py is the exte
2 min read