#helping with debuging python module

79 messages · Page 1 of 1 (latest)

sudden isle
#

im trying to make a python module with cpp and pybind11, how should i debug my module when i execute the python file

light kilnBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

rigid rune
#

I didn't have to do anything special, just run the whole python process in your debugger

rigid rune
#

Which one do you normally use?

sudden isle
rigid rune
#

C++ one

sudden isle
#

the python source code is the executable

rigid rune
#

How do you use it normally?

#

In some kind of IDE?

#

Or in the terminal?

#

And which C++ debugger is it?

sudden isle
#

in vsc

rigid rune
#

So just set python as the executable in launch.json

#

And you can give it command line arguments to run your script/whatever

sudden isle
#

in the python file

#

and even in the cpp

rigid rune
#

I expect it to only work in .cpp files

#

Did you compile your python module with -g?

sudden isle
rigid rune
#

If it doesn't work, then probably not

#

Can you recompile it and try again? If this doesn't work, show the exact compilation command you used

sudden isle
#

i did recompile it and try again 6 or 7 time

rigid rune
#

What CMAKE_BUILD_TYPE do you use? Debug?

sudden isle
rigid rune
#

Ok. Delete the build directory and rebuild in verbose mode. Show the compilation commands

rigid rune
#

Do you know how to use cmake from the terminal?

rigid rune
#

Then instead of cmake --build ... do cmake --build ... --verbose

#

It should show the compilation commands

rigid rune
#

What python are you testing the module with?

sudden isle
rigid rune
#

I mean msys2 python or not?

sudden isle
rigid rune
#

Okay, that's good

#

Where do you place your breakpoint? Are you sure this function is getting called?

#

Try printing something from your module

sudden isle
#

i put a breakpoint in the constructure of a class in the python file

#

then put another breakpoint in somewhere in the constructure function

#

idk if i spelled constructure correct

#

btw what did you tell me to debug python with gdb

#

gdb python src.py?

rigid rune
#

Yeah, that should work

sudden isle
#

however, it dont work

rigid rune
sudden isle
#

i work

#

but i dont debug

rigid rune
#

Does it not work if you place breakpoint on the line that prints stuff?

#

To be clear, when you run it in a debugger, you see the text being printed, but the breakpoint on the exact same C++ line doesn't get triggered?

sudden isle
#

i dont stop on breakpoint

rigid rune
#

Alright

#

I've created this file, called 1.cpp:

#include <iostream>
#include <pybind11/pybind11.h>

PYBIND11_MODULE(example, m)
{
    m.def("foo", []
    {
        std::cout << "Hello!\n";
    });
}
#

Compiled with g++ 1.cpp `pkg-config --libs --cflags python3-embed` -Ipybind11/include -shared -o example.pyd -g in MSYS2 UCRT64

#

Then did gdb --args python3 -c 'import example; example.foo()'

b 1.cpp:8
y
r
#

And the breakpoint is being hit

#

Test the exact same thing, see if it's being hit for you or not

sudden isle
#

whatsgoingon

rigid rune
#

sudden isle
#

i have no idea what you say

rigid rune
#

I've tried debugging a simple module and it works for me

#

I've given you the exact steps on how to do that

#

Test if this process works for you or not

sudden isle
rigid rune
#

That shouldn't matter, continue with the steps and see if it works

sudden isle
#

i tried compile but it doesnt work

#

can i use cmake

rigid rune
#

Let's avoid cmake for now

#

Wdym by "doesn't work"? Show errors

sudden isle
rigid rune
#

Please read my last comment again