Saturday, November 24, 2012

How to install an alternative version of Pyhton and use it in a virtual environment

I've decided to learn a bit more of Python. My friend @remosu recommended me to use virtual environments so that I can practice with different versions of Python.

First I installed an alternative version of Python on my Ubuntu following the first two steps in this great post by Eli Bendersky:
I installed first some required packages:
$ sudo apt-get install libreadline-dev
$ sudo apt-get install libsqlite3-dev
$ sudo apt-get install libbz2-dev
$ sudo apt-get install libssl-dev
Then I downloaded Python from http://www.python.org/, configured and built it:
$ ./configure
$ make -j
Then I stopped following Eli's post because I wanted to keep the version that was already installed on my computer instead of replacing it with a new version.

I started googling how to it and after a while I found this discussion in Stack Exchange Unix & Linux:
In there I found out that the "trick to easier installation of multiple interpreters from source" was using (thaks to vperic's answer):
$ sudo make altinstall
After doing that I had two Python versions living together in the same Ubuntu. Then I only had to use virtualenvwrapper to create my virtual environment.
First I installed virtualenv and virtualenvwrapper using pip:
$ pip install virtualenv
$ pip install virtualenvwrapper
And executed virtualenvwrapper.sh:
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
virtualenvwrapper.user_scripts creating /home/myuser/Envs/initialize
virtualenvwrapper.user_scripts creating /home/myuser/Envs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/myuser/Envs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/myuser/Envs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/myuser/Envs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/myuser/Envs/predeactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/postdeactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/preactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/postactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/get_env_details
virtualenvwrapper.user_scripts creating /home/myuser/Envs/premkproject
virtualenvwrapper.user_scripts creating /home/myuser/Envs/postmkproject
virtualenvwrapper.user_scripts creating /home/myuser/Envs/prermproject
virtualenvwrapper.user_scripts creating /home/myuser/Envs/postrmproject
Finally, I created a virtual environment with python 2.7 called learning_env:
$ mkvirtualenv --python python2.7 learning_env
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in learning_env/bin/python2.7
Also creating executable in learning_env/bin/python
Installing setuptools............................done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/myuser/Envs/learning_env/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/learning_env/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/learning_env/bin/preactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/learning_env/bin/postactivate
virtualenvwrapper.user_scripts creating /home/myuser/Envs/learning_env/bin/get_env_details
(learning_env)~$
To check that the virtual environment really had the right version of Python I did:
$ workon learning_env
(learning_env)bscuser@trikitrok:~$ python
Python 2.7.3 (default, Nov 21 2012, 01:38:50) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Then I got out of the virtual environment and checked that I still had the same version of Python on my Ubuntu:
$ workon learning_env
(learning_env)~$ deactivate
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Now I have my own learning environment to play with Python 2.7.

No comments:

Post a Comment