Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, July 12, 2014

Dealing with different python versions


One can install multiple python versions in unix based operating sytems such as Mac and Linux

To work on a specific python version one can simply use the commands - python'version' in the terminals

Examples:

python2.6 mycode.py would run python 2.6
python3 mycode.py would run python version3.*


Similarly for specific library installation using pip on can directly call 

pip2.6 or pip-2.6 (respectively depending on using pip version 1.5 or lower)


The syntax for using easy_install is also similar:

sudo easy_install2.6 package
sudo easy_install package (would install the package for the default python)


Also to change the version of python one can simply create an alias for the command 'python' to the version of choice in the bashrc file

open the bashrc file with your favorite editor and place the following line in the end- 
eg- vim ~/.bashrc

alias "python" "python'version' "

example: alias "python" "python2.6"


If you dont have a bashrc file in your home folder, create one and paste the code


Also never delete your original python in Linux/Mac OS because a lot of the OS software dependencies are linked to python and deleting can simply make your OS stop working


PIP installation - dealing with multiple python versions

Recently, I was facing this problem  of having multiple versions of python installed and pip installs the install library to the wrong python version

The solution to this is very simple:

Suppose you have different python versions, say:
1) Python 2.6
2) Python 2.7
3) Python 3.1

Pip supports the format pip-{version}

So to install a package for say python2.7 one can simply call

pip-2.7 install packagename

Similary for 3.1

pip-3.1 install packagename


Also for one having pip version 1.5 or higher the format would be pip{version}
that is the above commands will become

pip2.7 install packagename
pip3.1 install packagename

^notice that the '-' is missing in the above commands.