Python Forum

Full Version: Resolving ImportError: No module named gdb (Python in C++)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following serves as the foundation of my code; however, I encounter an error: "ImportError: No module named gdb". How can I resolve this issue? Google Bard AI has suggested using the command pip install gdb in the terminal, but I would like the GDB Python API to be installed permanently within my project. I want it to reside in the project's third-party or binary folder, similar to how the python312.dll can be placed in the binary folder. This way, it remains unaffected even if I reinstall the operating system. I'm building a game engine, and I want to make it easy for users to get started without requiring them to install the GDB Python API. I want to replicate the experience of using VSCode, where users can install GCC without needing to install GDB Python API.

Python version: 3.12.0

// https://stackoverflow.com/questions/17028576/using-python-3-3-in-c-python33-d-lib-not-found
#ifdef _DEBUG
    #undef _DEBUG
    #include <Python.h>
    #define _DEBUG
#else
    #include <Python.h>
#endif

int main() {
    Py_Initialize();

    PyRun_SimpleString(std::string(R"_(
import gdb # ImportError: No module named gdb
print("Hello, World!")
    )_").substr(1).c_str());

    Py_Finalize();
    return 0;
}
@Larz60+, I know how to use GDB with command line but I don't know how to use with Python. I forgot to generalize my question to make it easier to answer.

The question should be:

"How to fix ImportError: No module named X (Python in C++)". So, it's not only about GDB but a general question on how to fix that error with Python in C++.
mingw64\share\gdb
├── python/
│   └── gdb/
│       └── ...
├── syscalls/
├── system-gdbinit/
└── gdbinit
It's quite challenging. I finally know how to import but can anyone tell me what is the location of the right GDB Python API? Maybe I'm using the wrong path.

Note: Absence of errors does not mean success. For instance, calling PyImport_AddModule("abcde") may result to import abcde not having error, but the module remains empty.

// Referenced from https://stackoverflow.com/questions/35137145/python-c-api-pyimport-importmodule-fail-when-the-file-has-an-import-statement
PyObject *sysPath = nullptr; // TODO: Check for memory leak.

sysPath = PySys_GetObject("path");
PyList_Append(sysPath, PyUnicode_FromString("C:\\blender-3.6.2-windows-x64\\3.6\\python\\lib\\site-packages\\"));
PyObject *numpyModule = PyImport_ImportModule("numpy");
    // ...
    // IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
    // 
    // Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed.
    // ...

sysPath = PySys_GetObject("path");
PyList_Append(sysPath, PyUnicode_FromString("C:\\mingw64\\share"));
PyObject *gdbModule = PyImport_ImportModule("gdb");
    // (no error)
Python with PyRun_SimpleString():
import gdb
print(gdb.PYTHONDIR) # AttributeError: module 'gdb' has no attribute 'PYTHONDIR'
print(gdb.current_language()) # AttributeError: module 'gdb' has no attribute 'current_language'
print("Hello, World!")