[Solved] The difference between subprocess shell=False and shell=True in python | pyinstaller reports error urllib3.packages.six.moves pre-safe-imp

https://www.cnblogs.com/pengfy/p/12108054.html
pyinstaller reports error urllib3.packages.six.moves pre-safe-import-module hook failed, needs fixing.

When packaging with pyinstaller, I got the following error:

urllib3.packages.six.moves pre-safe-import-module hook failed, needs fixing.

I searched for a long time and couldn’t find the problem. I suddenly remembered that because of another problem, I modified the __init__ of the popen class in the subprocess.py file that comes with python, and changed the shell=False inside to shell=True. Try Changed it back and found that the problem was solved.

So don’t change the library that comes with python casually, because the dependencies between other third-party libraries are complex.

https://blog.csdn.net/xiaoyaozizai017/article/details/72794469
The shell=True parameter will make subprocess.call accept a variable of type string as a command, and call the shell to execute the string. When shell=False, subprocess.call only accepts an array variable as a command, and the first array of The element is used as a command, and the rest are used as parameters of the command.

To illustrate with an example:

from subprocess import call
import shlex
cmd = "cat test.txt; rm test.txt"
call(cmd, shell=True)

In the above script, the setting of shell=True, the final effect is to execute two commands

cat test.txt and rm test.txt

Change shell=True to False,

from subprocess import call
import shlex
cmd = "cat test.txt; rm test.txt"
cmd = shlex(cmd)
call(cmd, shell=False)

When calling call, only the cat command will be executed, and the three strings of “test.txt;” “rm” “test.txt” are used as parameters of cat, so it is not what we intuitively see as if there are two a shell command.

Maybe you will say that shell=True is not very good, and executing two commands is what I expect. But in fact, this approach is unsafe, because multiple commands are separated by semicolons. If the inspection is not careful enough, the consequences of executing dangerous commands such as rm -rf / will be very serious, and using shell=False This risk can be avoided.

Generally speaking, it depends on the actual needs. The official recommendation is to try not to set shell=True.