将 Python(可能通过中间 C 表示)编译成机器代码有多可行?
据推测,它需要链接到 Python 运行时库,并且 Python 标准库的任何部分本身就是 Python 也需要编译(和链接)。
此外,如果您想对表达式进行动态评估,则需要捆绑 Python 解释器,但也许不允许这样做的 Python 子集仍然有用。
它会提供任何速度和/或内存使用优势吗?大概 Python 解释器的启动时间将被消除(尽管共享库仍需要在启动时加载)。
将 Python(可能通过中间 C 表示)编译成机器代码有多可行?
据推测,它需要链接到 Python 运行时库,并且 Python 标准库的任何部分本身就是 Python 也需要编译(和链接)。
此外,如果您想对表达式进行动态评估,则需要捆绑 Python 解释器,但也许不允许这样做的 Python 子集仍然有用。
它会提供任何速度和/或内存使用优势吗?大概 Python 解释器的启动时间将被消除(尽管共享库仍需要在启动时加载)。
正如@Greg Hewgill 所说,这并非总是可能的,这是有充分理由的。但是,某些类型的代码(例如非常算法代码)可以变成“真正的”机器代码。
有几种选择:
之后,您可以使用现有软件包之一(freeze、Py2exe、PyInstaller)将所有内容放入一个二进制文件中。
总而言之:您的问题没有通用答案。如果您的 Python 代码对性能至关重要,请尝试使用尽可能多的内置功能(或询问“如何使我的 Python 代码更快”的问题)。如果这没有帮助,请尝试识别代码并将其移植到 C(或 Cython)并使用扩展。
尝试ShedSkin Python-to-C++ 编译器,但它远非完美。如果只需要加速,还有 Psyco - Python JIT。但恕我直言,这是不值得的努力。对于代码的速度关键部分,最好的解决方案是将它们编写为 C/C++ 扩展。
Nuitka是一个链接到 libpython 的 Python 到 C++ 编译器。这似乎是一个相对较新的项目。作者声称在 pystone 基准测试中速度比 CPython 有所提高。
一些额外的参考:
https://github.com/dropbox/pyston是 Dropbox 开发的 Python 的 JIT 编译器
http://pythran.readthedocs.io/是用于科学计算的编译时 python 到 C++ 转换器
https://github.com/cosmo-ethz/hope是一个用于科学计算的 JIT python 到 C++ 的翻译器
Jython 有一个针对 JVM 字节码的编译器。字节码是完全动态的,就像 Python 语言本身一样!很酷。(是的,正如 Greg Hewgill 的回答所暗示的,字节码确实使用 Jython 运行时,因此 Jython jar 文件必须与您的应用程序一起分发。)
Psyco是一种即时 (JIT) 编译器:适用于 Python 的动态编译器,代码运行速度快 2-100 倍,但它需要大量内存。
简而言之:它可以更快地运行您现有的 Python 软件,而您的源代码没有任何变化,但它不会像 C 编译器那样编译为目标代码。
答案是“是的,有可能”。您可以获取 Python 代码并尝试使用 CPython API 将其编译为等效的 C 代码。事实上,曾经有一个 Python2C 项目就是这样做的,但我已经很多年没有听说过它了(回到 Python 1.5 天是我最后一次看到它的时候。)
您可以尝试将 Python 代码尽可能多地转换为原生 C,并在需要实际 Python 功能时回退到 CPython API。在过去的一两个月里,我自己一直在玩弄这个想法。然而,这是一项非常大量的工作,而且大量 Python 特性很难翻译成 C:嵌套函数、生成器、除了具有简单方法的简单类之外的任何东西,任何涉及从模块外部修改模块全局变量的东西等等, ETC。
这不会将 Python 编译为机器代码。但允许创建一个共享库来调用 Python 代码。
如果您正在寻找的是一种从 C 运行 Python 代码而不依赖 execp 的简单方法。您可以从 python 代码生成一个共享库,其中包含对Python 嵌入 API的一些调用。好吧,该应用程序是一个共享库,一个 .so 您可以在许多其他库/应用程序中使用。
这是一个创建共享库的简单示例,您可以将其与 C 程序链接。共享库执行 Python 代码。
将执行的python文件是pythoncalledfromc.py
:
# -*- encoding:utf-8 -*-
# this file must be named "pythoncalledfrom.py"
def main(string): # args must a string
print "python is called from c"
print "string sent by «c» code is:"
print string
print "end of «c» code input"
return 0xc0c4 # return something
你可以用python2 -c "import pythoncalledfromc; pythoncalledfromc.main('HELLO')
. 它将输出:
python is called from c
string sent by «c» code is:
HELLO
end of «c» code input
共享库将由以下定义callpython.h
:
#ifndef CALL_PYTHON
#define CALL_PYTHON
void callpython_init(void);
int callpython(char ** arguments);
void callpython_finalize(void);
#endif
相关callpython.c
的是:
// gcc `python2.7-config --ldflags` `python2.7-config --cflags` callpython.c -lpython2.7 -shared -fPIC -o callpython.so
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <python2.7/Python.h>
#include "callpython.h"
#define PYTHON_EXEC_STRING_LENGTH 52
#define PYTHON_EXEC_STRING "import pythoncalledfromc; pythoncalledfromc.main(\"%s\")"
void callpython_init(void) {
Py_Initialize();
}
int callpython(char ** arguments) {
int arguments_string_size = (int) strlen(*arguments);
char * python_script_to_execute = malloc(arguments_string_size + PYTHON_EXEC_STRING_LENGTH);
PyObject *__main__, *locals;
PyObject * result = NULL;
if (python_script_to_execute == NULL)
return -1;
__main__ = PyImport_AddModule("__main__");
if (__main__ == NULL)
return -1;
locals = PyModule_GetDict(__main__);
sprintf(python_script_to_execute, PYTHON_EXEC_STRING, *arguments);
result = PyRun_String(python_script_to_execute, Py_file_input, locals, locals);
if(result == NULL)
return -1;
return 0;
}
void callpython_finalize(void) {
Py_Finalize();
}
您可以使用以下命令编译它:
gcc `python2.7-config --ldflags` `python2.7-config --cflags` callpython.c -lpython2.7 -shared -fPIC -o callpython.so
创建一个名为的文件callpythonfromc.c
,其中包含以下内容:
#include "callpython.h"
int main(void) {
char * example = "HELLO";
callpython_init();
callpython(&example);
callpython_finalize();
return 0;
}
编译并运行:
gcc callpythonfromc.c callpython.so -o callpythonfromc
PYTHONPATH=`pwd` LD_LIBRARY_PATH=`pwd` ./callpythonfromc
这是一个非常基本的例子。它可以工作,但根据库的不同,将 C 数据结构序列化为 Python 以及从 Python 序列化为 C 可能仍然很困难。事情可以在某种程度上自动化......
Nuitka可能会有所帮助。
还有numba,但他们都不打算完全按照您的意愿行事。从 Python 代码生成 C 标头是可能的,但前提是您指定如何将 Python 类型转换为 C 类型或可以推断该信息。有关 Python ast 分析器的信息,请参阅python astroid。