0

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!

Sheva Kadu
  • 43
  • 9
  • Does this answer your question? [How can I make one python file run another?](https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another) – Tomerikoo Dec 27 '20 at 08:21

3 Answers3

1

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.

marcos
  • 4,099
  • 1
  • 6
  • 18
0

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.

Amiram
  • 887
  • 3
  • 13
  • From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Dec 16 '19 at 14:57
  • Thank you for bringing this to my attention. I edited my comment respectively. – Amiram Dec 16 '19 at 17:38
  • the problem is that I want to make the project public so everyone could download it. and the people have different paths – Sheva Kadu Dec 29 '19 at 14:39
  • Please elaborate. – Amiram Dec 29 '19 at 16:06
  • wait... if you don't need to enter the full path to th scripts, I'll try then! – Sheva Kadu Jan 05 '20 at 10:33
0

Another python script can be executed by using runpy module.

Let's say we have two scripts as:

  • abc.py
print('Executing abc.py')
  • main.py
import runpy
runpy.run_path('abc.py') #Path to the abc.py
  • Output
Executing abc.py