Reputation: 101
Hello!
I want to run a script within another script but the second script is in a different path. How do I do it?
I'm using tkinter and Python 3.7.1 on macOS Catalina. Thanks!
Upvotes: 0
Views: 861
Reputation: 1305
It depends if you like to get the results of the first script. If not (no output), the easy way:
import os os.system('full_path_to_the_script')
If you mind the output of the script you are running you can use the following:
import subprocess p = subprocess.Popen('full_path_to_the_script', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() ret_code = p.returncode
For more information about subprocess.
You can see more examples here.
P.S If the script is located at the same location of the running script I believe that there is no need in full path.
Upvotes: 0
Reputation: 119
Another python script can be executed by using runpy module.
Let's say we have two scripts as:
print('Executing abc.py')
import runpy
runpy.run_path('abc.py') #Path to the abc.py
Executing abc.py
Upvotes: 0
Reputation: 4510
You basically import the class/function from the other script. For example:
from other_script import a_class
Now, make sure you open your IDLE in the same directory of the other script so you can import it.
Upvotes: 1