C语言调用python

出现的问题 找不到python的头文件

1、更换镜像

2、更新python : sudo apt-get install python3.10-dev

1、包含Python.h头文件,以便使用Python API。

2、使用void Py_Initialize()初始化Python解释器,

3、使用PyObject *PyImport_ImportModule(const char *name)和PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)获取sys.path对象,并利用 int PyList_Append(PyObject *list, PyObject *item)将当前路径.添加到sys.path中,以便加载 当前的Python模块(Python文件即python模块)。

4、使用PyObject *PyImport_ImportModule(const char *name)函数导入Python模块,并检查是否 有错误。

5、使用PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)函数获取 Python函数对象,并检查是否可调用。

6、使用PyObject *PyObject_CallObject(PyObject *callable, PyObject *args)函数调用 Python函数,并获取返回值。

7、使用void Py_DECREF(PyObject *o)函数释放所有引用的Python对象。

8、结束时调用void Py_Finalize()函数关闭Python解释器。

 garbage.c

#include <Python.h>
#include "garbage.h"
#include <string.h>

    PyObject *pModule = NULL;
    PyObject *pPath = NULL;
    PyObject *pSysPath = NULL;
    PyObject *pFunc = NULL;
    PyObject *pArgs = NULL;
    PyObject *pResult = NULL;
void garbage_init(void)
{
     // 1. 初始化Python解释器
    Py_Initialize();

    // 2. 获取sys.path并添加当前路径
    pSysPath = PySys_GetObject("path");
    if (pSysPath == NULL) {
        fprintf(stderr, "Failed to get sys.path\n");
        goto fail;
    }
    pPath = PyUnicode_FromString(".");
    if (pPath == NULL) {
        fprintf(stderr, "Failed to create path object\n");
        goto fail;
    }
    if (PyList_Append(pSysPath, pPath) == -1) {
        fprintf(stderr, "Failed to append to sys.path\n");
        goto fail;
    }
}

void garbage_final(void)
{
    // 8. 关闭Python解释器
    Py_Finalize();

}
char* garbage_category(char *category) {
    // 3. 导入Python模块
    pModule = PyImport_ImportModule("garbage");
    if (pModule == NULL) {
        fprintf(stderr, "Failed to import module\n");
        goto fail;
    }

    // 4. 获取Python函数对象
    pFunc = PyObject_GetAttrString(pModule, "alibaba_garbage");
    if (pFunc == NULL || !PyCallable_Check(pFunc)) {
        fprintf(stderr, "Function not found or not callable\n");
        goto fail;
    }

    // 5. 准备调用函数的参数
    pArgs = PyTuple_New(0);  // 如果函数没有参数,传递一个空元组
    if (pArgs == NULL) {
        fprintf(stderr, "Failed to create arguments\n");
        goto fail;
    }

    // 6. 调用Python函数
    pResult = PyObject_CallObject(pFunc, pArgs);
    if (pResult == NULL) {
        fprintf(stderr, "Function call failed\n");
        goto fail;
    }
    char* result = NULL;
    if(!PyArg_Parse(pResult,"s",&result)){
        fprintf(stderr, "Failed to return\n");
        goto fail;
    }

    printf("result = %s\n",result);
    category = (char*)malloc(sizeof(char*)*(strlen(result)+1));
    memset(category,'\0',sizeof(char*)*(strlen(result)+1))
    strcpy*(category,result);
    return category;

    // 7. 清理Python对象
    
fail:
    Py_DECREF(pArgs);
    Py_XDECREF(pFunc);  // 使用 Py_XDECREF 来处理可能为 NULL 的对象
    Py_XDECREF(pModule); // 同上
    Py_XDECREF(pPath);
    Py_XDECREF(pResult);
}

 

 garbage.h

#ifndef __GARBAGE__H
#define __GARBAGE__H

void garbage_init(void);
void garbage_final(void);
char* garbage_category(char *category);
#endif

main.c

 

#include <stdio.h>
#incldeu "garbage.h"
int main()
{
    char *category = NULL;
    garbage_init();
    
    category = garbage_category(category);
    printf("category = %s\n",category)
    garbage_final();
    
    if(category){
        free(category);
    }
}
   
    

相关推荐

  1. C语言调用python

    2024-07-14 09:24:02       25 阅读
  2. Python调用C++/C

    2024-07-14 09:24:02       55 阅读
  3. 语言编程:在C#应用程序中调用Python

    2024-07-14 09:24:02       39 阅读
  4. Python 调用 CC 调用 Python 方法

    2024-07-14 09:24:02       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 09:24:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 09:24:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 09:24:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 09:24:02       69 阅读

热门阅读

  1. pytorch GPU cuda 使用 报错 整理

    2024-07-14 09:24:02       25 阅读
  2. 大语言模型LLM

    2024-07-14 09:24:02       21 阅读
  3. 大模型时代,还需要跨端framework吗?

    2024-07-14 09:24:02       27 阅读
  4. 搭建docker私有仓库

    2024-07-14 09:24:02       26 阅读
  5. uvm中使用clone时,为什么要使用$cast

    2024-07-14 09:24:02       23 阅读