PTVS
PTVS copied to clipboard
Can't debug Python in my application
This issue has been moved from a ticket on Developer Community.
My application is built in C++. And it runs a lot of Python codes. Previously I just needed to attach to the running process to start to debug Python. Since a couple of days ago, I can’t debug Python any more.
In the output window there are some warnings and errors printed. I past them here:
1> WARNING: No handler registered for event of type ‘debugpyWaitingForServer’!
1> WARNING: Request ‘attach-2’ has not received a response within 1000 ms!
1> [DebugAdapter] <-- R (attach-2) [15031 ms]: {“type”:“response”,“request_seq”:2,“success”:false,“command”:“attach”,“message”:“Timed out waiting for debug server to connect.”,“seq”:5}
1> ERROR: Unexpected error
I have no idea what these messages mean and how to fix them. Anyone can help me? Thanks!
Original Comments
(no comments)
Original Solutions
(no solutions)
New found: VS 2019 works. VS 2022 doesn't work.
Are you using Debug -> Attach to Process, and then selecting the process from the list?
If so, does it make any difference if you instead use debugpy.listen()
in your app, and attach to the socket?
Yes, I attach to process and start to debug.
I don't know what is debugypy. Is it a python module? How can I call the listen()
?
When I use VS2019, there is a warning printed out:
PYDEV DEBUGGER WARNING: sys.settrace() should not be used when the debugger is being used. This may cause the debugger to stop working correctly. If this is needed, please check: http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html to see how to restore the debug tracing back correctly. Call Location: File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\ptvsd\debugger.py", line 2111, in new_thread_wrapper sys.settrace(cur_thread.trace_func)
I tried listen()
API. It didn't work.
My codes are like this:
import debugpy
debugpy.listen("127.0.0.1")
debugpy.wait_for_client()
After I attached VS2022, a lot of errors were printed. Some of them are like this:
File "c:\program files\microsoft visual studio\2022\preview\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 72, in attach
log.info("Configuring injected debugpy: {0}", json.repr(setup))
AttributeError: module 'debugpy.common.json' has no attribute 'repr'
AttributeError: module 'debugpy.common.json' has no attribute 'repr'
E+00025.640: Traceback (most recent call last):
File "c:\program files\microsoft visual
studio\2022\preview\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 72, in attach
log.info("Configuring injected debugpy: {0}", json.repr(setup))
AttributeError: module 'debugpy.common.json' has no attribute 'repr'
Stack where logged:
File "<string>", line 1, in <module>
File "c:\program files\microsoft visual studio\2022\preview\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 90, in attach
log.reraise_exception()
File "D:\WorkSpaceSsd\animation-authoring\_build\windows-x86_64\release\data\Kit\animation\0.0\pip3-envs\default\debugpy\common\log.py", line 226, in reraise_exception
_exception(format_string, *args, **kwargs)
E+00025.640: Traceback (most recent call last):
File "c:\program files\microsoft visual studio\2022\preview\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 72, in attach
log.info("Configuring injected debugpy: {0}", json.repr(setup))
AttributeError: module 'debugpy.common.json' has no attribute 'repr'
Stack where logged:
File "<string>", line 1, in <module>
File "c:\program files\microsoft visual studio\2022\preview\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 90, in attach
log.reraise_exception()
File "D:\WorkSpaceSsd\animation-authoring\_build\windows-x86_64\release\data\Kit\animation\0.0\pip3-envs\default\debugpy\common\log.py", line 226, in reraise_exception
_exception(format_string, *args, **kwargs)
Yeah, python debugging in visual studio 2022 is broken it appears.
VS2019 doesn’t work either now. I can only use VS Code to debug.
Is this resolved in a preview version of visual studio? Its a blocker for me right now.
@niexuchina does python / native c++ mixed mode debugging work in vs code?
As the OP has noted, this method of Visual Studio 2022 debugging of embedded Python seems to be broken. Note the method discussed here does not require changing the source code or attaching to a remote port. That method still works
You just attach to the application from the debugger and set a break point in the python. It seems VS tries to inject code
[2023-01-17 13:32:19]: [warning] File "c:\program files\microsoft visual studio\2022\professional\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 72, in attach log.info("Configuring injected debugpy: {0}", json.repr(setup)) [2023-01-17 13:32:19]: [warning] AttributeError: module 'debugpy.common.json' has no attribute 'repr' [2023-01-17 13:32:25]: [warning] E+00086.359: Traceback (most recent call last): File "c:\program files\microsoft visual studio\2022\professional\common7\ide\extensions\microsoft\python\core\debugpy\adapter/../..\debugpy/..\debugpy\server\attach_pid_injected.py", line 72, in attach log.info("Configuring injected debugpy: {0}", json.repr(setup)) AttributeError: module 'debugpy.common.json' has no attribute 'repr'
If you're using debugpy.listen()
or its CLI equivalent, you need to attach using remote debugging as described e.g. https://learn.microsoft.com/en-us/visualstudio/python/debugging-python-code-on-remote-linux-machines?view=vs-2022 - just connect to localhost.
If you're attaching to a local process, the debugger will indeed try to inject itself into it, and this can conflict with another version that is already running in the process. Thus, you shouldn't be importing debugpy
in that case - just attach, and it should work. Can you clarify if the error still happens with that change?
The mixed-mode native/Python debugger in VS is completely separate from debugpy, and only kicks in if you attach to a process while manually selecting the code types to be "Native" and "Python (native)" at the same time. It is not supported in VSCode.
OK, thanks for clarifying that code-injection is happening. I am attaching to a local process and I am not importing debugpy. Yes, I know it should work, but it doesn't. I have seen it work in the past in another application, so I am sort of scratching my head right now.
This is the bit that really throws me off:
AttributeError: module 'debugpy.common.json' has no attribute 'repr'
That's just a global function in debugpy/common/json.py, not conditionally defined or anything. The only way it wouldn't be there is if the module is still being imported - but that can't be since the import
statement has already returned by that point - or if something is deleting it afterwards.
I wonder if perhaps it ends up using a different debugpy
version, somehow? The attach code mucks around with sys.path
to prevent that from happening, but that's the only thing I can think of. Can you try tweaking your local copy of debugpy/server/attach_pid.py
to log debugpy.__file__
somewhere after it imports it?
Hmm, I tried going back to old-school debugging method using remote Python and now this ... I verified the debugpy package in the environment was exactly 1.6.3 ( it's visible in the solution), so I am now at a loss....
I think it's complaining about the version installed in whatever environment that's active in the Python Environments window (which wouldn't necessarily be the same as the one you're using to launch your app, if you're doing it directly and then attaching to it).
This issue was closed because it has been stalled for 30 days with no activity. If the issue still persists, please reopen with the information requested. Thanks.