6

How to run another python scripts in a different folder?

I have main program: calculation_control.py

In the folder calculation_folder, there is calculation.py

How do I run calculation_folder/calculation.py from within calculation_control.py?

So far I have tried the following code:

calculation_file = folder_path + "calculation.py"
if not os.path.isfile(parser_file) :

    continue


subprocess.Popen([sys.executable, parser_file])
6
  • 1
    Are you sure you don't want to import the module? Commented Sep 30, 2018 at 10:33
  • how to import from different folder ? , and i have calculation.py in several folder . and they have same variable and function. will it be a problem if i import all calculation.py to calculation_control.py ? Commented Sep 30, 2018 at 10:40
  • stackoverflow.com/questions/7974849/… Commented Sep 30, 2018 at 10:41
  • 1
    Possible duplicate of How can I make one python file run another? Commented Sep 30, 2018 at 10:50
  • 1
    i think it is not duplicate , my problem is the another program in different folder Commented Sep 30, 2018 at 11:21

1 Answer 1

10

There are more than a few ways. I'll list them in order of inverted preference (i.e., best first, worst last):

  1. Treat it like a module: import file. This is good because it's secure, fast, and maintainable. Code gets reused as it's supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.
  2. The infamous (and unsafe) exec command: execfile('file.py'). Insecure, hacky, usually the wrong answer. Avoid where possible.
  3. Spawn a shell process: os.system('python file.py'). Use when desperate.

Source: How can I make one python file run another?


Solution

Python only searches the current directory for the file(s) to import. However, you can work around this by adding the following code snippet to calculation_control.py...

import sys
sys.path.insert(0, 'calculation_folder') # Note: if this relavtive path doesn't work or produces errors try replacing it with an absolute path
import calculation
Sign up to request clarification or add additional context in comments.

6 Comments

i think it doesnt work if the another python program in different folder
@Avic I have updated my answer to provide a solution at the end that should work. Let me know if it doesn't.
it doesnt work, but when i change relative path to absolut path ( D:/calc_system/calculation_folder ) it works , thanks
oh i know , '/calculation_folder' , should be changed to 'calculation_folder' , and know it works
@Avic If you have found my answer helpful and/or a solution to your issue, please take a moment to mark it as the accepted answer. Thanks.
|

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.