Davide Rizzotti
Davide Rizzotti

Reputation: 31

How ro run a C++ application with embedded Python in Debug mode

I'm trying to run the script below in Debug mode (Release works fine). I've linked against python310_d.lib and python310_d.dll which have been built from the source code found here https://github.com/python/cpython/tree/3.10 and manually moved into C:\Users\rizzo\AppData\Local\Programs\Python\Python310 and \libs folder. I'm then using the python.exe from my virtual environment so it should automatically detect libraries.

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h> 
#include <locale>
#include <codecvt>

int main(){
    
    PyConfig config;
    PyConfig_InitIsolatedConfig(&config);

    auto venv_executable = L"C:\\Users\\rizzo\\Desktop\\TESI\\MMM_env\\Scripts\\python.exe";
    PyConfig_SetString(&config, &config.executable, venv_executable);

    auto status = Py_InitializeFromConfig(&config);
    PyConfig_Clear(&config);

    if (PyStatus_Exception(status)) {
        std::cout << "status.func: " << (status.func ? status.func : "N/A") << '\n';
        std::cout << "status.err_msg: " << (status.err_msg ? status.err_msg : "N/A") << '\n';
    }
    
    PyRun_SimpleString("import numpy; print(\"Hello World! :33333\")");
  
    return 0;
}

This is the error I'm getting:

Traceback (most recent call last):
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\_core\__init__.py", line 23, in <module>
    from . import multiarray
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\_core\multiarray.py", line 10, in <module>
    from . import overrides
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\_core\overrides.py", line 8, in <module>
    from numpy._core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy._core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\__init__.py", line 128, in <module>
    from numpy.__config__ import show as show_config
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\__config__.py", line 4, in <module>
    from numpy._core._multiarray_umath import (
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\_core\__init__.py", line 49, in <module>
    raise ImportError(msg)
ImportError:

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.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.10 from "C:\Users\rizzo\Desktop\TESI\MMM_env\Scripts\python.exe"
  * The NumPy version is: "2.1.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy._core._multiarray_umath'


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\rizzo\Desktop\TESI\MMM_env\lib\site-packages\numpy\__init__.py", line 133, in <module>
    raise ImportError(msg) from e
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.

C:\Users\rizzo\Desktop\TESI\dependencies_test\build\Debug\python_cpp.exe (process 15864) exited with code 0 (0x0).
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Any ideas?

Upvotes: 3

Views: 70

Answers (1)

Koushik Dutta
Koushik Dutta

Reputation: 23

The error you're seeing occurs because the debug build of Python (which you are using) is incompatible with pre-built versions of third-party libraries like NumPy. Debug builds of Python use different runtime symbols (e.g., python310_d.dll), and most pre-built Python packages, including NumPy, are built against release builds of Python.

I would suggest you to ,

  • Cloning the NumPy repository: git clone https://github.com/numpy/numpy.git
  • Navigating to the NumPy directory and running python_d setup.py build
  • Then, install the built package using python_d setup.py install

Upvotes: 2

Related Questions