I'm working with Python 3.10 64-bit,win10,vs2013
I run the python script alone and there are no errors and it also imports the pandas package properly.However, when calling python scripts in c++ (scripts that include imported pandas) it gives an error.
In the following example, loading pandas in test63.py gives an error. testproject.cpp
#include "testproject.h"
#include <QDebug>
testproject::testproject(QObject *parent)
: QObject(parent)
{
Py_SetPythonHome(L"D:/anaconda3/envs/newenvs/");
Py_Initialize();
if (Py_IsInitialized() == 0){
qDebug() << "fal to initialize Python\n";
return;
}
qDebug() << "server start\n";
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('D:/pycode')");
PyRun_SimpleString("import pandas as pd");
PyObject* comModule = PyImport_ImportModule("test63");
if (!comModule)
{
PyErr_Print();
qDebug() << "Python get module failed: test63.py.";
Py_DECREF(comModule);
return ;
}
PyObject* offline_test = PyObject_GetAttrString(comModule, "caljiandan");
PyObject* args = PyTuple_New(2);
PyTuple_SetItem(args, 0, Py_BuildValue("i", 2));
PyTuple_SetItem(args, 1, Py_BuildValue("i", 3));
PyObject* pRet_Ot = PyObject_CallObject(offline_test, args);
int SRresult = -1;
if (pRet_Ot)
{
PyArg_Parse(pRet_Ot, "i", &SRresult);
qDebug() << "succeed:" << SRresult;
}
Py_DECREF(comModule); Py_DECREF(offline_test); Py_DECREF(args); Py_DECREF(pRet_Ot);
qDebug() << "end";
}
testproject::~testproject()
{
Py_Finalize();
}
testproject.h
#ifndef TESTPROJECT_H
#define TESTPROJECT_H
#include <QObject>
#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")
class testproject : public QObject
{
Q_OBJECT
public:
testproject(QObject *parent = 0);
~testproject();
private:
};
#endif // TESTPROJECT_H
main.cpp
#include "testproject.h"
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testproject w;
return a.exec();
}
The error message is as follows:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "D:\anaconda3\envs\newenvs\lib\site-packages\pandas\__init__.py", line 62, in <module>
from pandas.core.api import (
File "D:\anaconda3\envs\newenvs\lib\site-packages\pandas\core\api.py", line 9, in <module>
from pandas.core.dtypes.dtypes import (
File "D:\anaconda3\envs\newenvs\lib\site-packages\pandas\core\dtypes\dtypes.py", line 47, in <module>
from pandas.errors import PerformanceWarning
File "D:\anaconda3\envs\newenvs\lib\site-packages\pandas\errors\__init__.py", line 6, in <module>
import ctypes
File "D:\anaconda3\envs\newenvs\lib\ctypes\__init__.py", line 8, in <module>
from _ctypes import Union, Structure, Array
ImportError: DLL load failed while importing _ctypes: Can't find specified module.
Solutions that have been tried:
1.ffi.dll,ffi-7.dll,ffi-8.These dll files are not missing.
2.When calling python 3.7, import pandas does not report an error. But I need to use python 3.8 or higher (3.8 or higher, pandas gives me an error).
__init__is trying to dynamically load a native library from a fixed list of paths which SetPythonHome doesn't affect and can't find it there.procmon.exeto see which DLL it is loading.fi.dll, ffi-7.dll, ffi-8.dllfiles into the C:\Windows\System32 directory and it solved the problem!