*  Informatique / Python / Building Python 2.7.x/3.6.x with Mayavi 4.5.0 on centOS 7

        * Required packages
        * Python : compilation and installation
        * Installing VTK
        * Installing PyQt
        * Installing matplotlib, numpy, ...
        * Installing mayavi

pict pict

In this section I explain how to install Mayavi 4.5.0 on Python 2.7.x, 3.5.x or 3.6.x (not tested with other versions) on CentOS 7.
The steps are the following :

The installation process described here was succesfully tested with Python 2.7.14, 3.5.5 and 3.6.0.

*  Required packages

On my CentOS 7 computer, I need to install (as root) the following packages to compile Python/VTK/PyQt4

sudo yum install gcc gcc-c++ gcc-gfortran ncurses-devel tk-devel gdbm-devel \
         readline-devel sqlite-devel bzip2-devel openssl-devel xz-devel

To compile VTK, one must use cmake version 3 but only version 2.8.12 can be installed by default.

For VTK compilation some other packages are required :

sudo yum install mesa-libGL-devel libXt-devel

For PyQt version 4, the package qt4-devel must be installed
sudo yum install qt4-devel

*  Python : compilation and installation

I assume the installation directory to be /fcopt. This directory is writable for me, otherwise one has to be root for install process. The directory ~/compil is used as working directory.
The following compilation/installation commands are given for Python 3.6.4 version. To use another version, one only has to set PYVER variable. The PYDIR variable contains /fcopt/PYTHON/3.6.4 which is the installation directory and the CDIR variable contains ~/compil which is the compilation directory.

PYVER=3.6.4
CDIR=~/compil
DIR=/fcopt
PYDIR=$DIR/PYTHON/$PYVER
mkdir -p $CDIR
cd $CDIR
wget https://www.python.org/ftp/python/$PYVER/Python-$PYVER.tar.xz
tar -Jxf Python-$PYVER.tar.xz
cd Python-$PYVER
./configure --enable-shared --enable-optimizations  --with-ensurepip=install --prefix=$PYDIR

Thereafter the make command is used by using 8 threads (ususally use as maximum the number of core of your computer)

 make -j 8

it is important to ensure that there are no important missing modules by reading last lines (before tests starting with –enable-optimizations option) of the output of previous make command. There is what I obtain on my computer :

...
Python build finished, but the necessary bits to build these modules were not found:
_bsddb             bsddb185           dl
imageop            sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.
...

These missing modules are not not needed in the following (and they are obsolete !). Some other missing modules can be found. There are some of them and the corresponding CentOS packages to install with sudo yum install <package_name> command :

missing modules packages


_tkinter tk-devel
_gdbm and _dbm gdbm-devel
_sqlite3 sqlite-devel
_lzma xz-devel

If some CentOS packages are installed to prevent missing modules one has to rerun the configure command the make command.
At last one can install Python under $PYDIR directory

make install

On March 2018, packages installed with this Python installation are pip-9.0.1 and setuptools-28.8.0.
To correctly use and test this installed python version
export PATH=$PYDIR/bin:/usr/bin:/bin
export PYTHONPATH=$PYDIR/lib/python${PYVER%.*}/site-packages
export LD_LIBRARY_PATH=$PYDIR/lib

With PYVER=3.6.4, ${PYVER%.*} is 3.6 and ${PYVER%%.*} is 3.
To test the installation, I use pip to install ipython package :
pip${PYVER%%.*} install ipython

The mayavi package requires VTK (with ccmake command) and PyQt package to be installed.

*  Installing VTK

On march 2018, The lastest version of VTK is 8.1.0. I choose as installation directory /fcopt/VTK/vtk8.1.0-py$PYVER which is writeable for me. In the following commands, the DIR, CDIR, PYVER, PYDIR variables are those used for the previously Python installation. There are some remarks on the following commands :

cd $CDIR
VTKVER=8.1.0
VTKDIR=$DIR/VTK/vtk$VTKVER-py$PYVER
wget http://www.vtk.org/files/release/${VTKVER%.*}/VTK-$VTKVER.tar.gz
tar zxvf VTK-$VTKVER.tar.gz
mkdir VTK-$VTKVER-py$PYVER
mkdir -p $DIR/VTK
cd VTK-$VTKVER-py$PYVER
if [ ${PYVER%%.*} -eq 2 ]; then EXT=; else EXT=m; fi
ccmake3 -DCMAKE_INSTALL_PREFIX:PATH=$VTKDIR \
     -DBUILD_SHARED_LIBS:BOOL=ON \
     -DVTK_WRAP_PYTHON:BOOL=ON \
     -DVTK_PYTHON_VERSION:STRING=${PYVER%%.*} \
     -DPYTHON_EXECUTABLE:PATH=$PYDIR/bin/python${PYVER%%.*} \
     -DPYTHON_INCLUDE_DIR:PATH=$PYDIR/include/python${PYVER%.*}$EXT \
     -DPYTHON_LIBRARY:PATH=$PYDIR/lib/libpython${PYVER%.*}$EXT.so \
     -DVTK_USE_GL2PS:BOOL=ON\
     -DVTK_LEGACY_SILENT:BOOL=ON\
     ../VTK-$VTKVER

In the ccmake3 windows, I press :

