How to run a Python file in another Python file?

Sometimes, we want to run a Python file in another Python file.

In this article, we’ll look at how to run a Python file in another Python file.

How to run a Python file in another Python file?

To run a Python file in another Python file, we can use the subprocess.call method.

For instance, we write

from subprocess import call

call(["python", "your_file.py"])

to call call with a list with the command and arguments to run the your_file.py Python script in the same directory as the current script.

Conclusion

To run a Python file in another Python file, we can use the subprocess.call method.