Python 2, Python 3 or both ?

Python programs are generally expected to run slower than Java programs, but they also take much less time to develop. Python programs are typically 3-5 times shorter than equivalent Java programs. It is often 5-10 times shorter than equivalent C++ code! Anecdotal evidence suggests that one Python programmer can finish in two months what two C++ programmers can’t complete in a year. Check more here.

Moving on python 2.7 is the standard version used by most devs in this world. But the latest is 3.5, we usually used to use 3.4 as the most stable version. There are many changes that has been introduced in python 3.X, you can check here. Some of the most noticeable changes are print, range/xrange and introduction to some new libraries. There is another feature that is introduced in python that the devs do not interact on daily basis is the GIC(Global Interpreter Lock) . It handles the lock better than previous versions, adn that’s why it’s ridiculously fast.

After check all this i’m tempted to use 3.x in my project and in all upcoming projects, But there is always a BUT. How do i migrate python 2.X to 3.X specially at a situation where my code base is HUGE. Another question how do we work on python 2.7 and learn python 3.x at the same time without harming the existing code.

So here i Give you 2 Solutions. First start all your new projects with python 3.x
second solution if you are working in an existing project directly migrate to python 3.x and your code will break. Yep that’s a good sign. If it breaks, then go here and check how can you make the system support python 2.X and 3.X at the same time.

Ok Lets get back to the first solution. Installing 3.x in your system.With Ubuntu 14.04 and higher python 3.4 comes inbuilt but if you are working with an existing codebase with python 2.X and you want both 3.x and 2.x to run in your same system, follow the steps.

First of all install python 3.4 by typing

sudo apt-get install python3.4

Once done check the version by typing python3 -V, if it’s not showing 3.4.x assign the aliases by typing the following else you can skip the next 3 steps.

gedit ~/.bash_aliases

It will open the bash_aliases file in a new text editor. Paste the following in the editor.

alias python3=python3.4

Once done source the aliases by typing

source ~/.bash_aliases

now if you check the version by typing python3 -V it should show you the right version.

with the above conf your system should execute existing python version if you directly cann python from terminal and should execute python 3.4 if you cann python3 from terminal.

I hope your virtualenv version currently supports python 3 if not it will give you an error while creating new env with python 3. To create a new env with python3 type

virtualenv -p /path/to/python /path/to/env

/path/to/python is usually /usr/bin/python in a ubuntu system however if its different find out the right path and give there. for python3 give path to python 3.

Once you env is created and activated evoke the python shell by typing python in terminal and check the version to ensure.

That all for this post, Hope it will help. Thanks 🙂

3 thoughts on “Python 2, Python 3 or both ?

Leave a comment