Python 3.4 on Ubuntu 14.04 using virtual environments

In a previous post discussing virtualenv and virtualenvwrapper on Ubuntu 14.04 I talked about using virtual environments with the default Python version– this is Python 2.7.6 for Ubuntu 14.04. However, both Python 2.7 and 3.4 are available on Ubuntu 14.04. In this post I’ll go over setting up a virtual environment for working with Python 3 code.

To see that both versions of Python are available without additional installations try out the following at the terminal:

$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.0

For installing Python 3 packages, I’m going to install the python3-dev Ubuntu package using apt-get:

$ sudo apt-get update
$ sudo apt-get install python3-dev

This will require that you have sudo privileges, but the 4 packages that are installed will be needed to install Python 3 packages using pip.

Next, I create a virtual environment for Python3 using virtualenvwrapper, instead of virtualenv, because the environment will be put in a standard place that I have already setup and specified– see my post on virtualenv and virtualenvwrapper on Ubuntu 14.04 for more information on this aspect. I will call the environment py3 and specify that I want Python 3:

$ mkvirtualenv py3 -p /usr/bin/python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in py3/bin/python3
Also creating executable in py3/bin/python
Installing setuptools, pip...done.
(py3)$

First, notice that the prompt now indicates the name of the created virtual environment. Also notice that setuptools and pip are automatically added. Next let’s check the python version and use pip to see what’s installed:

(py3)$ python --version
Python 3.4.0
(py3)$
(py3)$ pip list
pip (1.5.6)
setuptools (3.6)
(py3)$

That all looks good. The Python version is 3.4 and only pip and setuptools are installed.

Next, let’s install some packages in py3. I’m working on a project that uses both jinja2 and pyyaml and I’d like to make sure that the package works for both Python 2.7 and 3.4. Note: if you have not installed the python3-dev Ubuntu package, as described above, the following will not work. Instead you’ll get compilation errors. The reason for this is that the code downloaded by pip needs to compile against files provided in python3-dev. Assuming, you’re ready to go, the install is easy:

(py3)$ pip install jinja2

--- edited out compiling ---

(py3)$ pip install pyyaml

--- edited out compiling ---

(py3)$
(py3)$ pip list
Jinja2 (2.7.3)
MarkupSafe (0.23)
pip (1.5.6)
PyYAML (3.11)
setuptools (3.6)

The final pip list shows that the desired Python packages, in addition to MarkupSafe that is required by jinja2, are installed.

Finally, you can deactivate the environment using:

(py3)$ deactivate
$

and you’re back to the normal environment and using Python 2.7.6. The next time the py3 environment is needed starting it up is as simple as:

$ workon py3
(py3)$

and you’re all set.

As always please leave comments, suggestions and questions...anything that will be helpful to me or other readers.

Tags

api [1]   arduino [1]   audio [2]   audio features [1]   babel [1]   Bayesian [7]   Beta [1]   blog setup [1]   bootstrap [1]   bottleneck [1]   c++ [1]   caret [1]   cmpy [1]   conditional probability [6]   coursera [1]   coursera intro to data science [3]   css [1]   cython [1]   d3 [2]   decision trees [2]   diy [1]   dropbox [1]   dsp [1]   e1071 [1]   essentia [1]   garmin [1]   geojson [1]   ggplot2 [1]   gis [2]   git [1]   gnuplot [1]   graphs [1]   html5 [1]   igraph [1]   ipython [1]   javascript [7]   joint probability [6]   json [1]   LaTeX [2]   LDA [1]   Lea [2]   machine learning [3]   marginal probability [6]   matplotlib [1]   meteor [2]   mir [1]   MongoDB [3]   music [2]   my python setup [5]   my ubuntu setup [10]   mysql [3]   networks [1]   networkx [1]   nodejs [5]   npm [3]   numexpr [1]   numpy [1]   octave [1]   Open Oakland [2]   openpyxl [1]   pandas [3]   patsy [1]   pip [2]   pweave [1]   pygraphviz [1]   pymc [1]   PySoundFile [2]   python [15]   Python [1]   python 2.7 [5]   python 3.4 [2]   pyyaml [1]   qgis [1]   R [1]   randomForest [1]   restview [1]   resume [1]   rpart [1]   running [1]   scikit-learn [3]   scipy [1]   screen [1]   server setup [1]   shapefile [1]   social networks [1]   Socrata [1]   sound [2]   sphinx [1]   sql [4]   sqlite3 [1]   ssh [1]   ssh keys [1]   statsmodels [1]   supervised learning [2]   sympy [1]   tableau [1]   tinkerer [2]   topic models [1]   tree [1]   ubuntu 14.04 [13]   Ubuntu 14.04 [3]   ubuntu 16.04 [4]   vim [2]   virtualbox [1]   virtualenv [4]   virtualenvwrapper [3]   VPS [1]   vundle [1]   webpack [1]   yaml [1]