Debugging of subprocess.popen has unexpected results and fails
Hi People,
Can any of you please try the following code block and tell me if you get the same problem or if I'm doing something stupid as this is completely stopping me from debugging my scripts and I'm back to old school debug prints everywhere in my code!!
I have logged a bug for this here:
http://youtrack.jetbrains.com/issue/PY-7464
The following code which runs perfectly at the command prompt and PyCharm can also run it without issues however if I try debug it with PyCharm all hell breaks loose and directories are created based on my parameters even if I just run something simple like 'echo HelloWorld'
Trying to debug the above even without breakpoints gives the following error:
============================================================
C:\Python32\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2.5.2\helpers\pydev\pydevd.py" –multiproc –client 127.0.0.1 –port 50488 –file D:/Scripts/test/Systray/Build/BuildBinProduct/test.py
pydev debugger: process 6832 is connecting
Connected to pydev debugger (build 117.663)
1
The system cannot find the path specified.
Error occurred while processing: .exe /c ECHO.
A subdirectory or file HELLOWORLD already exists.
Error occurred while processing: HELLOWORLD.
Process finished with exit code 1
============================================================
Any ideas as to what the problem is? I assume my code is not at fault because it works when you run it inside PyCharm or from the command line with expected results.
My system setup is as follows:
PyCharm 2.5.2
Python 3.2.2 (also tested with 3.0.1) from http://www.python.org/
Windows Vista x64
Can any of you please try the following code block and tell me if you get the same problem or if I'm doing something stupid as this is completely stopping me from debugging my scripts and I'm back to old school debug prints everywhere in my code!!
I have logged a bug for this here:
http://youtrack.jetbrains.com/issue/PY-7464
The following code which runs perfectly at the command prompt and PyCharm can also run it without issues however if I try debug it with PyCharm all hell breaks loose and directories are created based on my parameters even if I just run something simple like 'echo HelloWorld'
Code
#!/usr/bin/python3
import os
import subprocess
import sys
# Executes a command given an optional stdin byte string and returns a return code, stdout and stderr from the application
# Example:
# p4filename = "\"" + os.path.join(Variable.getvarvalue("P4PATH"), Variable.getvarvalue("P4FILE")) + "\""
# (retCode, stdout, stderr) = ExecuteCommand(p4filename + " set", None, mystring.encode('utf-8'))
def ExecuteCommand(cmd, cwd = None, stdin_bytes = None):
if stdin_bytes != None and type(stdin_bytes) != bytes:
print("ExecuteCommand stdin_bytes must be of type bytes.\n\tIf you have passed a string use your string.encode('UTF-8') to convert the string to a byte array of UFT-8 characters.")
print("Type passed - %s" % type(stdin_bytes))
sys.exit(1)
stdout = ""
stderr = ""
if cwd is not None:
cwd = os.path.realpath(cwd)
if not os.path.exists(cwd):
print( "***ERROR*** - Current working directory [%s] does not exist and therefore cannot execute command [%s]" % (cwd, cmd), {"status": "failed"})
sys.exit(1)
try:
process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd)
(stdout, stderr) = process.communicate(input=stdin_bytes)
return (process.returncode, stdout.decode("cp437"), stderr.decode("cp437"))
except OSError as ex:
return (ex.errno, "", ex.strerror)
(retcode, stdout, stderr) = ExecuteCommand("echo HelloWorld")
print(retcode)
print(stdout)
print(stderr)
sys.exit(retcode)
Trying to debug the above even without breakpoints gives the following error:
============================================================
C:\Python32\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2.5.2\helpers\pydev\pydevd.py" –multiproc –client 127.0.0.1 –port 50488 –file D:/Scripts/test/Systray/Build/BuildBinProduct/test.py
pydev debugger: process 6832 is connecting
Connected to pydev debugger (build 117.663)
1
The system cannot find the path specified.
Error occurred while processing: .exe /c ECHO.
A subdirectory or file HELLOWORLD already exists.
Error occurred while processing: HELLOWORLD.
Process finished with exit code 1
============================================================
Any ideas as to what the problem is? I assume my code is not at fault because it works when you run it inside PyCharm or from the command line with expected results.
My system setup is as follows:
PyCharm 2.5.2
Python 3.2.2 (also tested with 3.0.1) from http://www.python.org/
Windows Vista x64
Please sign in to leave a comment.
If this is working for others then there has to be something different with my setup or configuration but I have no idea what to look for. Does anyone have any other ideas?
Do you have any pycharm packages installed for python? I don't have any but have seen some that come with the linux version of PyCharm installer and wondered if this could be a problem?
I currently have the following packages installed but this test.py script does not use any of them.
distribute 0.6.24
p4python 2010.2.295040
pip 1.0.2
setuptools 0.6c11
wsgiref 0.1.2
Also I'm not using any virtual environments I'm directly using the Python 3.2.3 environment.
Any other things I can try?
http://youtrack.jetbrains.com/issue/PY-7464
In short the problem occurs if the MULTIPROCESS option in the .idea\workspace.xml file is set to true. You have to manually set the back to false in the file and then things start working again. You cannot do this from the IDE because there appears to be another bug where the IDE (run/debug configurations window->Check no other instances are running) can set this to true but then cannot set it back to false again.