Debugging from PyCharm to Visual Studio C++ code - python

I'm currently trying to debug issues in Caffe for Windows PyCaffe.
Because of a bug in Python Tools for Visual Studio, PTVS doesn't work so I'm using PyCharm and trying to attach to PyCaffe's process through Visual Studio 2013. That is, I'm running PyCharm debugger on a Python script with a breakpoint set at the point where I call the Python entry point into PyCaffe.
I debug the Python script in PyCharm which calls modules written in C++ in VS. I want to debug those modules in C++. So I'm trying to attach to the PyCharm or Python processes with breakpoints set in VS.
The problem is that the breakpoint isn't firing at the entry point in PyCaffe in the Visual Studio C++ code.
Has anyone successfully gotten this kind of thing to work or is there an alternative way of doing this?

I faced a similar problem a few years ago, trying to
debug a user-mode driver wish was loading automatically from a RPC, the solution I found was:
Download Debugging Tool for Windows (depending on your system x86 or x64), wish is free,from the Microsoft Download.
Add "__asm int 3"(this is asm instruction for a hardware breakpoint) in the place on the C code where you whant to the break point to take effect.
Compile the C program, nomally with debug info (this part is important,because help the Debugger to find the source file), this should not make any problems.
Attach the Debugger to the running process (in my case was the explorer process), in your case should be the process that make first call to the library, for example if PyCharm create a process for the new python code, the debugger need to be attached to that process(you can manage this by setting a breakpoint in python, wish will give you time for doing the attach process), the easy way for me to figure this out was to let the process crash, because "__asm int 3" cause a process crash if a debugger is not attach, you can take advantage of this waiting for the crash and when windows shows you the screen "Process ... has detected a problem an need to be closed", you know who is the process that you are looking for.
This could be painful the first time but for me quite effective, because you
can see the data flow from one program to another,

We attach to one process and allow to set breakpoints within code that has not started from the VS debugger. But one important issue is that we often debug/run the app in the VS, for example, we debug the Web code that runs under IIS, we will attach to the IIS process or others.
Your project is different from the above sample, you run/debug your app in Pycharm (not the VS), but you want use the VS Attach to process function, so it would have a limitation. As you said that you debug script in PyCharm, and want to call C++, so you would check that whether the PyCharm supports a similar feature like the attach tool in VS.

Related

Debugging Python and C++ in Visual Studio

I want to be able to step through C++ code that is part of a dll used by a Python script.
I have looked at https://learn.microsoft.com/en-us/visualstudio/python/debugging-mixed-mode-c-cpp-python-in-visual-studio?view=vs-2019 and I think I've implemented this correctly. However when ever I enable Enable Native code debugging the breakpoints in Python are no longer functioning and it states that no symbols are found in code.
I went to the Visual Studio Python installation and have selected Python native development tools
And the Python symbols have been installed and are present in the active environment:
On the debug tab I have tried setting the interpreter path to python_d.exe and in Debug>Options on the Symbols tab I've selected the associated symbols:
I have tried selecting multiple options in the image above with no success.
I suspect it is something simple that I'm doing wrong, any ideas? Thanks!
I faced the same problem and didn't find a solution.
If you want a workaround, try to attach your native code to the python.exe process:
Start the debugging of your python script (without native support) and wait on some breakpoint
Launch your native project in another VisualStudio instance, set needed breakpoint and then go to Debug -> Attach to process, select started python.exe process (you can find it by PID)
Continue python execution.
It worked for me. Note that you need your native code to be compiled with debug symbols as well.
Happy bug-hunting! =)

Loading code from editor into PyCharm terminal

I've just started using PyCharm, and am consistently pleasantly surprised by its tools and configurability. I know that in PyCharm, you can load code from the editor into a Python console (see https://www.jetbrains.com/pycharm/webhelp/loading-code-from-editor-into-console.html). However, the remote console I'm using in PyCharm is very slow.
If I access python on the remote machine directly via ssh, it has no performance issues. As such, I'd like to just open the remote version of python via PyCharm's Terminal, and execute code in the terminal from my editor.
However, I haven't found any key bindings (or options) that let me automatically load code -- it's a manual copy and past process for now. When I try to create a keyboard shortcut to do this, the option isn't available. Is there a method to create a keyboard shortcut to load code from the editor into the terminal?
The only way to create such a shortcut by yourself is by writing a plugin to PyCharm. PyCharm plugins are written in Java, so I don't know whether this sounds like an appealing option for you. Alternatively, you can file a feature request at http://youtrack.jetbrains.com/ asking for this feature to be added.
Note that you can set up a run configuration that will run your code for you on a remote interpreter without going through a console. Have you tried this? Is it also slow?

