[英]Call a Python function from within a C program
I have an application in C and at some point I need to solve a non-linear optimization problem. 我在C中有一个应用程序,在某些时候我需要解决非线性优化问题。 Unfortunately AFAIK there are very limited resources to do that in C (please let me know otherwise).
不幸的是,AFAIK在C中的资源非常有限(请让我知道其他情况)。 However it is quite simple to do it in Python, eg scipy.optimize.minimize .
但是在Python中使用它很简单,例如scipy.optimize.minimize 。
While I was trying to do that I encountered some of what it seems to be very frequent pitfalls, eg Python.h
not found, module not loading, segmentation fault on function call, etc. 当我试图这样做时,我遇到了一些看起来非常频繁的陷阱,例如找不到
Python.h
,模块没有加载,函数调用时出现分段错误等。
What is a quick and easy first-timer's way to link the two programs? 什么是快速简便的第一计时器链接这两个程序的方式?
There are some things that you have to make sure are in place in order to make this work: 为了使这项工作有一些你必须确保到位的东西:
python-dev
package). python-dev
包)。 Python.h
file, eg by locate Python.h
. Python.h
文件,例如locate Python.h
。 One of the occurrences should be in a sub(sub)folder in the include
folder, eg the path should be something like ../include/python2.7/Python.h
. include
文件夹的sub(sub)文件夹中,例如路径应该是../include/python2.7/Python.h
。 #include “<path_to_Python.h>"
in your C code in order to be able to use the Python API. #include “<path_to_Python.h>"
以便能够使用Python API。 Use any tutorial to call your Python function. 使用任何教程来调用Python函数。 I used this one and it did the trick.
我用过这个 ,它就是诀窍。 However there were a couple of small points missing:
然而,有几个小点缺失:
Whenever you use any Py<Name>
function, eg PyImport_Import()
, always check the result to make sure there was no error, eg 无论何时使用任何
Py<Name>
函数,例如PyImport_Import()
, 总是检查结果以确保没有错误,例如
// Load the module object pModule = PyImport_Import(pName); if (!pModule) { PyErr_Print(); printf("ERROR in pModule\\n"); exit(1); }
Immediately after initializing the Python interpreter, ie after Py_Initialize();
初始化Python解释器之后,即在
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): ,你必须将当前路径附加到
sys.path
以便能够加载你的模块(假设它位于你当前的目录中):
PyObject *sys = PyImport_ImportModule("sys"); PyObject *path = PyObject_GetAttrString(sys, "path"); PyList_Append(path, PyString_FromString("."));
.py
. .py
。 ../include/python2.7/Python.h
file you used before? ../include/python2.7/Python.h
文件吗? Include the include
folder in the list of the header files directories with the -I
option in the gcc
options during compilation, eg -I /System/Library/Frameworks/Python.framework/Versions/2.7/include
. gcc
选项中使用-I
选项在头文件目录列表中包含include
文件夹,例如-I /System/Library/Frameworks/Python.framework/Versions/2.7/include
。 include
folder is located, eg -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). include
文件夹所在的同一文件夹中,例如-L /System/Library/Frameworks/Python.framework/Versions/2.7/lib
,以及-lpython2.7
选项(当然会相应地调整它) Python版)。 Now you must be able to successfully compile and execute your C program that calls in it your Python program. 现在你必须能够成功编译和执行你的C程序,并在其中调用你的Python程序。
I hope this was helpful and good luck! 我希望这是有益的,祝你好运!
Sources: 资料来源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.