-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vscode debug embedded Python #1821
Comments
Option 1 is likely the only way to get this to work. Remote debugging essentially. The key part for breakpoints is the path mapping. See this wiki page for a similar example: |
Thanks for the tips! I tried all sorts of path mappings here and nothing worked. I've obfuscated my paths to MyPath Log files attached: Here's my launch
Here's my test code
|
Those path mappings look wrong to me:
The embedded python is a windows machine? Or maybe I'm misunderstanding your situation entirely. I thought you were remoting to something like a rasberry pi. Can you describe the application more? It's also weird that you'd need to stick debugpy in the app. Why not start your app with debugpy itself? |
I'm running Siemens NX which has an embedded python that I want to debug. This is windows. When I start the app, I need to set a bunch of environment variables for it to pick up my python code. I'm open to any suggestions! Thanks |
And Siemens NX is a C++ application? Does the python code pause at any point? You should be able to just use the normal attach functionality in VS code without having to import debugpy into the app, but you need to have it sit there and wait. |
Here's the instructions from NX (yes, c++, tons of dlls) on how to integrate with pydevd in eclipse
|
You could do something like this: import sys
import time
def wait_for_debugpy():
while "pydevd" not in sys.modules:
time.sleep(1)
wait_for_debugpy() Then just do an attach from VS code using the PID of the Siemens NX process. (well assuming it loads python through the dll interface). If it starts another process, then you'd have to use the PID of that process. There should be no need to inject pydevd or debugpy into the process yourself. The attach will do that for you. |
Actually, that didn't work for me, it was too fast. Debugpy needs time to handle the initialize request. The script exited as soon as I attached. This worked better: import sys
import time
import os
print(os.getpid())
def wait_for_debugpy():
while "debugpy" not in sys.modules:
time.sleep(1)
time.sleep(2) # Give time to debugpy to initialize
wait_for_debugpy()
print('after debugpy attach') # breakpoint here Here's the launch.json I used: {
"name": "Python Debugger: Attach to Process",
"type": "debugpy",
"request": "attach",
"processId": "${command:pickProcess}",
}, |
Another alternative (if you have it) is to use Visual Studio to debug python code. It supports mixed mode debugging. It would let you debug the python code and the Siemens NX code at the same time. Current version only supports 3.9 though (next preview version will support up to 3.13) |
Very, Cool, I'm getting there thanks to your help! I can now attach by calling debugpy.breakpoint(). I'm trying to make it repeatable now... Something for Monday! Thank you! |
I'm now able to attach and I get print outs. I still cannot hit breakpoints. When justmycode is false, I get this error When justmycode is true, I get prints but no breakpoints... I cannot step through the code b/c of the file not found error.. I have no idea where the system is grabbing C:\CAD from.... Thanks
|
It might be how threading.py is being loaded, but yes it looks like it's using the current directory. The pydevd.log is the one that should have more information. It will show why it can't bind the breakpoints. |
The file not found error is also a red herring. It's because you have justMyCode = false. It means the debugger stops all on exceptions, not just the ones in your code. Your root problem is the debugger not being able to bind breakpoints. The pydevd log should say what mapping it tried. It might also be it's not waiting long enough. What does your actual code you're running look like? |
Does this matter?
I've attached my code and logs
debugger.vscode_b6c9183c-4c93-464f-848d-96ee3595f347.log |
This does matter:
The original text for this error referenced this bug here: http://bugs.python.org/issue1666807 What version of python are you using? |
Python 3.10.9 |
You might try a newer version. That bug I referenced was fixed in 3.11 I think. 3.13 would be a better bet to make sure it works. |
Thanks! I think im stuck at 3.10 due to the cad system prebuilt python binaries. I also use this to sidecar in an adjacent folder https://github.com/jtmoon79/PythonEmbed4Win Basically , I extract the python zip and update sitecustomize. Webdbg works but I'd still rather be in vscode. Can you think of anything I can do on my end with 3.10? Possibly turning frozen modules off within my environment? |
I don't know for sure that 3.10 is the root cause of the problem. It might also depend upon how the CAD system is launching the python code. The pydevd.log should tell us for sure if the path is the root cause of the problem. On my machine that ends up being a file called something like:
|
I"ll grab these tomorrow |
OK, I replaced all threading, ntpath and a few others with the non frozen (.py files) of those libs and the frozen import path errors are gone. I still cannot set breakpoints. I cannot find the debugpy.pydevd , maybe b/c I'm logging from launch.json?
Here's the output from the debug console
Here are my logs: Thanks for all of your assistance! clean_debugger.vscode_29161ac0-85fc-498a-89b1-5f7de9e7cc1e.log |
Pydevd log should be in the same spot as the other logs. |
Searched my entire c:\ -- -file doesnt exist when launching from vscode. It seems that I have to turn it off in launch.json and turn it on in the python code importing debugpy to get that file.. I will try |
This code is supposed to setup the pydevd log. Maybe something is messing it up. # If debugpy logging is enabled, enable it for pydevd as well
if "DEBUGPY_LOG_DIR" in os.environ:
os.environ[str("PYDEVD_DEBUG")] = str("True")
os.environ[str("PYDEVD_DEBUG_FILE")] = os.environ["DEBUGPY_LOG_DIR"] + str("/debugpy.pydevd.log")
|
Got those logs working with DEBUGPY_LOG_DIR debugpy.pydevd.51804.log odd things about these logs --- I'm embedded so debugpy.pydev tells me about roots I shouldnt see. I should not see anything AppData, There are no libs in C:\CAD\Lib either...
debugpy.server is also telling me wrong info now....
Whereas the other logs from the launch json way to log did not yield those libaries Here are the logs from the unfrozen version of the code: debugpy.pydevd.38568.log Thanks |
The log makes it look like the breakpoints did bind. But the trace function isn't being called. I can only hazard a guess that the Siemens NX is actually disabling tracing or is setting its own tracing. It looks like we never get called for events. Your only recourse may be to try mixed mode debugging in Visual Studio. When Visual Studio 17.13 ships, it should support mixed mode debugging for 3.10. |
Thanks Rich! Will keep an eye out |
Hi @rchiodo --- I made it a bit further with the following changes. I can now stop at a breakpoint, print a variable but the file doesnt load due to the debgugger thinking the current file is If I can get around this and find the "right" file, we are good to go! Here's what I did.
D+00026.593: /handling #64 event "stopped" from Server[1]/ D+00026.625: Client[1] --> { D+00026.625: /handling #14 request "threads" from Client[1]/ D+00026.625: Server[1] --> { D+00026.625: /handling #14 request "threads" from Client[1]/ D+00026.625: Client[1] --> { D+00026.625: /handling #15 request "stackTrace" from Client[1]/ D+00026.625: Server[1] --> { D+00026.625: Server[1] --> { D+00026.625: /handling #15 request "stackTrace" from Client[1]/ D+00026.625: /handling #70 event "module" from Server[1]/ D+00026.656: Client[1] --> { D+00026.656: /handling #16 request "source" from Client[1]/ D+00026.656: Server[1] --> { E+00026.687: /handling #16 request "source" from Client[1]/ D+00026.687: /handling #16 request "source" from Client[1]/
0.00s - Set tracing of frame: - 0.00s - Set tracing of frame: - ...... 0.00s - PyDB.do_wait_suspend 0.00s - Process SetBreakpointsRequest: {
|
If the Siemen's app is running the script as a string, that would certainly make setting breakpoints ahead of time impossible. You'd have to do a manual breakpoint inside the code and then step through the 'source' that debugpy provides to VS code. |
Gotcha! Appreciate it... That explains why webpb works and this does not! How do I provide that code to pydevd on the vscode side? Thanks |
Got it!!!!! Thank you. NX runs the first bit as a string but it cannot avoid importing my imports which get set from the run. I can use pydevd settrace!!!!!!! Thanks again for all of your help! Joe |
Hi
I've read about this issue in multiple places, and it seems that all of the threads on it are pretty old. I would like some general guidance on how to go about the bugging and embedded python install in a C++ program.
I am able to use web_dbg without any issues. However, I would much rather use VS code.
I have tried to debug the program using 2 methods:
I am at a complete loss I would really want to use VS code, but it seems I am out of luck and have to drop back to web debug. I'm not quite sure where to find all the different log files so any guidance would be helpful.
Thanks
Joe
The text was updated successfully, but these errors were encountered: