Linux 我们可以在 Python 中使用 C 代码吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18762621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we use C code in Python?
提问by dspjm
I know that Python provides an API so you can call Python interpreter in C code, but what I want is the opposite.
我知道 Python 提供了一个 API,因此您可以在 C 代码中调用 Python 解释器,但我想要的是相反的。
My program needs to use some C API, so the code must be written in C. But I also want to package the program with Python. Which means I want to invoke those C function or executables in Python. Is that possible?
我的程序需要用到一些C API,所以代码必须用C编写。但我也想用Python打包程序。这意味着我想在 Python 中调用那些 C 函数或可执行文件。那可能吗?
If I want the C code to be a library, which means I use it with #include
and linkage of the *.o
likely in Python, how to do it? Is that possible? If I write the C code into executable, which means it becomes a command, can I invoke it in Python directly?
如果我想让 C 代码成为一个库,这意味着我在 Python 中使用它#include
和*.o
可能的链接,该怎么做?那可能吗?如果我把C代码写成可执行文件,也就是说它变成了一个命令,我可以直接在Python中调用它吗?
Also, I heard that Python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?
另外,听说可以编译Python代码,是不是说不用源文件就可以执行代码?输出文件是二进制文件吗?它会提高性能吗?
采纳答案by Jacqui Gurto
I want to invoke those C function or executables in python. Is that possible.
我想在 python 中调用那些 C 函数或可执行文件。那可能吗。
Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:
是的,您可以编写可以作为模块导入 Python 的 C 代码。Python 调用这些扩展模块。您可以直接从 Python 调用它,文档中的示例:
Python Code
Python代码
import example
result = example.do_something()
C Code
C代码
static PyObject * example(PyObject *self)
{
// do something
return Py_BuildValue("i", result);
}
If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.
如果我希望 C 代码成为一个库,这意味着我将它与 #include 和 *.o 的链接一起使用,可能在 python 中,如何做或者是否可能。
You build it as a shared library *.dllor *.soYou can also investigate using distutils to distribute your module.
您将其构建为共享库*.dll或*.so您还可以调查使用 distutils 分发您的模块。
If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?
如果我把C代码写成可执行文件,也就是说它变成了一个命令,我可以直接在python中调用它吗?
If you write a *.exethen you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python"or a "Python program with some C".
如果您编写*.exe ,那么您正在做相反的事情(从 C调用Python)。您选择的方法(exe 与共享库)取决于您是想要“带有一些 Python 的 C 程序”还是“带有一些 C 的 Python 程序”。
Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?
另外,听说python代码是可以编译的,是不是说不用源文件就可以执行代码了?输出文件是二进制文件吗?它会提高性能吗?
Python reads *.pyfiles and compiles to *.pycbytecodefiles when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided."(from the Python glossary) So if you haven't edited your *.pyfiles, it will run the *.pyc. You can distribute *.pycfiles without *.pyfiles, however they are not encrypted and can be reverse-engineered.
Python 读取*.py文件并在您运行它时编译为*.pyc字节码文件。然后字节码在 Python 虚拟机中运行。这意味着“第二次执行相同的文件会更快,因为可以避免从源代码重新编译为字节码。” (来自 Python 词汇表)因此,如果您尚未编辑*.py文件,它将运行*.pyc。您可以在没有*.py文件的情况下分发*.pyc文件,但它们未加密并且可以进行逆向工程。
回答by Daniel Hepper
Yes, it is possible to extend Python with C/C++.
是的,可以使用 C/C++ 扩展 Python。
Have a look at the documentation: Extending Python with C or C++
查看文档:用 C 或 C++ 扩展 Python