本文介绍了有没有办法将python应用程序编译成静态二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



--always-copy 不能按预期工作,因此您必须手动将一堆库复制到virtualenv中)



(理论上)有



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

解决方案

有两种方法可以解决问题


  1. 使用静态生成器,例如冻结或 pyinstaller py2exe

  2. 使用 cython


$ b进行编译$ b

我将解释如何使用第二种方法,因为第一种方法不是跨平台和版本的,并且已在其他答案中进行了解释。另外,使用pyinstaller之类的程序通常会导致文件很大,而使用cython会导致文件大小为KBs



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

  sudo pip install cython 
mv test.py test.pyx

然后,您可以使用 cython 和GCC进行编译( cython 从Python .pyx 文件生成C文件,然后GCC编译C文件)



(参考)

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

注意:取决于您的版本python,您可能必须更改最后一个命令。要知道您使用的是哪个版本的python,只需使用

  $ python -V 

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



其他注意事项


  1. Cython用于将C类型变量定义用于静态内存分配加快Python程序的速度。但是,就您而言,您仍将使用传统的Python定义。

  2. 如果您使用的是其他库(例如,例如 opencv ),您可能必须使用 -L 提供目录,然后使用 -l 在GCC标志中。有关更多信息,请参阅GCC标志


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.

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-copy doesn't work as expected) and generally slow.

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

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 run or ./mypython -m gunicorn -c ./gunicorn.conf myapp.wsgi:application.

解决方案

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

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

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

sudo pip install cython
mv test.py test.pyx

Then, you can use cython along with GCC to compile it (cython generates a C file out of a Python .pyx file, and then GCC compiles the C file)

(in reference to 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 -V

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

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 -L and then specify the name of the library using -l in the GCC Flags. For more information on this, please refer to GCC flags

这篇关于有没有办法将python应用程序编译成静态二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 12:41
查看更多