Reputation: 25
I am very disappointed that after looking for several hours, I have still not found an answer to: - Make a python script that opens another python script ?
Mabye I don't know how to search for stuff, but I am usually good at finding solutions online.
Any help would be gladly appreciated!
Here's my code:
import subprocess
subprocess.Popen(['C:\Users\user\Documents\run.py'])
Here is the python file called "run.py"
print('Hello World!')
Thanks!
Upvotes: 1
Views: 287
Reputation: 4739
i find this to work and it looks clearer:
import subprocess
import pathlib
path_to_file=pathlib.Path(__file__).parent.resolve()
script_path=f'{path_to_file}/your_script.py'
subprocess.run(['python',script_path])
Upvotes: 0
Reputation: 178
You can use the call
library, it's pretty easy:
from subprocess import call
call(["python", "your_file.py"])
Upvotes: 4
Reputation: 2960
This is how I will go about it. Suppose you have two files: Main.py
and run.py
and will want Main.py
to open and run this file run.py
:
Main.py
: it calls run.py
through import (run.py
file needs to be in the same folder)import run
print('Hello World!')
Then you can run Main.py
by calling python Main.py
the output will be : Hello World!
Let me know if it works.
Upvotes: 2
Reputation: 895
Try this:
your_cmd = "python3 path/to/file/run.py"
p = subprocess.Popen("exec " + your_cmd, stdout=subprocess.PIPE, shell=True)
Upvotes: 1