有没有办法将python应用程序编译成静态二进制文件?

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

Is there a way to compile python application into static binary?

pythonbuild

提问by roboslone

What I'm trying to do is ship my code to a remote server, that may have different python version installed and/or may not have packages my app requires.

我想要做的是将我的代码发送到远程服务器,该服务器可能安装了不同的 python 版本和/或可能没有我的应用程序需要的包。

Right now to achieve such portability I have to build relocatable virtualenv with interpreter and code. That approach has some issues (for example, you have to manually copy a bunch of libraries into your virtualenv, since --always-copydoesn't work as expected) and generally slow.

现在为了实现这种可移植性,我必须使用解释器和代码构建可重定位的 virtualenv。这种方法有一些问题(例如,您必须手动将一堆库复制到您的 virtualenv 中,因为--always-copy无法按预期工作)并且通常很慢。

There's (in theory) a wayto build python itself statically.

有(理论上)一种静态构建python本身的方法

I wonder if I could pack interpreter with my code into one binary and run my application as module. Something like that: ./mypython -m myapp runor ./mypython -m gunicorn -c ./gunicorn.conf myapp.wsgi:application.

我想知道是否可以将解释器和我的代码打包成一个二进制文件,并将我的应用程序作为模块运行。类似的东西:./mypython -m myapp run./mypython -m gunicorn -c ./gunicorn.conf myapp.wsgi:application

回答by R. S. Nikhil Krishna

There are two ways you could go about to solve your problem

有两种方法可以解决您的问题

  1. Use a static builder, like freeze, or pyinstaller, or py2exe
  2. Compile using cython
  1. 使用静态构建器,如冻结,或pyinstaller,或py2exe
  2. 编译使用 cython

I will explain how you can go about doing it using the second, since the first method is not cross platform and version, and has been explained in other answers. Also, using programs like pyinstaller typically results in huge file sizes, where as using cython will result in a file that's KBs in size

我将解释如何使用第二种方法进行操作,因为第一种方法不是跨平台和版本,并且已在其他答案中进行了解释。此外,使用像 pyinstaller 这样的程序通常会产生巨大的文件大小,而使用 cython 会产生大小为 KB 的文件

First, install cython. Then, rename your python file (say test.py) into a .pyxfile

首先,安装cython. 然后,将您的 python 文件(比如test.py)重命名为一个.pyx文件

sudo pip install cython
mv test.py test.pyx

Then, you can use cythonalong with GCC to compile it (cythongenerates a C file out of a Python .pyxfile, and then GCC compiles the C file)

然后,你可以cython和GCC一起使用来编译它(cython从Python.pyx文件中生成一个C文件,然后GCC编译这个C文件)

(in reference to https://stackoverflow.com/a/22040484/5714445)

(参考https://stackoverflow.com/a/22040484/5714445

cython test.pyx --embed
gcc -Os -I /usr/include/python3.5m -o test test.c -lpython3.5m -lpthread -lm -lutil -ldl

NOTE: Depending on your version of python, you might have to change the last command. To know which version of python you are using, simply use

注意:根据您的 Python 版本,您可能需要更改最后一个命令。要知道您使用的是哪个版本的 python,只需使用

$ python -V

You will now have a binary file 'test', which is what you are looking for

您现在将拥有一个二进制文件“test”,这就是您要查找的内容

Other things to note:

其他注意事项

  1. Cython is used to use C-Type Variable definitions for static memory allocation to speed up Python programs. In your case however, you will still be using traditional Python definitions.
  2. If you are using additional libraries (like opencv, for example), you might have to provide the directory to them using -Land then specify the name of the library using -lin the GCC Flags. For more information on this, please refer to GCC flags
  1. Cython 用于使用 C 类型变量定义进行静态内存分配以加速 Python 程序。但是,在您的情况下,您仍将使用传统的 Python 定义。
  2. 如果您正在使用其他库(opencv例如 ),您可能必须向他们提供目录 using -L,然后-l在 GCC 标志中指定库的名称。有关这方面的更多信息,请参阅 GCC 标志

回答by Prakhar Agarwal

You might wish to investigate Nuitka. It takes python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.

您可能希望调查Nuitka。它采用 python 源代码并将其转换为 C++ API 调用。然后它编译成一个可执行的二进制文件(Linux 上的 ELF)。它已经存在几年了,支持各种 Python 版本。

You will probably also get a performance improvement if you use it. Recommended.

如果您使用它,您可能还会获得性能改进。受到推崇的。

回答by André Aulich

If you are on a Mac you can use py2app to create a .app bundle, which starts your Django app when you double-click on it.

如果你在 Mac 上,你可以使用 py2app 创建一个 .app 包,当你双击它时会启动你的 Django 应用程序。

I described how to bundle Django and CherryPy into such a bundle at https://moosystems.com/articles/14-distribute-django-app-as-native-desktop-app-01.html

我在https://moosystems.com/articles/14-distribute-django-app-as-native-desktop-app-01.html描述了如何将 Django 和 CherryPy 捆绑到这样的捆绑包中

In the article I use pywebview to display your Django site in a local application window.

在文章中,我使用 pywebview 在本地应用程序窗口中显示您的 Django 站点。

回答by iScrE4m

Freeze options:

冻结选项:

However, your target server should have the environment you want -> you should be able to 'create' it. If it doesn't, you should build your software to match the environment.

但是,您的目标服务器应该具有您想要的环境 -> 您应该能够“创建”它。如果不是,您应该构建与环境相匹配的软件。

I found this handy guide on how to install custom version of python to a virtualenv, assuming you have ssh access: https://stackoverflow.com/a/5507373/5616110

我找到了这个关于如何将自定义版本的 python 安装到 virtualenv 的方便指南,假设您有 ssh 访问权限:https: //stackoverflow.com/a/5507373/5616110

In virtualenv, you should be able to pip install anything and you shouldn't need to worry about sudo privileges. Of course, having those and access to package manager like apt makes everything a lot easier.

在 virtualenv 中,你应该能够 pip install 任何东西,你不需要担心 sudo 权限。当然,拥有这些并访问像 apt 这样的包管理器会让一切变得更容易。

回答by asc11

You're probably looking for something like Freeze, which is able to compile your Python application with all its libraries into a static binary:

您可能正在寻找类似 Freeze 的东西,它能够将您的 Python 应用程序及其所有库编译为静态二进制文件:

PyPi page of Freeze

冻结的 PyPi 页面

Python Wiki page of Freeze

Freeze 的 Python Wiki 页面

Sourceforge page of Freeze

Freeze 的 Sourceforge 页面