如何在调试模式下运行带有嵌入式Python的C++应用程序

问题描述 投票:0回答:1

我正在尝试在调试模式下运行下面的脚本(发布工作正常)。我已经链接了 python310_d.lib 和 python310_d.dll,它们是从此处找到的源代码构建的 https://github.com/python/cpython/tree/3.10 并手动移至 C:\Users izzo\AppData\Local\Programs\Python\Python310 和 \libs 文件夹。然后,我使用虚拟环境中的 python.exe,因此它应该自动检测库。

#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;
}

这是我遇到的错误:

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 . . .

有什么想法吗?谢谢!

python c++ python-c-api
1个回答
0
投票

出现您所看到的错误是因为 Python(您正在使用的)的调试版本与 NumPy 等第三方库的预构建版本不兼容。 Python 的调试版本使用不同的运行时符号(例如 python310_d.dll),并且大多数预构建的 Python 包(包括 NumPy)都是针对 Python 的发布版本构建的。

我建议你,

  • 克隆 NumPy 存储库:git clone https://github.com/numpy/numpy.git
  • 导航到 NumPy 目录并运行 python_d setup.py build
  • 然后,使用 python_d setup.py install 安装构建的包
© www.soinside.com 2019 - 2024. All rights reserved.