Skip to content

Instantly share code, notes, and snippets.

Last active December 14, 2020 03:31
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reorx/4067217 to your computer and use it in GitHub Desktop.
Save reorx/4067217 to your computer and use it in GitHub Desktop.
Python Deployment

This document is still a scratch

Python Deployment

Setup A Workplace

You’ll want a directory to do this in so that you don’t screw up your machine. Here’s what I did:

$ cd ~/
$ mkdir -p Envs/Python && cd Envs/Python
$ export DEPLOY=$PWD

That last bit is so you can refer to this deployment directory with $DEPLOY (which I’ll be using in the instructions from now on).

Get Python

You’ll probably want to get 2.7 for your deployment, let's download the source code first:

$ cd /tmp
$ wget

Install Dependences

Type this command to automatically install some dependences:

$ sudo apt-get build-dep python2.7

Make sure you bring in other -dev packages that are listed in follow:

  • python-dev
  • libncurses5-dev
  • libsqlite3-dev
  • libbz2-dev
  • libreadline-dev
  • libdb4.8-dev
  • tcl8.5-dev,tk8.5-dev

These packages are not shipped in early ubuntu versions (like 10.04), do a check to make sure:

  • libssl-dev
  • libexpat1-dev
  • libreadline6-dev
  • libgtk2.0-dev

For lxml if you want to install it later:

  • libxml2-dev
  • libxslt1-dev

For MySQLdb (which is acturally named MySQL-python in pypi) if you want to install it later

  • libmysqlclient-dev

The final result of ./configure --prefix=$DEPLOY may looks like follows: :

Python build finished, but the necessary bits to build these modules were not found:
_tkinter           bsddb185           dl
gdbm               imageop            sunaudiodev
To find the necessary bits, look in in detect_modules() for the module's name.

It will show you the modules that can not be build, note that some of them are unnecessary or deprecated:

  • bsddb185: Older version of Oracle Berkeley DB. Undocumented. Install version 4.8 instead.
  • dl: For 32-bit machines. Deprecated. Use ctypes instead.
  • imageop: For 32-bit machines. Deprecated. Use PIL instead.
  • sunaudiodev: For Sun hardware. Deprecated.
  • _tkinter: For tkinter graphy library, unnecessary if you don't develop tkinter programs.

Make Install Python

$ tar -xvf Python-2.7.4.tgz
$ cd Python-2.7.4
$ ./configure —prefix=$DEPLOY
$ make
$ make install

After this you will have a bunch of new directories in $DEPLOY:

$ ls $DEPLOY
bin     include lib     share   source

Let's make a test by put this new bin directory into your $PATH:

$ export PATH=$DEPLOY/bin:$PATH

then you just try it out to make sure that you have the right one:

$ which python

$ python
Python 2.7.4 (default, Nov 27 2012, 22:30:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Operating system fanatics will scoff at putting the python install in this directory, so if you want you can just install it to the default /usr/local on your system and deal with all the various clashes and conflicts you’ll have, especially if you are on an MacOSX machine.

Install virtualenv and virtualenvwrapper

Now we need to create a “virtual environment” to install all your software. To do this we’ll need easy_install installed to your $DEPLOY directory:

sh setuptools-0.6c11-py2.7.egg


or (the latest):

wget -O - | python


if path in output information is not match with $DEPLOY, add --prefix=$DEPLOY after the command

Install pip:

easy_install pip

As you can see, you now have a clean install of easy_install in your fresh $DEPLOY/bin directory for you to use. Now you need to install virtualenv:

$ pip install virtualenv
$ which virtualenv

Make sure you use --prefix $DEPLOY above or you’ll install things into the default system setup even though easy_install is clearly and obviously running from a Python in a totally different location so easy_install should know that.

$ pip install virtualenvwrapper

if you follow the step of kubuntu_deployment_for_pc.rst the installaion is done.

if not, you should add some configurations in shell environment, paste the following code to your .zshrc or .bashrc:

export PYTHONENV=$HOME/Envs/Python
export PYTHONSTARTUP=$HOME/.pystartup

# for virtualenvwrapper
export WORKON_HOME=$PYTHONENV/virtualenvs
export PROJECT_HOME=$HOME/workspace/current
source $PYTHONENV/bin/

restart your shell, now following commands are avaliable, test arould:

$ mkvirtualenv TORNADO
$ pip install tornado
$ cdsitepackages


These tools should work under system python environment, so let's make easy_install and pip available in normal situation:

$ sudo sh setuptools-0.6c11-py2.7.egg
$ sudo easy_install pip


ipython is a full-stack python shell, for a quick install you can just:

$ sudo apt-get install ipython ipython-notebook ipython-qtconsole






Copy link

vickyi commented Jul 31, 2013


Copy link

aresowj commented Jul 21, 2016

Thanks, saved me a lot of time on the starting steps.

Copy link


Copy link

very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment