一、配置问题
1.python36.lib
将pragma comment(lib,“python38_d.lib”)改为pragma comment(lib,“python38.lib”),并且注释掉 # define Py_DEBU
也可复制粘贴重命名python36_d.lib
2.修改object.h和pyconfig.h为引入import_array函数
//object.h54行修改
/* Py_DEBUG implies Py_TRACE_REFS. */
#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
#define Py_TRACE_REFS
#endif
/* Py_DEBUG implies Py_TRACE_REFS. */
#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
//#define Py_TRACE_REFS
#endif
//pyconfig358行修改
#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
//#define Py_TRACE_REFS
#endif
#ifdef _DEBUG
//# define Py_DEBUG
#endif
//代码前添加此函数
size_t init_numpy()
{
//初始化 numpy 执行环境,主要是导入包,python2.7用void返回类型,python3.0以上用int返回类型
import_array();
}
二、项目代码
//启动脊柱勾边分割
QVector<QPointF> BllSegPoint::spineOutline()
{
QVector<QPointF> pathPointVector;
//Py_SetPythonHome(L"C:/Program Files/Python36");
Py_Initialize();
// 检查初始化是否成功
if (!Py_IsInitialized())
{
qDebug() << "Py_IsInitialized Error";
return pathPointVector;
}
if (m_num == 0)//临时-只运行一次
{
init_numpy();
m_num++;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("print ('---import sys---')");
PyRun_SimpleString("sys.path.append('./')");
PyRun_SimpleString("sys.path.append('D:/WorkSpace/MainModule1222/MainModule1222/x64/Debug/end2end_spine_outline/')");
PyRun_SimpleString("sys.path.append('D:/anaconda3/envs/my_pytorch/Lib/site-packages')");
PyObject *pModule = NULL;
pModule = PyImport_ImportModule("end2end_spine");
if (!pModule)