Python Forum
Resolving ImportError: No module named gdb (Python in C++)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resolving ImportError: No module named gdb (Python in C++)
#1
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;
}
Reply
#2
see: about GDB
Reply
#3
@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++.
Reply
#4
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!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 894 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Problem with pymodbus - ModuleNotFoundError: No module named 'pymodbus.client.sync' stsxbel 2 23,817 Nov-02-2023, 08:20 AM
Last Post: South_east
  ModuleNotFoundError: No module named 'requests' Serg 18 2,603 Oct-29-2023, 11:33 PM
Last Post: Serg
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 1,523 Aug-07-2023, 05:32 AM
Last Post: DPaul
  ModuleNotFoundError: No module named 'eyed3' Wimpy_Wellington 2 1,348 Jul-10-2023, 03:37 AM
Last Post: Wimpy_Wellington
  How to fix this error: ModuleNotFoundError: No module named 'notears' yaoyao22 2 1,047 Jul-09-2023, 11:24 AM
Last Post: yaoyao22
  Help with pyinstaller "No module named" korenron 9 9,139 Jun-15-2023, 12:20 PM
Last Post: snippsat
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 1,019 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  ModuleNotFoundError: No module named 'omsdk.sdkproto' donvito7 4 1,869 Oct-20-2022, 02:56 PM
Last Post: deanhystad
  ModuleNotFoundError: No module named '_struct' when starting pip3 yuhuihu 0 2,883 May-05-2022, 04:41 AM
Last Post: yuhuihu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020