Qt中嵌入Python代码爬坑

Python官网的文件中有将Python嵌入的,这是python3.8的嵌入#define PY_SSIZE_T_CLEAN#include <Python.h>intmain(int argc, char *argv[]){ wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot dec
摘要由CSDN通过智能技术生成

实现这个过程是根据QT中 使用c++调用python这个博客来的,只是我没有作者那么容易就实现了,所以记录下着爬坑经过。
Python官网的文件中有讲解Python嵌入的,这是python3.8的嵌入

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

运行报错:关于slots

在这里插入图片描述
此处参考了Qt与Python混合编程经验记录,感谢大佬的博客!
报错原因:这是由于QT中定义了slots作为关键了,而Python3中有使用slot作为变量,所以有冲突。
解决办法:把Python代码暂时取消冲突的宏定义,然后重新定义相关宏
报错的是object.h:
修改为:

typedef struct{
        

      const char* name;     

      int basicsize;    

       int itemsize;     

      unsigned int flags;

      #undef slots     //这里取消slots宏定义

       PyType_Slot *slots;  /* terminated by slot==0. */

      #define slots Q_SLOTS  //这里恢复slots宏定义与QT中QObjectDefs.h中一致

  } PyType_Spec;  

运行出现新的报错:

Fatal Python error: failed to get the Python codec of the filesystem encoding

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值