0

I'm trying to run from my python script an external python script asynchronously. I use Popen to run it:

main.py:

from subprocess import Popen
arg1='foo'
p=Popen(['python', 'external_python_script.py', arg1],shell=True)

but nothing happens.

When I run python external_python script.py 'foo' from the shell I get the expected result - what am I doing wrong?

Binyamin Even
  • 3,318
  • 1
  • 18
  • 45
  • because I want it to run asynchronously. – Binyamin Even Nov 29 '20 at 17:56
  • the 'foo' in your python code does not translate to 'foo' in the terminal, since the single quotes are used to signify a string. If you want to write 'foo' to the terminal, then you should write arg1 = '\'foo\'' . – Foivoschr Nov 29 '20 at 17:59
  • I wouldn't recommend using `shell=True` due to security issues unless it's necessary and you know how to do it securely. – Brian McCutchon Nov 29 '20 at 18:00
  • 1
    Which operating system? Don't set `shell=True` for a list - on a posix system, those extra parameters are for the shell program itself, not the thing being execute.d – tdelaney Nov 29 '20 at 18:02
  • Does this answer your question? [Actual meaning of 'shell=True' in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – Brian McCutchon Nov 29 '20 at 18:07
  • @tdelaney I tried also without shell=True and it still doesn't work. – Binyamin Even Nov 29 '20 at 18:08
  • 1
    Which operating system? I cooked up an example on linux and ran it on the command line. Without `shell=True` it worked. With `shell=True` it brought up the python shell because everything past the first "python" argument was ignored. – tdelaney Nov 29 '20 at 18:10
  • Also, how did you run it? Things that write to stdout work differently depending on whether they were run from the console, double-clicked or run in an IDE. – tdelaney Nov 29 '20 at 18:12
  • I'm getting the same results as tdelaney. You'll need to add more info to help us reproduce, such as the OS and a minimal example version of `external_python_script.py` that reproduces the problem. – Brian McCutchon Nov 29 '20 at 18:13
  • Another possibility is that `external_python_script.py` is just slow and doesn't finish until some time after the main script exits. – Brian McCutchon Nov 29 '20 at 18:14
  • @Foivoschr - You don't want to add extra quotes when using a list for `cmd`. The quotes will still be on the parameter when the executed program tries to use it. – tdelaney Nov 29 '20 at 18:18

0 Answers0