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

前端 未结 5 722
醉酒成梦
醉酒成梦 2020-12-04 05:45

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 t

相关标签:
5条回答
  • 2020-12-04 06:28

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

    PyPi page of Freeze

    Python Wiki page of Freeze

    Sourceforge page of Freeze

    0 讨论(0)
  • 2020-12-04 06:29

    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
    0 讨论(0)
  • 2020-12-04 06:29

    Freeze options:

    • https://pypi.python.org/pypi/bbfreeze/1.1.3
    • http://cx-freeze.sourceforge.net/

    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

    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.

    0 讨论(0)
  • 2020-12-04 06:31

    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.

    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

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

    0 讨论(0)
  • 2020-12-04 06:34

    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.

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

    0 讨论(0)
提交回复
热议问题