[Python] Using subprocess to open a cmd to run a Python function but having issues with...

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    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("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(""""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("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.

    Continue reading...

Compartilhe esta Página