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

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

问题描述

我知道Python提供了API,因此您可以用C代码调用Python解释器,但是我想要的是相反的情况.

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

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

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代码成为一个库,这意味着我可能将它与#include以及*.o的链接一起使用在Python中,该怎么做?那可能吗?如果我将C代码写入可执行文件,这意味着它成为命令,是否可以直接在Python中调用它?

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?

另外,我听说可以编译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中调用这些C函数或可执行文件.那有可能吗.

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

是的,您可以编写可以作为模块导入到Python中的C代码. Python调用了这些扩展模块.您可以直接从Python调用它,例如文档:

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代码

import example
result = example.do_something()

C代码

static PyObject * example(PyObject *self)
{
    // do something
    return Py_BuildValue("i", result);
}

如果我希望C代码成为一个库,这意味着我将它与#include以及* .o的链接(可能在python中)一起使用,怎么做或有可能.

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.

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

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

如果我将C代码写入可执行文件,这意味着它成为命令,我可以直接在python中调用它吗?

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

如果您编写的是 *.exe ,则说明情况与此相反(调用还是要是带有某些C的Python程序" .

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".

此外,我听说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读取 *.py 文件,并在运行时将其编译为 *.pyc bytecode 文件.然后,字节码在Python虚拟机中运行.这意味着第二次执行同一文件的速度更快,因为可以避免从源代码到字节码的重新编译." (来自Python词汇表)因此,如果您尚未编辑 *.py 文件,它将运行 *.pyc .您可以分发 *.pyc 文件而不分发 *.py 文件,但是这些文件未加密,可以进行反向工程.

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天全站免登陆