How can I make one python file run another?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There are several ways to make one Python file run another, but the right choice depends on what you actually mean by "run." Sometimes you want to reuse functions from another file in the same process, and sometimes you want to launch a completely separate Python process. Those are different operations, and treating them the same leads to brittle code.
Use import When You Want Reuse
If the other file contains functions or classes you want to call, the cleanest solution is to turn it into a module and import it.
This keeps both files in the same Python process. It is the best approach when the second file is part of the same application and you want normal function calls, shared objects, and testable design.
One important detail is that top-level code in the imported file runs at import time. That is why reusable modules should usually protect executable demo code with if __name__ == "__main__":.
Use subprocess When You Want a Separate Program
If the second file should run as its own program with a separate process, use subprocess. This is the right tool when the called script has its own command-line interface, environment, or failure boundary.
Using sys.executable matters because it ensures the child script runs with the same Python interpreter as the parent. That avoids a long list of virtual-environment and path problems.
runpy Exists, but It Is a Niche Tool
Python also has the runpy module, which can execute another module as if it were run as a script. This is occasionally useful for tooling, but it is usually not the first choice for application code.
This still runs in the current interpreter, which means it shares process state in ways that can be surprising. For ordinary software design, import or subprocess is usually clearer.
What to Avoid
A lot of beginners discover patterns like this:
Technically, it executes the file, but it is rarely a good design. It mixes namespaces, makes debugging harder, and creates security problems if the target code is not fully trusted. In most real projects, this is the wrong abstraction.
How to Choose
A practical rule is:
- use
importfor reusable code in the same application - use
subprocessfor running another script as a separate program - use
runpyonly when you specifically need script-style execution inside the same interpreter
Once you frame the problem that way, the choice becomes much simpler.
Common Pitfalls
- Using
exec()when an import or subprocess call would be safer and clearer. - Forgetting that imported modules execute top-level code on first import.
- Launching a subprocess when a normal function call would be simpler.
- Hard-coding
pythoninstead of usingsys.executable. - Mixing application logic and script entry-point logic in the same file without clear boundaries.
Summary
- '
importis the best option for code reuse in the same Python process.' - '
subprocessis the best option for running another script as a separate program.' - '
runpyis a specialized middle ground, not the default answer.' - Avoid
exec()for ordinary cross-file execution. - Decide first whether you need reuse or process isolation, then choose the tool.
Related reading
- How can I map True/False to 1/0 in a Pandas DataFrame?
- How can I multiply all items in a list together with Python?
- How can I obtain the element-wise logical NOT of a pandas Series?
- How can I one hot encode in Python?
- How can I one hot encode in Python?
- How can I open multiple files using with open in Python?
- How can I open multiple files using with open in Python?
- How can I overcome "datetime.datetime not JSON serializable"?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.