我在C++代码中嵌入Python 3.82.(使用VisualStudio 2019)。Python安装了pandasn(通过pip)
我设法从C++程序中导入熊猫,但是,当我尝试再次导入它时,它崩溃了。p>
#include <Python.h>
#include <iostream>
int main( int argc, char* argv[] )
{
{
Py_SetPythonHome( L"C:\\Python38" );
// Initialize the Python Interpreter
Py_Initialize();
std::cout << "Importing pandas..." << std::endl;
if ( PyRun_SimpleString( "import pandas" ) == 0 )
std::cout << "SUCCESS" << std::endl;
else
std::cout << "FAIL" << std::endl;
Py_Finalize();
}
{
Py_SetPythonHome( L"C:\\Python38" );
// Initialize the Python Interpreter
Py_Initialize();
std::cout << "Importing pandas..." << std::endl;
if ( PyRun_SimpleString( "import pandas" ) == 0 )
std::cout << "SUCCESS" << std::endl;
else
std::cout << "FAIL" << std::endl;
Py_Finalize();
}
return 0;
}
这会导致一个例外:
_multiarray_umath.cp38-win_amd64.pyd!00007ffbd5b8ca69() Inconnu
_multiarray_umath.cp38-win_amd64.pyd!00007ffbd5b8ffd6() Inconnu
_multiarray_umath.cp38-win_amd64.pyd!00007ffbd5b9d34d() Inconnu
python38.dll!00007ffbd22f6131() Inconnu
python38.dll!00007ffbd22f6092() Inconnu
输出为:
Importing pandas...
SUCCESS
Importing pandas...
Traceback (most recent call last):
File "<string>", line 1, in <module>
是否有任何初始化/取消初始化步骤我错过了,可能会使这个失败,而它可以工作
注意,我不能作为pandascannot be loaded in Debug build进行调试
根据OP的要求,我制作了一个小演示,介绍如何在应用程序中包装用户Python脚本,以防止用户脚本的全局变量意外持久化:
输出:
但是,我不确定这是否足以解决导入的Pandas库的OPs问题
在我们的应用程序中(我们使用了上述技巧),我们在
Py_Initialize()
之后导入选定的库一次。 (我大致记得,这是我们最后一次不顾一切地解决类似问题,如OP观察到的问题。)相关问题 更多 >
编程相关推荐