0

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

6
  • welcome to SO, your post lacks minimal details. Recommend you read stackoverflow.com/help/minimal-reproducible-example then edit your submission such that folks can respond Commented Nov 26, 2024 at 11:19
  • For those wondering, the message states "the specified module cannot be found". Commented Nov 26, 2024 at 14:06
  • Perhaps ctypes' __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. Commented Nov 26, 2024 at 20:47
  • Run you program alongside procmon.exe to see which DLL it is loading. Commented Nov 27, 2024 at 8:10
  • I put the fi.dll, ffi-7.dll, ffi-8.dll files into the C:\Windows\System32 directory and it solved the problem! Commented Nov 27, 2024 at 9:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.