Standalone Python applications in Linux

前端 未结 8 2365
被撕碎了的回忆
被撕碎了的回忆 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 $@
    

提交回复
热议问题