我们可以在 Python 中使用 C 代码吗? [英] Can we use C code in Python?

查看:27
本文介绍了我们可以在 Python 中使用 C 代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Python 提供了一个 API,所以你可以在 C 代码中调用 Python 解释器,但我想要的是相反的.

我的程序需要使用一些C API,所以代码必须用C编写.但我也想用Python打包程序.这意味着我想在 Python 中调用那些 C 函数或可执行文件.这可能吗?

如果我希望 C 代码成为一个库,这意味着我将它与 #include 和可能在 Python 中的 *.o 链接一起使用,该怎么做它?那可能吗?如果我把C代码写成可执行文件,也就是变成一个命令,我可以直接在Python中调用吗?

另外,听说Python代码可以编译,是不是说不用源文件也可以执行代码?输出文件是二进制文件吗?它会提高性能吗?

解决方案

我想在 python 中调用那些 C 函数或可执行文件.有可能吗.

是的,您可以编写可作为模块导入 Python 的 C 代码.Python 调用这些扩展模块.您可以直接从 Python 调用它,documentation 中的一个示例:

Python 代码

导入示例结果 = example.do_something()

C 代码

静态 PyObject * 示例(PyObject *self){//做一点事返回 Py_BuildValue("i", 结果);}

<块引用>

如果我希望 C 代码成为一个库,这意味着我将它与 #include 和 *.o 的链接一起使用,可能在 python 中,如何做到这一点或者这可能.

您将其构建为共享库 *.dll*.so您还可以调查使用 distutils 分发您的模块.

<块引用>

如果我把C代码写成可执行文件,也就是变成命令,我可以直接在python中调用吗?

如果你写一个 *.exe 那么你正在做相反的事情(调用 来自 C 的 Python).您选择的方法(exe 与共享库)取决于您是想要带有一些 Python 的 C 程序"还是带有一些 C 的 Python 程序".

<块引用>

另外,听说python代码可以编译,是不是说不用源文件也可以执行代码?输出文件是二进制文件吗?它会提高性能吗?

Python 会在您运行时读取 *.py 文件并编译成 *.pyc 字节码 文件.然后字节码在 Python 虚拟机中运行.这意味着第二次执行相同的文件更快,因为可以避免从源代码到字节码的重新编译."(来自 Python 词汇表)所以如果你还没有编辑你的 *.py 文件,它将运行 *.pyc.您可以分发没有 *.py 文件的 *.pyc 文件,但它们没有加密并且可以进行逆向工程.

I know that Python provides an API so you can call Python interpreter in C code, but what I want is the opposite.

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?

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?

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?

解决方案

I want to invoke those C function or executables in python. Is that possible.

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 Code

import example
result = example.do_something()

C Code

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.

You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.

If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?

If you write a *.exe then 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".

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 reads *.py files and compiles to *.pyc bytecode files 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 *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.

这篇关于我们可以在 Python 中使用 C 代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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