使用pip安装Mayavi时出现“no module named vtk”的错误提示

35

我想使用Mayavi。

情况

  • 我使用MacBook Air,运行OSX 10.7.3操作系统。
  • 我使用Python 2.7.2。
  • 我已经安装了virtualenv,但是没有用它来进行这个安装。

我已经手动安装了所有依赖项,包括VTK。

对于VTK,我按照其安装指南安装了python包装器。

现在,如果我打开一个新的终端窗口并打开Python,我可以导入vtk而不会出现任何错误。

问题

我试图使用以下命令安装Mayavi:

pip install mayavi

但是出现了以下错误:
Downloading/unpacking mayavi
  Running setup.py egg_info for package mayavi
    build_src
    building extension "tvtk.array_ext" sources
    building data_files sources
    build_src: building npy-pkg config files

    no previously-included directories found matching 'artwork'
    no previously-included directories found matching 'docs/pdf'
Requirement already satisfied (use --upgrade to upgrade): apptools in /Library/Python/2.7/site-packages (from mayavi)
Requirement already satisfied (use --upgrade to upgrade): traits in /Library/Python/2.7/site-packages (from mayavi)
Requirement already satisfied (use --upgrade to upgrade): traitsui in /Library/Python/2.7/site-packages (from mayavi)
Requirement already satisfied (use --upgrade to upgrade): configobj in /Library/Python/2.7/site-packages (from apptools->mayavi)
Requirement already satisfied (use --upgrade to upgrade): pyface in /Library/Python/2.7/site-packages (from traitsui->mayavi)
Installing collected packages: mayavi
  Running setup.py install for mayavi
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/Yasser/build/mayavi/setup.py", line 498, in <module>
        **config
      File "/Library/Python/2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.7-intel.egg/numpy/distutils/core.py", line 186, in setup
        return old_setup(**new_attr)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup
        dist.run_commands()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
        self.run_command(cmd)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "/Library/Python/2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.7-intel.egg/numpy/distutils/command/install.py", line 57, in run
        r = self.setuptools_run()
      File "/Library/Python/2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.7-intel.egg/numpy/distutils/command/install.py", line 31, in setuptools_run
        return old_install_mod._install.run(self)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 573, in run
        self.run_command('build')
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 326, in run_command
        self.distribution.run_command(command)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "/Users/Yasser/build/mayavi/setup.py", line 303, in run
        build_tvtk_classes_zip()
      File "/Users/Yasser/build/mayavi/setup.py", line 290, in build_tvtk_classes_zip
        gen_tvtk_classes_zip()
      File "tvtk/setup.py", line 46, in gen_tvtk_classes_zip
        from code_gen import TVTKGenerator
      File "tvtk/code_gen.py", line 10, in <module>
        import vtk
    ImportError: No module named vtk
    Complete output from command /usr/bin/python -c "import setuptools;__file__='/Users/Yasser/build/mayavi/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/53/_trhc_0x0fb6kvx6bw_wrk500000gn/T/pip-Z4hpsW-record/install-record.txt:
    running install

running build

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Users/Yasser/build/mayavi/setup.py", line 498, in <module>

    **config

  File "/Library/Python/2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.7-intel.egg/numpy/distutils/core.py", line 186, in setup

    return old_setup(**new_attr)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 152, in setup

    dist.run_commands()

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands

    self.run_command(cmd)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command

    cmd_obj.run()

  File "/Library/Python/2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.7-intel.egg/numpy/distutils/command/install.py", line 57, in run

    r = self.setuptools_run()

  File "/Library/Python/2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.7-intel.egg/numpy/distutils/command/install.py", line 31, in setuptools_run

    return old_install_mod._install.run(self)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/install.py", line 573, in run

    self.run_command('build')

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py", line 326, in run_command

    self.distribution.run_command(command)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command

    cmd_obj.run()

  File "/Users/Yasser/build/mayavi/setup.py", line 303, in run

    build_tvtk_classes_zip()

  File "/Users/Yasser/build/mayavi/setup.py", line 290, in build_tvtk_classes_zip

    gen_tvtk_classes_zip()

  File "tvtk/setup.py", line 46, in gen_tvtk_classes_zip

    from code_gen import TVTKGenerator

  File "tvtk/code_gen.py", line 10, in <module>

    import vtk

ImportError: No module named vtk

----------------------------------------
Command /usr/bin/python -c "import setuptools;__file__='/Users/Yasser/build/mayavi/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/53/_trhc_0x0fb6kvx6bw_wrk500000gn/T/pip-Z4hpsW-record/install-record.txt failed with error code 1 in /Users/Yasser/build/mayavi
Storing complete log in /Users/Yasser/Library/Logs/pip.log
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 8, in <module>
    load_entry_point('pip==1.1', 'console_scripts', 'pip')()
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main
    return command.main(args[1:], options)
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main
    log_fp = open_logfile(log_fn, 'w')
  File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile
    log_fp = open(filename, mode)
