Installing Mayavi with Python 3

After a few hours I finally managed to install Mayavi in a virtual conda environment, so now I’m able to use it with Jupyter.

Installing Mayavi on a Python 2 environment only takes a simple

pip install mayavi

But unfortunately this is still not supported in Python 3, so we have to figure out a workaround.

This post is really geeky, we are going to compile some code from a terminal and install a few dependencies. If you follow it step by step it should work, but it’s better if you understand what you are doing.

Shout out to the Vegan and Depressed guy whose blog post was the main source for this how-to.

Installing Dependencies

I did this on Ubuntu 16.04. My anaconda distribution is installed on the folder anaconda2 and it’s located in my home directory.

Let’s begin installing everything we need for the compilation to work. You can either do it from your package manager or a terminal:

sudo apt-get install cmake libxt-dev libqt5svg5-dev mesa-common-dev git

Creating a Conda Environment

Let’s assume you already have anaconda installed and working, otherwise check their documentation.

The first step is to create a virtual environment. Open a terminal and run the next commands

conda create --name mayavienv python=3 numpy
source activate mayavienv

Installing PyQT4

If you install PyQT with conda or pip it won’t work, you have to compile it from source. Go to your environment directory and create a src folder to download everything you need.

cd $HOME/anaconda2/envs/mayavienv/
mkdir src && cd $_

First you need to download SIP, which is a dependency for PyQT. So go to their download page, get the latest version, save it in the src directory we just created and decompress the file. You can also do that from a terminal:

tar -xvzf sip-4.19.tar.gz
cd sip-4.19/

Configure and install:

make install

Now you are ready to install PyQT4. Get the latest version from their site, save it into the src directory and decompress it. From the terminal this should do the trick:

cd $HOME/anaconda2/envs/mayavienv/src/
tar -xvzf PyQt4_gpl_x11-4.12.tar.gz
cd PyQt4_gpl_x11-4.12/

Again, configure and install

make install

Installing VTK

VTK is a Visualization Toolkit that serves as backend from Mayavi. We are going to download the source code from github and compile it.

cd $HOME/anaconda2/envs/mayavienv/src/
git clone
mkdir build-VTK && cd $_
     cd $HOME/anaconda2/envs/mayavienv/src/
     git clone
     mkdir build-VTK && cd $_
     cmake -D CAMAKE_BUILD_TYPE=Release \
           -D CMAKE_INSTALL_PREFIX=$HOME/anaconda2/envs/mayavienv/ \
           -D VTK_WRAP_PYTHON=ON \
           -D VTK_PYTHON_VERSION=3 \
           -D VTK_INSTALL_PYTHON_MODULE_DIR=$HOME/anaconda2/envs/mayavienv/lib/python3.6/site-packages \
           -D PYTHON_EXECUTABLE=$HOME/anaconda2/envs/mayavienv/bin/python3 \
     make install

Now go and get some fresh air, talk to your beloved ones or do some exercise. You spend way too much time in front of a computer screen. The VTK installation will take about 3 hours to complete.

Installing Mayavi

Everything is set up to install Mayavi the easy way.

pip install mayavi

And that’s it, you have successfully installed Mayavi on a Python 3 environment. Let’s test it.

Testing the Installation

You can use iPython or just write a plain script and run it. I will test in in a Jupyter notebook, but first I have to install Jupyter.

conda install jupyter

Now run the notebook, it will automatically open in your web browser

jupyter notebook

Create a new Python 3 notebook and run this:

from mayavi import mlab
import os

os.environ['ETS_TOOLKIT'] = 'qt4'
%gui qt


And you should see a beautiful molecule.


Unfortunately the Jupyter extension for Mayavi is not working, so the plots display in a separate window instead of the notebook itself.