-1

I need to run a command using python code and I tried to use both os.system and subprocess however both didn't work for some reason. Here's my code:

@app.route('/run-script')
def run_script():
   subprocess.call('python3.6 GoReport.py --id 31-33 --format word', cwd="working_dir", shell=True)
   return flask.render_template('results.html', **locals())

Running this command from terminal directly works as it should. Trying to reproduce this from python interpreter using command line works as as a charm as well. However it doesn't work when I use Flask. What's the reason for this?

  • 2
    what do you mean by "doesn't work" ? – Nullman Mar 24 '19 at 12:49
  • Why would you run Python as a subprocess of Python anyway? Just `import` the code you want to run; use `multiprocessing` if you ned to run it in a separate process. – tripleee Mar 24 '19 at 13:04
  • "However it doesn't work" - do you get any errors or nothing at all? – Dudnikof Mar 24 '19 at 14:29
  • No python problem. Seems to be a user group permission problem. ([here](https://stackoverflow.com/questions/55321451/how-to-run-a-terminal-command-using-python-script-which-is-held-through-wsgi-pr/55322885)) – Artist Unknown Mar 24 '19 at 20:39
  • No errors, just nothing happens when triggered from Flask :( >> Why would you run Python as a subprocess of Python anyway? The thing is that the initial script I want to run is written to parse terminal with CLICK package and I'm just not sure how to avoid it and pass arguments to the script from my own python code. – Bogdan Kozlovskyi Mar 25 '19 at 06:00

1 Answers1

0

So I've managed to edit my code and import the module instead of using subprocess and os.system. Thanks @tripleee for the explanation!