How to debug a Windows DLL used inside Python?

On Windows7 I have a python script that uses Windows DLLs, using the .NET Common Language Runtime (CLR). An error occurs inside one of the used DLL, but the standard Python debugger only debugs on the Python code level (and not the DLL).
How can I debug what is going on inside the DLL(s)?
If you have Microsoft Visual Studio available,
1) open the Visual Studio project that your DLL is part of (or create a new project).
2) If you have set up your DLL for debugging (you've built it with debugging info, and it will be the one that your python program will use), you can set breakpoints in the DLL code.
3) Start the program you want to debug as you would do normally.
4) Go back to the Visual Studio IDE and go to the Debug menu. Choose the Attach to Process option. You will then get a list of all the running processes.
5) Choose the process you want to debug, which will be your python program, or runtime that is running your program.
6) Sit back and wait for one of your breakpoints to be hit, or you can try a Break All from the Debug menu to temporarily halt the program.
This is a general way of starting out debugging not only python programs, but any program where you need to debug a DLL that is being used by the program.
Note that the above advice works best if you have built the DLL yourself with debugging information and is being utilized by your python application. If it is a third-party DLL where you have no source code, you can still debug from Visual Studio, but will need to know assembly language (since the source code is usually not available).
These 3 debugging scenarios with pythonnet are based on PTVS docs:
https://github.com/pythonnet/pythonnet/wiki/Various-debugging-scenarios-of-embedded-CPython

Console windows closes right after I hit ctrl+F5 in visual studio tools for python

I have installed Python Tools for Visual Studio and I cannot see the output in the console window like I see output when I run a C# console application within visual studio upon hitting the following Shortcut keys.
F5 -- Starts debugging the program and closes the console window in c# and in Python
Ctrl+F5 -- Start without debugging, works as expected in C# but not in Python program. It always exits the console window under both the options.
There is a third option "Execute Project in Python Interactive" for a Python Application. This option sometimes produces output and sometimes does not.
What should I do to see the output in the console window and the windows should not close after running the program? Is this even possible or some conceptual difference for python console apps?
Am I doing something wrong here?
There's an option in Tools->Options->Python Tools->Advanced under the Debugging section for "Wait for input when process exits abnormally" and "Wait for input when the process exits normally". I thought they were supposed to be on by default but enabling them should cause it to wait.
for future visitors from google. This doesn't involve Python settings, but to prevent the console window from closing automatically while using "Start without debugging" (Ctrl + F5). Try looking for this setting.
Project -> "project name" Properties -> Configuration Properties -> Linker -> System
Then look for the SubSystem property, and set it to
Console (/SUBSYSTEM:CONSOLE)
You can select it via drop down or try and type it.
if you have trouble seeing it under System you can also check under All Options
Hit save and then it should work immediately. Confirmed this works on VisualStudio 2013, but I also think it works on other versions too.
UPDATE: Visual Studio 2017 - the items listed have moved :-( and I don't know where they moved to

How can I debug Python code without running a script (using Eclipse)?

I've noticed how easy it is to debug a Python script from Eclipse. Simply set breakpoints and run a Python script from the debug menu. But is it possible to start a Python Interactive Interpreter instead of running a particular Python script, whilst still having Eclipse breaking on breakpoints? This would make it so much easier to test functions.
Thanks for any help
Still looking for a simple/ish way to start the debugger in Eclipse->PyDev that lets me use the interactive debugger. None of the answers as of yet is acceptable
You can explicitly write code to setup a breakpoint in your script, and then "remote debug". That means having pydevd in the pythonpath of your script wherever it is running, and running the eclipse pydev remote debugger on your devbox. If it's all happening on the same machine, this is fairly straightforward.
If not, you'll need to specify the hostname of the dev machine running the python remote debugger in the call to settrace(). You'll also need pydevd available on the machine running the script.
I have got this working in the past without having to install eclipse+pydevd on the machine running the script. It's not totally straightforward, and if you go that route I'd recommend checking that the pydevd versions match or at least you know they're compatible. Otherwise you end up wasting time debugging the debugger.
For details see: Pydev Remote Debugger
what about this, in the script, you can write a function, say onlyForTest, then each time you write a new function and want to test it, you can just put it in the onlyForTest function, then specify some arguments required by the new function you just write, then open the interactive python shell, import the script, call the onlyForTest function, check out the result.

Categories