问题描述
我有一个Python2.6程序,可以使用Cython加载编译为.so文件的Python模块。我使用Cython将.py模块编译为.so文件,并且一切正常。
I have a Python2.6 program that can load Python modules compiled to .so files using Cython. I used Cython to compile the .py modules to .so files and everything works fine.
这是我与Cython一起使用的setup.py文件:
This is the setup.py file I use with Cython:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
Extension("ldap", ["ldap.pyx"]),
Extension("checker", ["checker.pyx"]),
Extension("finder", ["finder.pyx"]),
Extension("utils", ["utils.pyx"]),
]
setup(
name = 'bchecker',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
所以我知道我可以编译使用Cython的Python模块(我想Cython会从我的Python文件创建 C文件,然后对其进行编译),但是我可以将我的主Python程序编译为可以在Linux平台上执行的程序吗?如果是这样,将不胜感激Cython命令行示例。谢谢。
So I know I can compile Python modules using Cython (I guess Cython creates 'C' files from my Python files and then compiles them), but can I compile my main Python program to something I can execute on a Linux platform? If so, a Cython command line example would be appreciated. Thanks.
推荐答案
与亚当·马坦(Adam Matan)和其他人的主张相反,您 可以 实际上是使用Cython从纯Python(.py)文件创建了一个可执行二进制文件。
Contrary to what Adam Matan and others assert, you can in fact create a single executable binary file using Cython, from a pure Python (.py) file.
是的,Cython旨在按规定使用-如一种简化了为CPython python运行时编写C / C ++扩展模块的方法。
Yes, Cython is intended to be used as stated - as a way of simplifying writing C/C++ extension modules for the CPython python runtime.
但是,正如nudzo在此,您可以使用-嵌入
But, as nudzo alludes to in this comment, you can use the --embed
switch at the command line prompt.
这里是一个非常简单的示例。我正在使用python3和cython3在Debian Sid工作站上执行此操作。
Here is an extremely simple example. I am peforming this from a Debian Sid workstation, using python3 and cython3..
请确保您具有 python-dev 或 python3 -dev 软件包已预先安装。
Make sure you have python-dev or python3-dev packages installed beforehand.
1)创建一个非常简单的Python程序,称为 hello.py
1) Create a very simple Python program called hello.py
print( Hello World! )
print("Hello World!")
2)使用Cython将python程序编译为C ...
2) Use Cython to compile your python program into C...
cython3 --embed -o hello.c hello.py
3)使用GCC将hello.c编译为名为 hello ...
3) Use GCC to compile hello.c into an executable file called hello...
gcc -Os -I /usr/include/python3.3m -o hello hello.c -lpython3.3m -lpthread -lm -lutil -ldl
4)您最终得到一个名为 hello 的文件...
4) You end up with a file called hello ...
您好:ELF 64位LSB可执行文件,x86-64,版本1(SYSV),
动态链接(使用共享库),用于GNU / Linux 2.6.32,
BuildID [sha1] = 006f45195a26f1949c6ed051df9cbd4433e1ac23,未剥离
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=006f45195a26f1949c6ed051df9cbd4433e1ac23, not stripped
$ ldd hello
linux-vdso.so.1 (0x00007fff273fe000)
libpython3.3m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.3m.so.1.0 (0x00007fc61dc2c000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc61da0f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc61d70b000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fc61d508000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc61d304000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc61cf5a000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc61cd52000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fc61cb28000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc61c90f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc61e280000)
在这种情况下,可执行文件动态链接到我的Debian系统上的Python 3.3。
In this case, the executable is dynamically linked to Python 3.3 on my Debian system.
5)运行你好 ...
Hello World!
Hello World!
如您所见,使用这种方法基本上可以使用Cython将您的纯Python应用程序转换为可执行的,已编译的目标代码。
As you can see, using this method you can basically use Cython to convert your pure Python applications into executable, compiled object code.
我正在将此方法用于更为复杂的应用程序-例如,功能强大的Python / PySide / Qt
I am using this method for vastly more complex applications - for example, a full blown Python/PySide/Qt application.
对于不同版本的Python,您可以定制gcc -I
并 -l
进行切换。
For different versions of Python, you tailor the gcc -I
and -l
switches to suit.
然后可以将可执行文件打包为分发文件(.deb等)文件,而无需打包Python / PySide / Qt文件-t他的优点是,即使在该发行版上将发行版更新为相同版本的Python等后,您的应用程序仍应能够运行。
You can then package the executable as a distribution (.deb, etc.) file, without having to package the Python/PySide/Qt files - the advantage being that your application should still be able to run even after a distribution update to the same versions of Python, etc. on that distribution.
这篇关于使用Cython编译主要的Python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!