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!

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

the problem is that I have the files in different folders
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

1 Comment

i will be sure to try this!
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.

5 Comments

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?
Thank you for bringing this to my attention. I edited my comment respectively.
the problem is that I want to make the project public so everyone could download it. and the people have different paths
Please elaborate.
wait... if you don't need to enter the full path to th scripts, I'll try then!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.