Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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 http://python.org/ftp/python/2.7.4/Python-2.7.4.tgz

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 setup.py 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
$DEPLOY/deploy/bin/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

http://www.virtualenv.org/en/latest/

http://virtualenvwrapper.readthedocs.org/en/latest/

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:

wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
sh setuptools-0.6c11-py2.7.egg

...

or (the latest):

wget https://bitbucket.org/pypa/setuptools/raw/0.8/ez_setup.py -O - | python

Note:

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
$DEPLOY/bin/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 VIRTUALENVWRAPPER_PYTHON=$PYTHONENV/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=$PYTHONENV/bin/virtualenv
export WORKON_HOME=$PYTHONENV/virtualenvs
export PROJECT_HOME=$HOME/workspace/current
source $PYTHONENV/bin/virtualenvwrapper.sh

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

$ mkvirtualenv TORNADO
$ pip install tornado
$ cdsitepackages

Tools

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

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

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

bpython

autopep8

flake8

pip-tools

References

http://www.kelvinwong.ca/2010/08/02/python-2-7-on-dreamhost/

http://lamsonproject.org/docs/deploying_lamson.html

@vickyi

This comment has been minimized.

Show comment Hide comment
@vickyi

vickyi Jul 31, 2013

nice

vickyi commented Jul 31, 2013

nice

@aresowj

This comment has been minimized.

Show comment Hide comment
@aresowj

aresowj Jul 21, 2016

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

aresowj commented Jul 21, 2016

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

@ducta-qc

This comment has been minimized.

Show comment Hide comment
@ducta-qc

ducta-qc Apr 13, 2017

Thanks

Thanks

@JiHyeongSeo

This comment has been minimized.

Show comment Hide comment
@JiHyeongSeo

JiHyeongSeo Sep 21, 2017

very nice

very nice

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