Why not try Python 3?

So, you’ve been using Python 2 since forever right? Well, Python 2 is still strong but you will, eventually, have to move on. The will be no Python 2.8.

Python 3 is currently on version 3.4.1 and all of  us should at least try it out. Or maybe you want to try some other “Python flavor”, like PyPy for example. Virtualenv will help us.

Sure, you might be working professionally with Python 2 and you still want that to be your default. No worries. If you’re working with Python and not using virtualenv, well… You should use it! Even if you always use the same Python version, you should use it (I will not get tired of saying this). But let’s leave the discussion about using virtualenv for some other time and just accept, for now, that you should use it.

First thing, head to the downloads section and download the latest Python version. I’m writing this on a Mac, so I’ll get the OS X version. After the installation, check, on the command line that Python 3.4 is available:

 $ python3.4 -c “import sys; print(sys.version)”

3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

Now we will create a virtual environment that will have Python 3.4 as it’s interpreter. We can achieve that by using the “-p”  parameter. But first, let’s locate the path to of your “new” Python:

$ which python3.4

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

Now that we know the path to Python 3.4, we can create our environment:

$ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /path/to/the/env

Running virtualenv with interpreter /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

Using base prefix ‘/Library/Frameworks/Python.framework/Versions/3.4’

New python executable in /Users/rcastro/.envs/test_python3.4/bin/python3.4

Also creating executable in /Users/rcastro/.envs/test_python3.4/bin/python

Installing setuptools, pip…done.

Let’s activate our new environment and check that Python 3.4 is our default:

$ source ~/.envs/test_python3.4/bin/activate

(test_python3.4)$ python -c “import sys; print (sys.version)”

3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

Excellent. This way you can even work with different versions on different projects. Cool, uh?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s