Search code examples
pythonlinuxdeploymentportabilityportable-applications

Easy Deploying of Python and application in one Bundle , For Linux


I Develop Fairly large python application on server side , with all database connect , files extraction , parsing , command line calls.

It becomes a nightmare for deploying as i used many third party modules outside of standard python lib. And i lost track of them . Especially Differnt Linux OS uses different version of them so it is no longer good to install them using OS's package manager.

I want to deploy them in all one bundle including current python version i am using (Most OS Still ship with Python 2.5,6 i am using 2.7 and 2.7 specific features.) .

Further more , i have to teach the client to how to deploy , so they can test out in other servers. But they are not linux experts . I have to make it easy , in one script or by doing copy and paste.

There is Portablepython for Windows But there's nothing for Linux. And i had never used python Packaging as i usually work on server that i only host.

Please enlighten me of avaliable packaging and deployment options for python , that includes all the installed python modules and python itself.


Solution

  • Most Python packages can be deployed by creating a lib or similar directory in your deployment, and adding it to sys.path in Python, or PYTHONPATH outside, then copying the package directory (usually inside the directory you unzipped) into that directory. This lets you keep the package with your deployed code, say, in a Mercurial repository.

    Deploying Python itself is going to be a bit more hassle, but if you can control where it's installed (such as /usr/local or /opt), then it's just a matter of ./configure --prefix=..., make, and sudo make install. Then you can point your scripts to that Python by starting them with a line like #!/usr/local/bin/python, as long as the script is marked executable.

    For example, if you were deploying code that needs docutils, then you'd do something like:

    cd projectDir
    mkdir -p lib
    tar xzvf ~/Downloads/docutils-0.8.tgz
    mv docutils-0.8/docutils lib
    rm -r docutils-0.8
    

    Then a Python module in this directory would just add the following at the start:

    #!/usr/local/bin/python
    
    import os
    import sys
    sys.path(os.path.join(os.path.dirname(sys.argv[0]), "lib"))
    import docutils