2

I have multiple python codes in different folders which have to run in a sequence. There are similar questions asked before but, the answers do not seem to work out. i have tried the following commands:

subprocess.check_call(["Python", "D:/demo/full_alg.py"])
subprocess.check_output(["Python", "D:/demo/full_alg.py"])
os.system("D:/demo/full_alg.py")

That full_alg.py file, when executed must print out a list of files on which the code was executed and create corresponding tiff files

rao
  • 123
  • 3

2 Answers2

0

I am wondering why you are not trying to import full_alg.py package and based on the function calls in main consume the results of the call. How to precisely do it depends strongly on the contents of full_alg.py though.

If you insist on running a subprocess which runs another python script I suggest looking at multiprocessing module and the examples here.

The reasons why your code does not work may be Python interpreter missing from PATH. I would suggest passing the full path to both the interpreter you are using and the script. This should do in case of running a subprocess.check_call or subprocess.check_output. For the last one (os.system) I don't think it can run unless you set up python interpreter to be the default application for opening *.py files and even then it depends on the non-obvious configuration of the OS to run.

sophros
  • 14,672
  • 11
  • 46
  • 75
  • Thank you for suggesting that module but the examples define a function and then call the function in the main. I would like to know how do I call python script externally and execute it? – rao Jan 23 '18 at 10:35
0

You can use subprocess library such as below:

import subprocess

args = ['{}/manage.py'.format('/home/<you>/<path>'), 'runserver']
subprocess.Popen(args, stdout=subprocess.PIPE)

Follow this method: My answer in another post


[UPDATE]:

This is an example with python3:

import subprocess

python_version = '3'
path_to_run = './'
py_name = '__main__.py'

# args = [f"python{python_version}", f"{path_to_run}{py_name}"]  # Available in python3
args = ["python{}".format(python_version), "{}{}".format(path_to_run, py_name)]

res = subprocess.Popen(args, stdout=subprocess.PIPE)
output, error_ = res.communicate()

if not error_:
    print(output)
else:
    print(error_)
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150