You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++嵌入Python环境中导入pandas报错问题求助

问题详情

环境:64位Python 3.10、Win10系统、VS2013
单独运行包含pandas导入的Python脚本可正常执行,但通过C++调用该脚本时出现错误,test63.py加载pandas失败。

示例代码

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();
}

错误信息

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.

已尝试的解决方案

  • 确认ffi.dll、ffi-7.dll、ffi-8.dll文件未缺失
  • 使用Python 3.7调用时导入pandas无报错,但业务需要使用Python 3.8及以上版本(3.8及以上版本导入pandas均出现相同错误)

内容的提问来源于stack exchange,提问作者panicwq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 22:29:58