Using subprocess to open a cmd to run a Python function but having issues with whitespaces and double quotations - Stack Overflo

admin2025-04-16  5

I need to address a very specific workflow where a function generates locks that are tied to the Python shell session. As a result, I'm trying to use subprocess to run that function in a completely separate cmd session.

I have a command that works in cmd:

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" -c "import sys; sys.path.insert(1, r'\\...\my_script_directory');import my_script; my_script.my_function('param 1', 'param 2')"

But when I try to execute it in subprocess.Popen, it keeps running into issues with the double quotations.

If I pass that cmd as a string into subprocess.Popen:

subprocess.Popen(cmd.exe c "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" -c "import sys; sys.path.insert(1, r'\\...\my_script_directory');import my_script; my_script.my_function('param 1', 'param 2')")

then I get the error: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

If I add additional quotations like so:

subprocess.Popen(cmd.exe /c """"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"""" -c """"import sys; sys.path.insert(1, r'\\...\my_script_directory');import my_script; my_script.my_function('param 1', 'param 2')"""")

then I get the error: The system cannot find the path specified.

I'm currently at a place where I can run Python code that has no spaces if I omit the quotations entirely

subprocess.Popen(cmd.exe /c "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" -c print(1+1))

which successfully returns 2

Does subprocess.Popen and/or cmd.exe handle quotations for commands to an executable differently? I've tried multiple escape characters for spaces as well, but can't get it to work.

I need to address a very specific workflow where a function generates locks that are tied to the Python shell session. As a result, I'm trying to use subprocess to run that function in a completely separate cmd session.

I have a command that works in cmd:

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" -c "import sys; sys.path.insert(1, r'\\...\my_script_directory');import my_script; my_script.my_function('param 1', 'param 2')"

But when I try to execute it in subprocess.Popen, it keeps running into issues with the double quotations.

If I pass that cmd as a string into subprocess.Popen:

subprocess.Popen(cmd.exe c "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" -c "import sys; sys.path.insert(1, r'\\...\my_script_directory');import my_script; my_script.my_function('param 1', 'param 2')")

then I get the error: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

If I add additional quotations like so:

subprocess.Popen(cmd.exe /c """"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"""" -c """"import sys; sys.path.insert(1, r'\\...\my_script_directory');import my_script; my_script.my_function('param 1', 'param 2')"""")

then I get the error: The system cannot find the path specified.

I'm currently at a place where I can run Python code that has no spaces if I omit the quotations entirely

subprocess.Popen(cmd.exe /c "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" -c print(1+1))

which successfully returns 2

Does subprocess.Popen and/or cmd.exe handle quotations for commands to an executable differently? I've tried multiple escape characters for spaces as well, but can't get it to work.

Share Improve this question edited Feb 4 at 3:34 fume_hood_geologist asked Feb 3 at 22:18 fume_hood_geologistfume_hood_geologist 1251 gold badge1 silver badge8 bronze badges 6
  • Use triple quotes only around the outer string -- those are Python syntax. Leave the quotes that are shell syntax exactly as they are in the shell. And use a raw string. – Charles Duffy Commented Feb 3 at 22:59
  • Just pass the command as a list of strings. – mkrieger1 Commented Feb 3 at 22:59
  • @mkrieger1, ...I'd always/unconditionally recommend that on UNIX (or any other sane OS), but Windows is in this regard not sane -- insofar as it passes programs a string rather than an argv array and lets them override the default libc-provided parsing of that string. Granted, since the program being run is Python itself, we know it's not doing anything too evil with that capability. – Charles Duffy Commented Feb 3 at 23:00
  • @CharlesDuffy Is Windows sane in any respect? – Adon Bilivit Commented Feb 4 at 7:56
  • If you really have to invoke cmd.exe directly, the first thing to do is start with a correct base command. The entire /C argument should be doublequoted, and any additional nested doublequotes remain unchanged, (cmd.exe removes the opening and closing doublequotes and preserves everything in between). That would be leave you with cmd.exe /c ""python.exe" -c "YourPythonCommandArgHere"". – Compo Commented Feb 4 at 12:20
 |  Show 1 more comment

1 Answer 1

Reset to default 1

The right way to do this is to pass an argv array instead of a string. On UNIXy operating systems that means no quoting and escaping is necessary at all, since the native way arguments are passed is as an array of strings (an "argument vector", hence sys.argv, and the conventional argv argument to main). On Windows, quoting is still necessary, but Python will generate quoting that works correctly for any program that doesn't override libc's default argument parsing.

python_exe = r'C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe'
script = r'''
import sys
sys.path.insert(1, r'\\...\my_script_directory')

import my_script
my_script.my_function('param 1', 'param 2')
'''

subprocess.run([python_exe, '-c', script])
转载请注明原文地址:http://www.anycun.com/QandA/1744747272a87039.html