Actions: | Security

AllGoodBits.org

Navigation: Home | Services | Tools | Articles | Other

Managing Development and Deployment of Python

Recently, I have written a few small webapps for internal purposes using Flask and SQLAlchemy. Once you have something to deploy, or possibly before, it's worth working out how to do that. Here are some tools and tips I used to make this work. There are many, many alternative approaches that would be just as good, and surely some that are better. But this worked for me and will serve as a reference for how I can do this again next time.

virtualenv

Particularly in development or legacy environments, we might need to have multiple versions of our scripting language interpreters or some of the modules/packages/library code that we depend upon for our applications.

virtualenv allows us to create an isolated python environment with the version of the interpreter, libraries and installed packages that are required by the app. This means that we don't have to pay any attention whatsoever to the list of packages installed on the system as a whole, whether they become upgraded, what their dependencies are, or anything else. As I develop, I install packages as required, within the virtualenv.

Create the virtualenv

>virtualenv --no-site-package my_new_app
--no-site-packages
 do not link to existing packages installed on the system
--python=<path to python binary>
 optionally provide a specific Python interpreter

Activate your new virtualenv

>cd my_new_app
>source bin/activate

pip

Installing python packages is made easy with pip. It will also upgrade, list and uninstall python packages.

>pip install Flask
>pip freeze
Flask==0.7.2
Jinja2==2.6
Werkzeug==0.7.1
wsgiref==0.1.2

The output of pip freeze is in the format of a requirements file that can be used to specify a package list with version numbers. This can be kept in version control with your code itself. Indeed, you could create a commit hook for your version control system that automatically updates a file with the contents of pip freeze.

If you redirect the output of pip freeze to a file, it can also be used to control installation:

pip install -r packagelist.txt

You might want to consider setting up a local mirror of the Python Package Index, pypi, particularly if you have many machines to install packages onto, because this will cushion from potential failures of pypi and relieve the load on pypi.