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 lineFirst 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
also, workon will cd you into the project directory automagically.
list all your environments issuing:
$lsvirtualenv
start working on ENV:
$cd ENV
$source bin/activate
$cd ENV
$source bin/activate
do some stuff
...
...
exit ENV:
$deactivate
$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
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.
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.