1

Could you help to check what's going on with subprocess, it performs differently on different machines with same Python version, but one is on Ubuntu docker and one is on Windows.

Ubuntu docker

I use subprocess to execute an external Python script with parameter shell=True, actually it opens a new process for me without executing the specified script, so I have to remove the parameter shell=True and then everything works as expected.

You can see from the screenshot below, I need to exit() after executing the first subprocess, and ran the second subprocess without shell=True.

screenshot in ubuntu

Windows

In Windows, shell=True works same as I execute subprocess in Ubuntu without shell=True parameter.

screenshot in windows

1 Answer 1

1

Quoting https://docs.python.org/3/library/subprocess.html#popen-constructor:

On POSIX with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself.

(emphasis mine)

That means, in your first example with run(['python', 'script.py'], shell=True) you are actually only starting an interactive Python session and not passing the script to the interpreter.

Further:

The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

Conclusion: Whenever possible, pass the arguments as a list (as you did), but do not use shell=True.

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

1 Comment

Thanks a lot mkrieger1! Great answer!

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.