1

Can someone give me a tip on how I can use os to run a different .py file from my python script? This code below works, but only because I specify the complete file path.

How can I modify the code to incorporate running plots.py from the same directory as my main script app.py? Im using Windows at the moment but hoping it can work on any operating system. Thanks

import os

os.system('py C:/Users/benb/Desktop/flaskEconServer/plots.py')
4
  • Have you tried just referring to it locally, doing os.system('py plots.py')? Commented May 28, 2019 at 19:57
  • 2
    Is there a reason you can't do from . import plots? Commented May 28, 2019 at 19:58
  • Additional note: this isn't why the os.system() call exists. Look into using subprocess, a standard-library module literally made for the purpose of executing other programs within a python program Commented May 28, 2019 at 19:58
  • What is the specific error the terminal gives you? Commented May 28, 2019 at 20:04

4 Answers 4

3

You can execute an arbitrary Python script as a separate process using the subprocess.run() function something like this:

import os
import subprocess
import sys

#py_filepath = 'C:/Users/benb/Desktop/flaskEconServer/plots.py'
py_filepath = 'plots_test.py'

args = '"%s" "%s" "%s"' % (sys.executable,                  # command
                           py_filepath,                     # argv[0]
                           os.path.basename(py_filepath))   # argv[1]

proc = subprocess.run(args)
print('returncode:', proc.returncode)

If you would like to communicate with the process while it's running, that can also be done, plus there are other subprocess functions, including the lower-level but very general subprocess.Popen class that support doing those kind of things.

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

Comments

0

You can get the directory of the app.py file by using the following call in app.py

dir_path = os.path.dirname(os.path.realpath(__file__))

then join the file name you want

file_path = os.path.join(dir_path,'plot.py')

Finally your system call

os.system(f'py {file_path}') # if you're on 3.6 and above.
os.system('py %s' % file_path) # 3.5 and below

As others have said sub-processes and multi-threading may be better, but for your specific question this is what you want.

Comments

0

Python has built-in support for executing other scripts, without the need for the os module.

Try:

from . import plots

If you want to execute it in an independent python process, look into the multiprocessing or subprocess modules.

Comments

-1

For it to run on all operating systems I would use the following:

import os
path = os.path.join(os.path.dirname(__file__), "plots.py")
os.system(f"py {plots.py}")

However


import os

os.system('py ./plots.py')

Seems to be the easiest solution on Ubuntu.

1 Comment

Not all operating systems have py – my macOS doesn't, for one. sys.executable would give you the path of the currently running Python interpreter.

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.