Friday, April 18, 2014

Creating a Python environment from scratch

Managing python environments is straightforward with the right tools. We have two flavors of Python, python 2.7 (which will be supported until 2020 as Guido has stated recently) and the new recommended version 3.x. We also most probably will need to use multiple versions of a library at the same time for different projects; for example we may have a project using python 2.7 and Django 1.5 which is in maintenance and at the same time we are starting a new one using python 3 and Django 1.7.

Without extra tools managing the combinations of environments can be quite tricky. Below I describe a basic simple configuration that solves this problems and more!

First of all, install Python

On Ubuntu:

$sudo apt-get python
$sudo apt-get python3


You can install both of them, they won't collide and you will be able to choose which one to use on a project by project basis.


Install pip

You can follow instructions here to install the newest pip version, or, if you don't mind being a step or two behind, you can use the package manager of your OS to install instead. In my case, I use ubuntu so here is the apt-get line

First you can get the package info to see how out-dated the package is

$ sudo apt show python-pip

Package: python-pip
Priority: optional
Section: universe/python
Installed-Size: 479 kB
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Python Modules Team <python-modules-team@lists.alioth.debian.org>
Version: 1.5.4-1
Depends: python (>= 2.7), python (<< 2.8), python:any (>= 2.7.1-0ubuntu2), ca-certificates, python-colorama, python-distlib, python-html5lib, python-pkg-resources, python-setuptools (>= 0.6c1), python-six, python-requests
Recommends: build-essential, python-dev-all (>= 2.6)
Download-Size: 97,7 kB
Homepage: http://www.pip-installer.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
APT-Sources: http://ar.archive.ubuntu.com/ubuntu/ trusty/universe amd64 Packages
Description: alternative Python package installer
pip is a replacement for easy_install, and is intended to be an improved
Python package installer. It integrates with virtualenv, doesn't do partial
installs, can save package state for replaying, can install from non-egg
sources, and can install from version control repositories.


Current version as from site is 1.5.4, published on 2014-02-21, so it's not really behind at this time
Let's install

$sudo apt-get install python-pip

Pip will help us installing the next set of tools we need, as well as python packages for our own applications

Managing virtual environments

Virtualenv is a tool which allows us to create virtual environments where we can install whatever packages and versions we want in isolation, without impacting the rest of the system, including other virtual python environments.

$sudo pip install virtualenv

Virtual env consists in only a handful of commands which you can use to manage your environments. You can create, activate and deactivate environmets like this

create ENV:

$virtualenv [--python=path-to-python-exec] ENV

start working on ENV:

$cd ENV
$source bin/activate

do some stuff
...

exit ENV:
$deactivate

You can create bootstrap scripts to automate environment set up and other more advanced stuff

Virtualenv is simple but it gets better if you combine it with VirtualEnvWapper. An extension of virtualenv which adds wrappers for creating/deleting environments and assist in your everyday development workflow .
pip will aid with installing python specific packages.


$sudo pip install virtualenvwrapper

And now you are ready to go!

If you are starting a new project you can create both a new virtual env and the project with a simple command

$mkproject projname

Alternatively, if you want to start using virtual environments with your existing project, you can just create your env with

$mkvirtualenv mynewenv

which will be automatically activated.

and bind your existing project using

$setvirtualenvproject [virtualenv_path project_path]

To exit the environment, just type

$deactivate


And to start working on another environment:

$workon otherenv

also, workon will cd you into the project directory automagically.


list all your environments issuing:

$lsvirtualenv 

or just

$workon

withouth any params


remove an obsolete environment with

$rmvirtualenv obsoleteenv


This is just a glimpse of what VirtualEnvWrapper can do for you, I encourage you to go through the command reference here to get a better taste of it.











No comments:

Post a Comment