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])
Matthew Smith
  • 508
  • 7
  • 22
Avic
  • 459
  • 1
  • 6
  • 15
  • 1
    Are you sure you don't want to _import_ the module? – henrycjc Sep 30 '18 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 ? – Avic Sep 30 '18 at 10:40
  • https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another – iamklaus Sep 30 '18 at 10:41
  • First think about organising it in such way that you don't have multiple instances of the same file. Then think about having everything in the same folder. Then, if you are positive that you want multiple folders, configure the `PYTHONPATH` correctly so that python can find the modules in all folders you are using – zvone Sep 30 '18 at 10:44
  • 1
    Possible duplicate of [How can I make one python file run another?](https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another) – Matthew Smith Sep 30 '18 at 10:50
  • 1
    i think it is not duplicate , my problem is the another program in different folder – Avic Sep 30 '18 at 11:21

1 Answers1

11

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
Matthew Smith
  • 508
  • 7
  • 22
  • i think it doesnt work if the another python program in different folder – Avic Sep 30 '18 at 11:23
  • @Avic I have updated my answer to provide a solution at the end that should work. Let me know if it doesn't. – Matthew Smith Sep 30 '18 at 11:36
  • it doesnt work, but when i change relative path to absolut path ( D:/calc_system/calculation_folder ) it works , thanks – Avic Sep 30 '18 at 12:03
  • oh i know , '/calculation_folder' , should be changed to 'calculation_folder' , and know it works – Avic Sep 30 '18 at 12:06
  • @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. – Matthew Smith Oct 01 '18 at 02:44
  • I think `sys.path.insert(1, 'calculation_folder')` might be better. Then you make sure that `path[0]` is still the folder of the original file. – Carsten Jan 07 '20 at 14:00