之前使用的是Python2.7,功能正常.结果到了Python3.5,执行就返回空.这是怎么回事?以下条件都符合.
- 执行目录有文件.
- PyCharm可以正常执行.
- 命令行下python3也可以正常执行.
- 那么还有什么可能出错?
在网上一通搜索,也没找到解决办法.后来看到有人不同的写法,试了一下,终于找到了正确结果.这里分享给大家:
int main(int argc, char** argv)
{
Py_Initialize();
init_numpy();
if ( !Py_IsInitialized() )
{
return -1;
}
//导入当前路径
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyRun_SimpleString("print(sys.path)");
#if PYTHON2_7
PyObject* m_pPythonName = PyString_FromString(pPythonPath);
if (m_pPythonName == NULL)
{
GH_LOG_INFO("PyBytes_FromString() error!");
return -1;
}
m_pPythonModule = PyImport_Import(m_pPythonName);
#else
m_pPythonModule = PyImport_Import(PyUnicode_FromString(pPythonPath));
#endif
if (!m_pPythonModule)
{
GH_LOG_INFO("PyImport_Import() error!");
return -1;
}
m_pPythonDict = PyModule_GetDict(m_pPythonModule);
......
}