IOError: [Errno 13] Permission denied: '/Users/Yasser/Library/Logs/pip.log'

我认为问题出在vtk上。
有人能帮忙吗?
其他细节:
运行sudo pip install mayavi不能解决问题。
我想这可能有所帮助。
which python
/usr/local/bin/python

› python
Python 2.7.2 (default, Feb 17 2012, 13:57:55) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
>>> vtk
<module 'vtk' from '/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/VTK-5.10.0-py2.7.egg/vtk/__init__.pyc'>
>>> 

我的PYTHONPATH为空。

7个回答

17

我使用的是Debian,但这个方法对你也适用。

我之前在虚拟环境中安装vtk时遇到了问题,经过几个小时的搜索仍然没有结果。后来我尝试了以下方法,成功解决了问题:

  1. 从系统仓库中全局安装python-vtk

  2. 将/usr/lib/pymodules/python2.7下的vtk文件夹复制到/PATH/TO/YOUR/VIRTUALENV/lib/python2.7/site-packages下

在你的情况下,全局python包的文件夹可能不同,但你可以打开系统python并输入以下命令轻松找到它:

import vtk,sys
print(sys.modules['vtk'])

使用pip install mayavi和pip install envisage,成功将mayavi安装到我的虚拟环境中。

编辑:在第2步中,不是复制文件夹,最好是创建符号链接。


在我的Xubuntu 16.04上运行良好。 - gammapoint

12

你的 pip 可能在使用不同的 python,就像你安装了 vtk 使用了 /usr/local/bin/python,而你的 pip 二进制文件使用的是 /usr/bin/python

检查一下:

$ head -n1 $(which pip)

为了解决这个问题,你可以选择使用 virtualenv,或者使用 /usr/local/bin/python 安装 pip

curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | /usr/local/bin/python

查看更多的安装说明,请访问http://www.pip-installer.org/en/latest/installing.html


10

我曾经遇到同样的问题,第一个答案对我没有帮助,因为pip已经在正确的目录下了,但是事实证明我没有安装vtk - 运行sudo apt-get install python-vtk修复了这个问题,之后我成功地运行了pip install mayavi。


不知道为什么这个排名这么低;它非常好用。 - Astrid

4

当我在使用安装在pyenv中的python安装mayavi时遇到了同样的问题,我通过将目录复制到/home/myname/.pyenv/versions/2.7.12/lib/python2.7/site-packages来解决了这个问题,下面是我的方法:

  1. install vtk and python-vtk

    sudo apt-get install vtk6
    sudo apt-get install python-vtk
    
  2. try python -c "import vtk", if failed again, go to step 3

  3. the import error may caused by the loss of vtk package folder in your current python, so we should find the vtk folder and copy to your current python(most possible the python in pyenv shims directory)'s directory .

    the path of the vtk folder in my ubuntu is

    /usr/lib/python2.7/dist-packages/vtk
    

    just copy the vtk folder to your current python's /site-packages, e.g.

    cp -r /usr/lib/python2.7/dist-packages/vtk /home/myname/pyenv/versions/2.7.12/lib/python2.7/site-packages
    
  4. try python -c "import vtk" again.

  5. if no import error, just do

    pip install mayavi
    

有没有办法不将整个文件夹复制到虚拟环境文件夹中?我有多个Python虚拟环境,不想在每个虚拟环境中都复制整个库文件夹。 - IgNite

1

我坚持使用sudo安装了几次,最终问题得到解决,不知道该如何解释。

sudo python2 -m pip install mayavi

sudo python3 -m pip install mayavi

更新

我无法通过pip在笔记本电脑上安装。然后我通过以下方式安装了最新版本的mayavi

git clone https://github.com/enthought/mayavi.git
cd mayavi
pip install -r requirements.txt
pip install PyQt5  # replace this with any supported toolkit
python setup.py install  # or develop

安装后。使用以下内容进行测试:
mayavi2

报告显示找不到module PyQt5.Qtsvg,因此
sudo apt install python3-pyqt5.qtsvg

现在它可以工作了。

0

我在Windows10上遇到了同样的问题。结果发现,在使用pip安装mayavi之前,应该先安装numpy。


-1

我使用Homebrew安装了vtk:

$ brew install vtk

希望这有所帮助。


通常情况下,使用sudo在brew中安装任何东西都不是一个好主意。这意味着你有一个由root拥有的brew可执行文件,这是不推荐的。 - Costas B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接