No module named X (Python in C++)

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

#ifdef _DEBUG
    #undef _DEBUG
    #include <Python.h>
    #define _DEBUG
    #include <Python.h>

int main() {

import gdb # ImportError: No module named gdb
print("Hello, World!")

    return 0;

The question is generalized. So, it’s not just about GDB but rather a general inquiry about how to resolve the error “No module named X” when using Python in C++.

I’m uncertain about the most appropriate forum for my question (Python forum or C++ forum?). I consider C++ to require the highest level of skill among all programming languages, which is why I decided to post this thread here instead of in the Python forum.

Have you tried importing things that are built-in? Like import this?

Maybe try it with a more common library like numpy. See if it gets linked right.

I tried to import the ‘numpy’ module, but encountered the same error. Interestingly, in other software (not my project), importing ‘numpy’ works, when I type ‘numpy’ then its path shows up, it prompts me to consider whether the same approach could work for ‘gdb’. However, I’m uncertain about how to configure the path for ‘gdb’ within a Python in C++ environment. Could you tell me how to set up this path effectively?

├── 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
PyObject *sysPath; // 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");
    // ...
    // 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!")

Try this if you haven’t already seen it. It’s not as straightforward as plugging in something like numpy since it’s not a normal python library. Also it seems like MinGW and Cygwin will yield different results. I have not done a ton of embedded python on windows. This seems interesting.

1 Like

Thank you, it’s the answer to the right direction.

A quote from your link:

import gdb only works when your Python code is running within the GDB process. It’s not supposed to work from the regular system Python interpreter.

This is another link I found yesterday:
Extending GDB with Python - Lisa Roach

The GDB module is only available to you in that C Python interpreter in GDB, …, you cannot ‘pip install gdb’, …

The difficulty with programming is sometimes I go in the wrong direction without realizing there’s another good way until a problem happens, sometimes it’s better to have good knowledge.

Yea that’s the process. It’s not so much a difficulty as a normal part of the process (discovery). The nice thing is that along the way you might run into other things which either might be useful or interesting.


I’ve found valuable insights by exploring code beyond my own. This exploration has led me to discover how to integrate Python with C++. In the future, when I implement AI, Python will be required due to its significance in AI development.