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

后端 未结 19 2754
眼角桃花
眼角桃花 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:32

    Use Cython to convert to C, compile, and link with GCC.

    Another could be, make the core functions in C (the ones you want to make hard to reverse), compile them and use Boost.Python to import the compiled code (plus you get a much faster code execution). Then use any tool mentioned to distribute.

    0 讨论(0)
  • 2020-11-21 05:35

    For Python 3.2 scripts, the only choice is cx_Freeze. Build it from sources; otherwise it won't work.

    For Python 2.x I suggest PyInstaller as it can package a Python program in a single executable, unlike cx_Freeze which outputs also libraries.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:39

    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. It is recommended.

    0 讨论(0)
  • 2020-11-21 05:39

    You may like py2exe. You'll also find information in there for doing it on Linux.

    0 讨论(0)
  • 2020-11-21 05:40

    Use py2exe.... use the below set up files:

    from distutils.core import setup
    import py2exe
    
    from distutils.filelist import findall
    import matplotlib
    
    setup(
        console = ['PlotMemInfo.py'],
    
        options = {
            'py2exe': {
                'packages': ['matplotlib'],
                'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll',
                'libgdk_pixbuf-2.0-0.dll']
            }
        },
        data_files = matplotlib.get_py2exe_datafiles()
    )
    
    0 讨论(0)
提交回复
热议问题