七武海狂新 2022-03-21 21:32 采纳率: 66.7%
浏览 195
已结题

由c++编写的qt在调用python文件时,无法读取文件

如题,运行时无法读取python文件。编译器是qt的creator,都是32位的。
.pro文件的内容
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32: LIBS += -LD:/python3.6.7/libs/ -lpython36

INCLUDEPATH += D:/python3.6.7/libs
DEPENDPATH += D:/python3.6.7/libs

win32:!win32-g++: PRE_TARGETDEPS += D:/python3.6.7/libs/python36.lib
else:win32-g++: PRE_TARGETDEPS += D:/python3.6.7/libs/libpython36.a

main.cpp中的内容

//#include "mainwindow.h"

//#include <QApplication>
# include "D:\python3.6.7\include\Python.h"
# include <iostream>
# include <QtDebug>


int main(int argc, char *argv[])
{
    Q_UNUSED(argc)
    Q_UNUSED(argv)
//    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
//    return a.exec();

Py_SetPythonHome(L"D:\\python3.6.7");
    Py_Initialize(); // 初始化python解释器
    // 将python工作路径切换到带调用mo'kuai 所在目录,一定要保证路径名的正确性


    // 判断是否成功
    if(!Py_IsInitialized()){
        qDebug() << "python初始化失败,即将退出程序";
        return -1;
    }
    // 导入测试的py文件
    PyObject * pModule = PyImport_ImportModule("test.py");
    if(!pModule){
        qDebug() << sizeof (* pModule);
        qDebug() << "导入python文件失败,即将退出程序";
        return -1;
    }

    // 获取py文件的phello函数。
    PyObject * pFun_phello = PyObject_GetAttrString(pModule, "phello");
    if(!pFun_phello){
        qDebug() << "获得py文件的函数失败,即将退出程序";
    }

    // 调用phello函数。
    PyObject * jiehuo_helloworld = PyObject_CallFunction(pFun_phello, 0);
    char * reshelloworld;
    PyArg_Parse(jiehuo_helloworld, "s", &reshelloworld);

    qDebug() << "我成功的在qt内实现了利用python打印" << reshelloworld;

    qDebug() << sizeof(void*)*8;
}

test.py

def phello():
    return 'hello world!'

def noadd(a, b):
    return a+b

如果注释掉main.cpp中判断py文件是否读取成功那里的代码,会说程序异常结束,倘若不注释掉的话,会打印:

8
导入python文件失败,即将退出程序

python36.dll和py文件都放到了exe同目录下。
希望可以调用python打印hello world!。
  • 写回答

1条回答 默认 最新

  • _GX_ 2022-03-21 21:45
    关注

    PyImport_ImportModule()函数参数是module name,而不是文件名

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 创建了问题 3月21日

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错