Thereafter one can run compilation and installation process ( 5 minutes on my computer) :

make -j 8
make -j 8 install

To test the VTK python wrapper, one have to add paths to the previously initialized environment variables PYTHONPATH and LD_LIBRARY_PATH :
export PATH=$PYDIR/bin:/usr/bin:/bin
export PYTHONPATH=$PYDIR/lib/python${PYVER%.*}/site-packages:$VTKDIR/lib/python${PYVER%.*}/site-packages
export LD_LIBRARY_PATH=$VTKDIR/lib:$PYDIR/lib

and run the python code sphere.py (700 bytes) :
wget http://www.math.univ-paris13.fr/~cuvelier/docs/Informatique/Python/sphere.py
$PYDIR/bin/python${PYVER%%.*} sphere.py

If you have errors as
...
ERROR: In .../Rendering/OpenGL2/vtkShaderProgram.cxx, line 430
vtkShaderProgram (0x1145600): 1: #version 120
...

Your graphic driver does not supported shader so you can try to add the option -DVTK_RENDERING_BACKEND:STRING=OpenGL to cmake3 command :
ccmake3 -DCMAKE_INSTALL_PREFIX:PATH=$VTKDIR \
     -DBUILD_SHARED_LIBS:BOOL=ON \
     -DVTK_WRAP_PYTHON:BOOL=ON \
     -DVTK_PYTHON_VERSION:STRING=${PYVER%%.*} \
     -DPYTHON_EXECUTABLE:PATH=$PYDIR/bin/python${PYVER%%.*} \
     -DPYTHON_INCLUDE_DIR:PATH=$PYDIR/include/python${PYVER%.*}$EXT \
     -DPYTHON_LIBRARY:PATH=$PYDIR/lib/libpython${PYVER%.*}$EXT.so \
     -DVTK_LEGACY_SILENT:BOOL=ON\
     -DVTK_RENDERING_BACKEND:STRING=OpenGL\
     ../VTK-$VTKVER
make -j 8 install

To save environment variables in a file one can do :
FILE=~/setmyPython$PYVER.sh
echo "export PATH=$PATH" > $FILE
echo "export PYTHONPATH=$PYTHONPATH"  >> $FILE
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> $FILE
echo "export PYTHON=$PYDIR/bin/python${PYVER%.*}" >> $FILE
echo "export MPLBACKEND=Qt4Agg" >> FILE
echo "Writing environment variables in $FILE"
echo "To load set environment variables do:"
echo "  source $FILE"

Thereafter one can source the created file to reload them in any terminal
source ~/setmyPython3.6.4.sh

*  Installing PyQt

We first have to install sip package. The environment variables PATH, PYTHONPATH and LD_LIBRARY_PATH must be correctly set and PYTHON is either python3 (version 3 of Python) or python2 (version 2 of Python) depending on previously installed version.

PYTHON=$PYDIR/bin/python${PYVER%.*}
SIPVER=4.19.8
cd $CDIR
wget https://sourceforge.net/projects/pyqt/files/sip/sip-$SIPVER/sip-$SIPVER.tar.gz
tar zxvf sip-$SIPVER.tar.gz
cd sip-$SIPVER/
$PYTHON configure.py
make -j 8
make install

Then we can install PyQt4 package (PyQt5 is not compatible with Mayavi). For my usage, I don’t need to install QT designer plugin (must be root for that) so I use the --no-designer-plugin option with configure-ng.py
QT4VER=4.12.1
cd $DIR
export PATH=$PATH:/usr/lib64/qt4/bin
wget https://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-$QT4VER/PyQt4_gpl_x11-$QT4VER.tar.gz
tar zxf PyQt4_gpl_x11-$QT4VER.tar.gz
cd PyQt4_gpl_x11-$QT4VER
$PYTHON configure-ng.py --confirm-license --no-designer-plugin
make -j 8
make install

To quickly test the PyQt4 package, one can run the python code pyqt4_window.py (339 bytes) .
export MPLBACKEND=Qt4Agg
wget http://www.math.univ-paris13.fr/~cuvelier/docs/Informatique/Python/pyqt4_window.py
$PYTHON pyqt4_window.py

In the following we suppose the environment variables PATH, PYTHONPATH, LD_LIBRARY_PATH and MPLBACKEND set as
export PATH=$PYDIR/bin:/usr/bin:/bin
export PYTHONPATH=$PYDIR/lib/python${PYVER%.*}/site-packages:$VTKDIR/lib/python${PYVER%.*}/site-packages
export LD_LIBRARY_PATH=$VTKDIR/lib:$PYDIR/lib
export MPLBACKEND=Qt4Agg

*  Installing matplotlib, numpy, ...

I choose to install matplotlib package after the installation of PyQt.

$PYDIR/bin/pip${PYVER%%.*} install numpy scipy matplotlib

*  Installing mayavi

$PYDIR/bin/pip${PYVER%%.*} install mayavi

The mayavi-4.5.0 python package is now installed (march 2018).
To quickly test the mayavi package, one can run the python code mayavi_boy.py (774 bytes) .
wget http://www.math.univ-paris13.fr/~cuvelier/docs/Informatique/Python/mayavi_boy.py
$PYTHON mayavi_boy.py