从C程序中调用Python函数 [英] Call a Python function from within a C program

查看:236
本文介绍了从C程序中调用Python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中有一个应用程序,在某些时候我需要解决一个非线性优化问题。不幸的是,AFAIK有非常有限的资源,在C(请让我知道,否则)。然而,在Python中做它很简单,例如。 scipy.optimize.minimize

I have an application in C and at some point I need to solve a non-linear optimization problem. Unfortunately AFAIK there are very limited resources to do that in C (please let me know otherwise). However it is quite simple to do it in Python, e.g. scipy.optimize.minimize.

当我试图这样做时,我遇到了一些似乎很常见的陷阱。 Python.h 未找到,模块未加载,函数调用时出现分段错误等。

While I was trying to do that I encountered some of what it seems to be very frequent pitfalls, e.g. Python.h not found, module not loading, segmentation fault on function call, etc.

推荐答案

有一些事情你必须确保到位以便使此工作:

There are some things that you have to make sure are in place in order to make this work:


  1. 确保您已安装Python(您可能需要 python-dev package)。

  2. 找到您的 Python.h 文件,例如由找到Python.h 。其中一个应出现在 include 文件夹中的子(子)文件夹中,例如。路径应该是 ../ include / python2.7 / Python.h

  3. 插入

  4. 使用任何教学课程,您都可以在C语言代码中使用#include< path_to_Python.h>调用您的Python函数我使用了这一个,它做了。

  1. Make sure you have Python installed (you may need the python-dev package).
  2. Locate your Python.h file, e.g. by locate Python.h. One of the occurrences should be in a sub(sub)folder in the include folder, e.g. the path should be something like ../include/python2.7/Python.h.
  3. Insert #include "<path_to_Python.h>" in your C code in order to be able to use the Python API.
  4. Use any tutorial to call your Python function. I used this one and it did the trick. However there were a couple of small points missing:


  • 每当你使用任何 Py< Name> 函数,例如 PyImport_Import()总是检查结果以确保没有错误,例如

  • Whenever you use any Py<Name> function, e.g. PyImport_Import(), always check the result to make sure there was no error, e.g.

// Load the module object
pModule = PyImport_Import(pName);

if (!pModule)
{
    PyErr_Print();
    printf("ERROR in pModule\n");
    exit(1);
}


  • 在初始化Python解释器之后,即在 Py_Initialize(); ,您必须将当前路径追加到 sys.path ,以便能够加载模块(假设它位于当前目录中):

  • Immediately after initializing the Python interpreter, i.e. after Py_Initialize();, you have to append the current path to sys.path in order to be able to load your module (assuming it is located in your current directory):

    PyObject *sys = PyImport_ImportModule("sys");
    PyObject *path = PyObject_GetAttrString(sys, "path");
    PyList_Append(path, PyString_FromString("."));
    



    • 记住 ../ include / python2.7 / Python.h 您以前使用过的文件?在文件夹中的 -I 选项中,将 include > gcc 选项,例如 -I /System/Library/Frameworks/Python.framework/Versions/2.7/include

    • 还传递到链接器文件夹与所需的库。它应该在 include 文件夹所在的同一文件夹中,例如。 -L /System/Library/Frameworks/Python.framework/Versions/2.7/lib 以及 -lpython2.7 选项(当然根据您的Python版本进行相应调整)。

    • Remember the ../include/python2.7/Python.h file you used before? Include the include folder in the list of the header files directories with the -I option in the gcc options during compilation, e.g. -I /System/Library/Frameworks/Python.framework/Versions/2.7/include.
    • Also pass to the linker the folder with the required libraries. It should be inside the same folder where the include folder is located, e.g. -L /System/Library/Frameworks/Python.framework/Versions/2.7/lib, along with the -lpython2.7 option (of course adjusting it accordingly to your Python version).



    <现在你必须能够成功地编译和执行你的Python程序调用的C程序。

    Now you must be able to successfully compile and execute your C program that calls in it your Python program.

    我希望这是有帮助的,祝你好运!

    I hope this was helpful and good luck!

    资料来源:

    • How do you call Python code from C code?
    • http://www.linuxjournal.com/article/8497?page=0,1
    • http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I
    • http://www.codeproject.com/Articles/11843/Embedding-Python-in-C-C-Part-II
    • Python C API doesn't load module
    • What sets up sys.path with Python, and when?
    • http://linux.die.net/man/1/gcc
    • PyObject segfault on function call
    • ubuntu: I have python, but gcc cant find Python.h
    • How do you call Python code from C code?

    这篇关于从C程序中调用Python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