Standalone Python applications in Linux

前端 未结 8 2328
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 10:54

How can I distribute a standalone Python application in Linux?

I think I can take for granted the presence of a recent Python interpreter in any modern distribution.

相关标签:
8条回答
  • 2020-12-04 11:30

    Setuptools is overkill for me since my program's usage is quite limited, so here's my homegrown alternative.

    I bundle a "third-party" directory that includes all prerequisites, and use site.addsitedir so they don't need to be installed globally.

    # program startup code
    import os
    import sys
    import site
    path = os.path.abspath(os.path.dirname(__file__))
    ver = 'python%d.%d' % sys.version_info[:2]
    thirdparty = os.path.join(path, 'third-party', 'lib', ver, 'site-packages')
    site.addsitedir(thirdparty)
    

    Most of my prereqs have setup.py installers. Each bundled module gets its own "install" process, so any customized stuff (e.g. ./configure) can be run automatically. My install script runs this makefile as part of the install process.

    # sample third-party/Makefile
    PYTHON_VER = `python -c "import sys; \
            print 'python%d.%d' % sys.version_info[:2]"`
    PYTHON_PATH = lib/$(PYTHON_VER)/site-packages
    MODS = egenix-mx-base-3.0.0 # etc
    
    .PHONY: all init clean realclean $(MODS)
    all: $(MODS)
    $(MODS): init
    init:
        mkdir -p bin
        mkdir -p $(PYTHON_PATH)
    clean:
        rm -rf $(MODS)
    realclean: clean
        rm -rf bin
        rm -rf lib
    
    egenix-mx-base-3.0.0:
        tar xzf $@.tar.gz
        cd $@ && python setup.py install --prefix=..
        rm -rf $@
    
    0 讨论(0)
  • 2020-12-04 11:33

    You can use cx_Freeze to do this. It's just like py2exe (bundles together the interpreter and and startup script and all required libraries and modules), but works on both Linux and Windows.

    It collects the dependencies from the environment in which it is run, which means that they need to be suitable for the destination as well. If you're doing something like building on 32 bit Debian and deploying on another 32 bit Debian, then that's fine. You can handle 32/64 bit differences by building multiple versions in appropriate environments (e.g. 32 bit and 64 bit chroots) and distributing the appropriate one. If you're wanting something more generic (e.g. build on Debian, deploy on any distribution), then this gets a bit murky, depending on exactly what your dependencies are.

    If you're doing a fairly straightforward distribution (i.e. you know that your build environment and deploy environments are similar), then this avoids the rather complex rpm/deb/egg/etc step (using cx_Freeze is very easy, especially if you're familiar with py2exe). If not, then anything from rolling your own dependancy installer to deb/rpm/egg/etc building will work, depending on how much work you want to do, how much flexibility with required versions you want to offer, and what the dependencies are.

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