`How to install and run `mayavi` by `pip3` with `v

2019-06-12 10:05发布

问题:

My system is Ubuntu 16.04 and my Python version is 3.5. I have installed vtk 7.1.0 into /home/why/software/vtk/7.1.0/ with Python wrapper. Furthermore, I add the following line into .bashrc

export PYTHONPATH=$PYTHONPATH:/home/why/software/vtk/7.1.0/lib/python3.5/site-packages

And I can import vtk in ipython3 and use it normally.

But when I install mayavi by

sudo -H pip3 install mayavi

I get the following error:

  Running setup.py bdist_wheel for mayavi ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-1_ze16zr/mayavi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmp55qp27ekpip-wheel- --python-tag cp35:
  running bdist_wheel
  running build
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-build-1_ze16zr/mayavi/setup.py", line 499, in <module>
      **config
    File "/usr/lib/python3/dist-packages/numpy/distutils/core.py", line 169, in setup
      return old_setup(**new_attr)
    File "/usr/lib/python3.5/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/usr/lib/python3.5/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/usr/lib/python3/dist-packages/wheel/bdist_wheel.py", line 179, in run
      self.run_command('build')
    File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/tmp/pip-build-1_ze16zr/mayavi/setup.py", line 311, in run
      build_tvtk_classes_zip()
    File "/tmp/pip-build-1_ze16zr/mayavi/setup.py", line 297, in build_tvtk_classes_zip
      gen_tvtk_classes_zip()
    File "tvtk/setup.py", line 50, in gen_tvtk_classes_zip
      from tvtk.code_gen import TVTKGenerator
    File "/tmp/pip-build-1_ze16zr/mayavi/tvtk/code_gen.py", line 10, in <module>
      import vtk
  ImportError: No module named 'vtk'

How to fix this problem? Thanks very much for your help.

Edit:

By Mike Müller suggestion, I can install mayavi now. But I run a simple test like following:

In [1]: import numpy as np
In [2]: from mayavi import mlab
In [3]: x, y, z, value = np.random.random((4, 40))
In [4]: mlab.points3d(x, y, z, value)
Out[4]: <mayavi.modules.glyph.Glyph at 0x7f3190ffca98>

and I just get a gray dead window without any figure in it. So how to fixed this problem?

回答1:

Part 1 - Installation

If you want/need to install as root, change to root first, then export the PYTHONPATH:

sudo -s
export PYTHONPATH=$PYTHONPATH:/home/why/software/vtk/7.1.0/lib/python3.5/site-packages
pip3 install mayavi

Root does not acknowledge the environmental variables you set as a another user.

Part 2 - Mayavi

You need to call mlab.show() to show your visualization:

import numpy as np
from mayavi import mlab

x, y, z, value = np.random.random((4, 40))
mlab.points3d(x, y, z, value)
mlab.show()