How can I make a Python script standalone executable to run without ANY dependency?

后端 未结 19 2752
眼角桃花
眼角桃花 2020-11-21 04:51

I\'m building a Python application and don\'t want to force my clients to install Python and modules.

So, is there a way to compile a Python script to be a standalone

19条回答
  •  遇见更好的自我
    2020-11-21 05:37

    Since it seems to be missing from the current list of answers, I think it is worth mentioning that the standard library includes a zipapp module that can be used for this purpose. Its basic usage is just compressing a bunch of Python files into a zip file with extension .pyz than can be directly executed as python myapp.pyz, but you can also make a self-contained package from a requirements.txt file:

    $ python -m pip install -r requirements.txt --target myapp
    $ python -m zipapp -p "interpreter" myapp
    

    Where interpreter can be something like /usr/bin/env python (see Specifying the Interpreter).

    Usually, the generated .pyz / .pyzw file should be executable, in Unix because it gets marked as such and in Windows because Python installation usually registers those extensions. However, it is relatively easy to make a Windows executable that should work as long as the user has python3.dll in the path.

    If you don't want to require the end user to install Python, you can distribute the application along with the embeddable Python package.

提交回复
热议问题