在带有 PyQT5 的 macOS 上使用 Mayavi

Using Mayavi on macOS with PyQT5

我尝试使用以下方法在 macOS(当前 运行 Sierra)上设置 Mayavi:

brew install --with-qt5 vtk 
brew install mayavi

这在理论上应该可行,但现在当我尝试在我的代码中使用 Mayavi 时,出现以下错误:

Traceback (most recent call last):   
  File "test.py", line 32, in <module>
    mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))   
  File "/usr/local/lib/python2.7/site-packages/mayavi/tools/figure.py", line 63, in figure
    engine = get_engine()   
  File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 101, in get_engine
    return self.new_engine()   
  File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 146, in new_engine
    check_backend()   
  File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 49, in check_backend
    ''') 
ImportError: Could not import backend for traits
_______________________________________________________________________________ 

Make sure that you have either the TraitsBackendWx or the TraitsBackendQt 
projects installed. If you installed Mayavi with easy_install, try 
easy_install <pkg_name>. easy_install Mayavi[app] will also work.

If you performed a source checkout, be sure to run 'python setup.py install' 
in Traits, TraitsGUI, and the Traits backend of your choice.

Also make sure that either wxPython or PyQT is installed. wxPython:
http://www.wxpython.org/ PyQT: 
http://www.riverbankcomputing.co.uk/software/pyqt/intro

我认为错误在于 Homebrew 仅支持 Qt5 和 PyQT5,而 Mayavi 在后台寻找 PyQT4。有没有办法让 Mayavi 与 PyQT5 一起工作,或者有没有办法用 brew 安装 PyQT4?

当然问题可能出在其他地方....

谢谢!

另一种方法是使用 wx 而不是 qt,请参阅此处的文档:docs.enthought.com/mayavi/mayavi/installation.html

然后,设置环境变量export ETS_TOOLKIT=wx - 值得一试

我不建议使用 wx;我 运行 遇到了一堆问题,无法将其转化为 运行 Mayavi 示例(更不用说 incompatibility with virtual environments). And from the mayavi repo:

Mayavi itself should work with the new wxPython 4.x. However, traitsui, pyface, and other ETS packages do not yet support it so the UI will not work correctly. Older versions should work. PyQt/PySide should work largely out of the box.

以下是如何在 macOS Sierra+ 上安装带 PyQt4 的 Mayavi

brew install vtk
brew tap cartr/qt4
brew tap-pin cartr/qt4
brew install cartr/qt4/qt
brew install cartr/qt4/pyqt
brew install pyside
pip install mayavi

这些步骤确保您获得 v4,而不是默认的 v5。您可能想要 unset ETS_TOOLKIT 以确保 mayavi 不会寻找不同的后端。

FWIW PyQt5 对于 Python 2x 不可用(至少作为 PyPI 包)——我看到你是 运行ning Python 2.7。您可以 pip3 PyQt5,但 mayavi 依赖项需要为 Python 3 进行额外设置——特别是 pip3 install traitsui.