Informatique / Python / (11/03/2018) Build Python 2.7.x/3.6.x with Mayavi 4.5.0 on Ubuntu 16.04 LTS
In this section I explain how to install Mayavi 4.5.0 on Python 2.7.x or Python 3.6.x on Ubuntu 16.04
LTS.
The steps are the following :
- installing some required packages
- compiling and installing Python
- compiling and installing VTK Python wrapper
- compiling and installing PyQt4 Python package
- installing Mayavi 4.5.0 Python package
Required packages
On my Ubuntu 16.04 LTS computer, I need to install (as root) the folowing packages to compile
Python
sudo apt install libncurses5-dev libreadline-dev libsqlite3-dev libbz2-dev tix-dev \
libssl-dev libgdbm-dev liblzma-dev libffi-dev tk8.5-dev
Python : compilation and installation
I assume the installation directory /fcopt I choose is writeable (for me), otherwise one have to install as root.
The directory ~/compil is used as working directory.
PYVER=3.6.4 DIR=/fcopt PYDIR=$DIR/PYTHON/$PYVER mkdir -p ~/compil cd ~/compil
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
- The option –enable-shared is needed when building VTK python wrapper.
- The option –enable-optimizations ensure to have an optimized version of Python. This option is
time consuming as it requires running a lot of tests (approximatively 30 minutes on my computer).
To quickly try Python compilation process, do not use this option !
- The option –with-ensurepip=install permits the installation of setuptools and pip packages.
In the following example PYDIR variable contains /fcopt/PYTHON/3.6.4.
Thereafter the make command is used by using 8 threads (ususally use as maximum the number of core of your
computer)
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 !). There are some other missing
modules which can be found and the Ubuntu packages to install with sudo apt install command :
_tkinter | tk8.5-dev |
_gdbm and _dbm | libgdbm-dev |
_sqlite3 | libsqlite3-dev sqlite3 |
_lzma | liblzma-dev |
If some Ubuntu 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
Installed versions 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:0:3}/site-packages
export LD_LIBRARY_PATH=$PYDIR/lib
With PYVER=3.6.4, ${PYVER:0:3} is 3.6 and ${PYVER:0:1} is 3
To test the installation, I use pip to install ipython package :
pip${PYVER:0:1} 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.
cd ~/compil wget http://www.vtk.org/files/release/8.1/VTK-8.1.0.tar.gz
tar zxvf VTK-8.1.0.tar.gz mkdir VTK-8.1.0-py$PYVER mkdir -p $DIR/VTK cd VTK-8.1.0-py$PYVER
if [ ${PYVER:0:1} -eq 2 ]; then EXT=; else EXT=m; fi ccmake -DCMAKE_INSTALL_PREFIX:PATH=$DIR/VTK/vtk8.1.0-py$PYVER \
-DBUILD_SHARED_LIBS:BOOL=ON \ -DVTK_WRAP_PYTHON:BOOL=ON \
-DVTK_PYTHON_VERSION:STRING=${PYVER:0:1} \ -DPYTHON_EXECUTABLE:PATH=$PYDIR/bin/python${PYVER:0:1} \
-DPYTHON_INCLUDE_DIR:PATH=$PYDIR/include/python${PYVER:0:3}$EXT \
-DPYTHON_LIBRARY:PATH=$PYDIR/lib/libpython${PYVER:0:3}$EXT.so \ ../VTK-8.1.0
Then in the ccmake windows, I press :
- configure key [c] a first time and waiting one minute configuring finished...
- configure key [c] a second time and waiting 30 seconds configuring finished...
- generate key [g]
Thereafter one can run compile and install process ( 5 minutes on my computer) :
To test the VTK python wrapper, one have to add paths to the previously initialized environment variables
PYTHONPATH and LD_LIBRARY_PATH :
export PYTHONPATH=$PYTHONPATH:$DIR/VTK/vtk8.1.0-py$PYVER/lib/python${PYVER:0:3}/site-packages
export LD_LIBRARY_PATH=$DIR/VTK/vtk8.1.0-py$PYVER/lib:$LD_LIBRARY_PATH
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:0:1} sphere.py
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:0:1}" >> $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
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.
cd ~/compil wget https://sourceforge.net/projects/pyqt/files/sip/sip-4.19.8/sip-4.19.8.tar.gz
tar zxvf sip-4.19.8.tar.gz cd sip-4.19.8/ $PYTHON configure.py make -j 8 make install
Then we can install PyQt4 package (PyQt5 is not compatible with Mayavi)
cd ~/compil wget https://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-4.12.1/PyQt4_gpl_x11-4.12.1.tar.gz
tar zxf PyQt4_gpl_x11-4.12.1.tar.gz cd PyQt4_gpl_x11-4.12.1 $PYTHON configure-ng.py 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
Installing matplotlib, numpy, ...
I choose to install matplotlib package after the installation of PyQt.
pip3 install numpy scipy matplotlib
Installing mayavi
The mayavi-4.5.0 python package is now installed.
To quickly test the mayavi package, one can run the python code mayavi_boy.py (774 bytes)
.
export MPLBACKEND=Qt4Agg wget http://www.math.univ-paris13.fr/~cuvelier/docs/Informatique/Python/mayavi_boy.py
$PYTHON mayavi_boy.pyvi too
With this installation, I set the environment variables PATH, PYTHONPATH and LD_LIBRARY_PATH before to
use python3, ipython3, ... as follows
export PATH=/fcopt/python3.6.0/bin:/usr/bin:/bin
export PYTHONPATH=/fcopt/python3.6.0/lib/python3.6/site-packages:/fcopt/VTK/vtk7.0-py3.6.0/lib/python3.6/site-packages
export LD_LIBRARY_PATH=/fcopt/python3.6.0/lib:/fcopt/VTK/vtk7.0-py3.6.0/lib export MPLBACKEND=Qt4Agg
The MPLBACKEND environment variable is used to set matplotlib backend.
Troubles
- When running mayavi_boy.py script, if you have the following warning :
..../site-packages/tvtk/array_handler.py:268: FutureWarning: Conversion of the second argument of issubdtype from ‘complex‘ to ‘np.complexfloating‘ is deprecated. In future, it will be treated as ‘np.complex128 == np.dtype(complex).type‘.
assert not numpy.issubdtype(z.dtype, complex), \
then change the line 268 of file ..../site-packages/tvtk/array_handler.py by
assert not numpy.issubdtype(z.dtype, numpy.complexfloating), \
See https://github.com/enthought/mayavi/pull/588/files.
- When saving a mlab figure in pdf format , I have the following error :
Saving as a vector PS/EPS/PDF/TeX file using GL2PS is either not supported
by your version of VTK or you have not configured VTK to work with GL2PS
-- read the documentation for the vtkGL2PSExporter class.
In this section I explain how to install Mayavi 4.5.1 (development version) for Python 3 on Ubuntu 17.10 by using
PyQt 5.
To use Mayavi 4.5.0 python package, the version 4 of PyQt is needed : version 5 is not supported in this
version of Mayavi. To use PyQt version 5 one has to install Mayavi 4.5.1 (development version on march 2018),
pyface (>= 5.2.0) and traitsui (>=5.2.0)
Firstly we install (as root) python3, python3-pip and python3-env Ubuntu packages. The last one will be
used to create Python virtual environments.
sudo apt-get install python3 python3-pip python3-venv
As first step, we create a virtual environment in /python-env/python3 directory
python3 -m venv ~/python-env/python3/Mayavi
We can now load this virtual environment :
source ~/python-env/python3/Mayavi/bin/activate
All the following commands will be execute in this environment. I have somme trouble when using pip3 for
installing some packages :
error : invalid command ’bdist_wheel’ ...
To avoid this trouble, I use the –no-cache-dir option. There are the command to install some usefull packages
(for me)
pip3 install numpy scipy matplotlib ipython3 --no-cache-dir
Then we classicaly install the VTK Python wrapper, PyQt version 5 and Mayavi 4.5.0
pip3 install VTK --no-cache-dir pip3 install pyQt5 --no-cache-dir pip3 install mayavi --no-cache-dir
And finally, one can upgrade the Mayavi pyface and traitsui from their git repository
pip3 install git+https://github.com/enthought/mayavi.git pip3 install git+https://github.com/enthought/pyface.git
pip3 install git+https://github.com/enthought/traitsui.git
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 python3 mayavi_boy.py