[ACCEPTED]-switch versions of python-ubuntu-9.04

Accepted answer
Score: 31

Use Virtualenv.

There is more information here: Working with virtualenv.

Using 22 virtualenv you can create a new virtual 21 python environment with whatever version 20 of Python you want for each project or application. You 19 can then activate the appropriate environment 18 when you need it.

To expand on my answer:

You can install multiple 17 versions of Python on your computer (I have 16 2.4, 2.5, 2.6 and 3.1 on my machine - I 15 install each from source). I use a Mac, and 14 keep my system Python as whatever OS X sets 13 as the default.

I use easy_install to install packages. On 12 ubuntu you can get easy_install like this:

sudo apt-get install python-setuptools

To 11 install virtualenv then do:

easy_install virtualenv

I tend to create 10 a new virtualenv for each project I'm working 9 on and don't give it access to the global 8 site-packages. This keeps all the packages 7 tight together and allows me to have the 6 specific versions of everything I need.

virtualenv -p python2.6 --no-site-packages ~/env/NEW_DJANGO_PROJECT

And 5 then whenever I am doing anything related 4 to this project I activate it:

source ~/env/NEW_DJANGO_PROJECT/bin/activate

If I run python 3 now it uses this new python. If I use easy_install 2 it installs things into my new virtual environment.

So, virtualenv 1 should be able to solve all of your problems.

Score: 6

Pythonbrew is a magical tool. Which can 50 also be called as Python version manager 49 similar to that of RVM-Ruby version manager 48 but Pythonbrew is inspired by Perlbrew.

Pythonbrew 47 is a program to automate the building and 46 installation of Python in the users $HOME.

 Dependencies – curl

Before 45 Installing the Pythonbrew, Install “curl” in 44 the machine, to install curl use the below 43 command in the terminal, give the the password 42 for the user when prompted.

 $sudo apt-get install curl

After Installing 41 the curl, Now Install Pythonbrew, copy and 40 paste the following commands in the terminal 39 and type the password for the user when 38 prompted.

Recomended method of installation 37 - Easy Install

 $ sudo easy_install pythonbrew 

To complete the installation, type 36 the following command


Alternate method of 35 installation:

Use curl command to download 34 the latest version of pythonbrew from github.

curl -kLO http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install

After 33 downloading, change “pythonbrew-install” to 32 “executable”

 chmod +x pythonbrew-install

Then, run the pythonbrew-install 31 in the terminal


Now the Pythonbrew has been 30 installed in the “Home Directory” i.e., /home/user/.pythonbrew

Next, copy and paste 29 the following line to the end of ~/.bashrc

*NOTE: change 28 “user” to your user name in the system

source /home/user/.pythonbrew/etc/bashrc

Thats it! Close 27 the terminal. Steps to Install different 26 versions of Python:

Open a new terminal, type 25 the following command or copy and paste 24 it.

$pythonbrew install 2.6.6

This will install Python 2.6.6 and to 23 install Python 2.7 or Python 3.2, change 22 the version number in the previous command.

$pythonbrew install 2.7


$pythonbrew install 3.2

Update: If 21 you get error while Installing then Install 20 using the below command.

$pythonbrew install --force 2.7


$pythonbrew install --force 3.2

How to manage 19 different versions of Python installed in 18 system

For instance, if Python 2.6.6, Python 2.7 and Python 3.2 is 17 installed in your system, switching between 16 the versions can be done as follows:

By default, Python 2.6.6 will 15 be active and in order to switch to Python 14 2.7 use the below command

$pythonbrew switch 2.7

The default Python 13 is changed to Python 2.7.

Now, to switch 12 to Python 3.2 change the version number 11 in the previous command.

$pythonbrew switch 3.2

Use the below command 10 to check or list the installed Python versions

$pythonbrew list

Use 9 the below command to check or list the available 8 Python Versions to install

$pythonbrew list -k

To uninstall any 7 of the installed Python version (for example 6 to uninstall Python 2.7), use the below 5 command.

$pythonbrew uninstall 2.7

Use the below command to update 4 the Pythonbrew

$pythonbrew update

Use the below command to disable the 3 Pythonbrew and to activate the default version

$pythonbrew off

Enjoy 2 the experience of installing multiple versions 1 of Python in single Linux / ubuntu machine!

Score: 4

I find http://github.com/utahta/pythonbrew much easier to install and use than 5 any other solution.

Just install it and you'll 4 have these options:

pythonbrew install 2.7.2
pythonbrew use 2.7.2 # use 2.7.2 for a current terminal session
pythonbrew switch 2.7.2 # use 2.7.2 by default system wide
pythonbrew uninstall 2.7.2

Note: if you're using a Linux-based 3 operating system with preinstalled Python, switching 2 (system wide) to another version can make 1 things go wrong, so be careful.

Score: 3

A more grassroot approach than Virtualenv 13 is the side-by-side installation of two 12 Python versions.

If there is an existing 11 installation, and you want a second installation 10 into the same root path (e.g. /usr/local), use 9 this target when making install:

make altinstall

When your 8 second installation is Python 2.6, this 7 will leave you with a /usr/local/bin/python2.6 6 alongside the old /usr/local/bin/python.

A 5 simple way to switch between these two versions 4 is using a shell alias (alias python=/usr/local/bin/python2.6) on 3 the shell where you invoke the interpreter. But 2 this won't work across sub-shells and she-bang 1 invocations.

Score: 2

pyenv is yet another Python manager. The README.md 6 at that link has a good set of instructions, but 5 they basically are:

$ cd
$ git clone git://github.com/yyuu/pyenv.git .pyenv

Then set up your $PATH.

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

Install 4 the desired versions of Python:

$ pyenv install 2.7.8

After installing 3 you need to run this:

$ pyenv rehash

Then switch to the 2 version of Python you want to run, for the 1 shell:

$ pyenv shell 2.7.8
Score: 1

"Question1: How will i tell any framework 27 that go and use version so and so pf python 26 like day django to use 2.6 and say mjango 25 to use 2.4?"

You simply run them with the 24 specific python version they need. Run mjango 23 with /usr/bin/python2.4 and django with 22 /usr/bin/python2.6. As easy as that.

"Question2: Is 21 there more elegant way to switch between 20 version as my hack of symlinking was a virtual 19 disaster?"

Yes, see above. Have two separate 18 installs of Python, and run explicitly with 17 the different versions.

"Question3: Can I 16 download a deb for say hardy and make jaunty 15 believe its for her?"

That generally works. If 14 it doesn't, it's because it has dependencies 13 that exist in Hardy, and does not exist 12 in Jaunty, and then you can't.

And here is 11 a Question 4 you didn't ask, but should 10 have. ;)

"Is there an easier way to download 9 all those Python modules?"

Yes, there is. Install 8 setuptools, and use easy_install. It will 7 not help you with library dependecies for 6 those Python modules that have C code and 5 need to be compiled. But it will help with all 4 others. easy_install will download and 3 install all the Python dependencies of the 2 module in question in one go. That makes 1 it a lot quicker to install Python modules.

Score: 0

Move to the project directory :

Create an 3 environment : virtualenv -p python2.7 --no-site-packages 2 ~/env/twoseven

Then activate your source 1 : source ~/env/twoseven/bin/activate

More Related questions