Python 在 Cython 中制作可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22507592/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:04:39  来源:igfitidea点击:

Making an executable in Cython

pythonpackagescythonexecutable

提问by Paul Nelson

Been playing with cython. Normally program in Python, but used C in a previous life. I can't figure out how to make a free-standing executable.

一直在玩cython。通常用 Python 编程,但在以前的生活中使用过 C。我不知道如何制作一个独立的可执行文件。

I've downloaded cython, and I can make a .pyx file (that's just a normal Python file with a .pyx extension), that executes in the Python shell, using: import pyximport; pyximport.install()

我已经下载了 cython,我可以制作一个 .pyx 文件(这只是一个带有 .pyx 扩展名的普通 Python 文件),它在 Python shell 中执行,使用:pyximport.install()

I can generate a .c file at the command line with: cython file.pyx I can generate a .so file by building a standard setup.py and executing:

我可以在命令行中生成一个 .c 文件: cython file.pyx 我可以通过构建标准 setup.py 并执行来生成一个 .so 文件:

setup.py build_ext --inplace

I've tried making an executable out of the .so file using gcc with various options, but always have tons of missing files, headers, etc. Have tried pointing to headers from virtually everywhere, but with no success, and am not really familiar with what all the gcc options do, or even if I should be using gcc.

我试过使用带有各种选项的 gcc 从 .so 文件中制作一个可执行文件,但总是有大量丢失的文件、头文件等。尝试过从几乎所有地方指向头文件,但没有成功,而且我不是很熟悉使用所有 gcc 选项的作用,或者即使我应该使用 gcc。

I've having a disconnect here with the fact that I can run my program in the Python shell, but not at the command line, (I don't want users to have to get into the shell, import modules, etc).

我在这里断开连接,因为我可以在 Python shell 中运行我的程序,但不能在命令行中运行(我不希望用户必须进入 shell、导入模块等)。

What am I missing here?

我在这里缺少什么?

采纳答案by IanH

What you want is the --embedflag for the Cython compiler. There isn't a ton of documentation on it, but thisis what I was able to find. It does link to a simple working example.

你想要的是--embedCython 编译器的标志。没有一吨就可以了文件,但是是我能找到。它确实链接到一个简单的工作示例。

To compile the Cython source code to a C file that can then be compiled to an executable you use a command like cython myfile.pyx --embedand then compile with whichever C compiler you are using.

要将 Cython 源代码编译为 C 文件,然后可以将其编译为可执行文件,您可以使用类似的命令cython myfile.pyx --embed,然后使用您使用的任何 C 编译器进行编译。

When you compile the C source code, you will still need to include the directory with the Python headers and link to the corresponding Python shared library on your system (a file named something like libpython27.soor libpython27.aif you are using Python 2.7).

当您编译 C 源代码时,您仍然需要包含带有 Python 头文件的目录并链接到系统上相应的 Python 共享库(一个类似名称的文件,libpython27.so或者libpython27.a如果您使用的是 Python 2.7)。

Edit:Here are some more instructions on how to get the commands for including the proper headers and linking against the proper libraries.

编辑:这里有更多关于如何获取包含正确标题和链接正确库的命令的说明。

As I said earlier, you need to run the Cython compiler like this:

正如我之前所说,您需要像这样运行 Cython 编译器:

cython <cython_file> --embed

To compile using gcc, you will need to find where the python headers are on your system (you can get this location by running distutils.sysconfig.get_python_inc()(you'll have to import it first). It is probably just the /includesubdirectory in your Python installation directory.

要使用 gcc 进行编译,您需要找到 python 头文件在您的系统上的位置(您可以通过运行获得该位置distutils.sysconfig.get_python_inc()(您必须先导入它)。它可能只是/include您的 Python 安装目录中的子目录。

You will also have to find the python shared library. For Python 2.7 it would be libpython27.aon Windows or libpython2.7.soon Linux.

您还必须找到 python 共享库。对于 Python 2.7,它将libpython27.a在 Windows 或libpython2.7.soLinux 上。

Your gcc command will then be

然后你的 gcc 命令将是

gcc <C_file_from_cython> -I<include_directory> -L<directory_containing_libpython> -l<name_of_libpython_without_lib_on_the_front> -o <output_file_name>

It may be wise to include the -fPICflag. On Windows 64 bit machines you will also have to include the flags -D MS_WIN64that tells mingw to compile for 64 bit windows.

包含-fPIC标志可能是明智的。在 Windows 64 位机器上,您还必须包含-D MS_WIN64告诉 mingw 为 64 位窗口编译的标志。

If you are compiling something that depends on NumPy, you will also need to include the directory containing the NumPy headers. You can find this folder by running numpy.get_include()(again, after importing numpy). Your gcc command then becomes

如果你正在编译依赖于 NumPy 的东西,你还需要包含包含 NumPy 头文件的目录。你可以通过运行找到这个文件夹numpy.get_include()(同样,在导入 numpy 之后)。你的 gcc 命令然后变成

gcc <C_file_from_cython> -I<include_directory> -I<numpy_include_directory> -L<directory_containing_libpython> -l<name_of_libpython_without_lib_on_the_front> -o <output_file_name>

This gcc command option guidemay be helpful.

这个 gcc 命令选项指南可能会有所帮助。

Also, I would recommend you use Cython memory views if possible. That will make it so that you won't have to include the NumPy headers and include the NumPy pxd file in your Cython file. It also makes slicing operations easier for the C compiler to optimize.

另外,如果可能,我建议您使用 Cython 内存视图。这将使您不必在 Cython 文件中包含 NumPy 标头和 NumPy pxd 文件。它还使 C 编译器更容易优化切片操作。

回答by Rafael

Tested this on Ubuntu:

在 Ubuntu 上对此进行了测试:

Install Cythonusing the following (Python 2):

安装Cython使用以下(Python的2):

sudo apt-get install cython

For Python 3:

对于 Python 3:

sudo apt-get install cython3

To compile the Python code into a C code run (change cythonto cython3for Python 3):

编译Python代码成C代码运行(变化cythoncython3用于Python 3):

cython --embed -o example.c example.py

This will generate the example.cfile.

这将生成example.c文件。

Now compile the example.cfile:

现在编译example.c文件:

gcc -Os -I /usr/include/python2.7 example.c -lpython2.7 -o example

Run the file:

运行文件:

./example

Now for Python 3, something like this will work (not tested):

现在对于 Python 3,这样的事情将起作用(未测试):

gcc -Os -I /usr/include/python3.6 example.c -lpython3.6 -o example

where python3.xis the version of Python installed on your machine.

python3.x你的机器上安装的 Python 版本在哪里。