Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mayavi doesn't run from within Spyder: complains about "ValueError: API 'QString' ..."

I am unable to run/use Mayavi library from within Spyder IDE. I have described the problem below. Any help will be very useful. (Thank you very much in advance.)

Steps to reproduce the problem:

  1. Just importing the Mayavi library in a script (for example using "import mayavi.mlab as mlab") and executing the script will reproduce this problem.
  2. I am including a test code (note that this code is an example code from the Mayavi website) here to reproduce the problem:

code

from numpy import sin, cos, mgrid
import mayavi.mlab as mmlab

def f(x,y):
  return sin(x + y) + sin(2*x - y) + cos(3*x + 4*y)
        
x,y = mgrid[-7.:7.05:0.01, -5.:5.05:0.05]
z = f(x,y)
s = mmlab.contour_surf(x,y,z)
mmlab.show()

Expected output and the Error:

Expected output: A Mayavi figure window with the figure. What I see instead: The following Value error (I am including the full sequence of message in order to be explicit)in the console window:

-------------------------- Beginning of error message ------------------

Traceback (most recent call last):
  File "C:\PROGRAMSANDEXPERIMENTS\PYTHON\MayaviScripts\Learning\testMayavi.py", line 2, in <module>
    import mayavi.mlab as mmlab
  File "C:\Python27\lib\site-packages\mayavi\mlab.py", line 27, in <module>
    from mayavi.tools.camera import view, roll, yaw, pitch, move
  File "C:\Python27\lib\site-packages\mayavi\tools\camera.py", line 25, in <module>
    from engine_manager import get_engine
  File "C:\Python27\lib\site-packages\mayavi\tools\engine_manager.py", line 12, in <module>
    from mayavi.preferences.api import preference_manager
  File "C:\Python27\lib\site-packages\mayavi\preferences\api.py", line 4, in <module>
    from preference_manager import preference_manager
  File "C:\Python27\lib\site-packages\mayavi\preferences\preference_manager.py", line 29, in <module>
    from traitsui.api import View, Group, Item
  File "C:\Python27\lib\site-packages\traitsui\api.py", line 35, in <module>
    from .editors.api import (ArrayEditor, BooleanEditor, ButtonEditor,
  File "C:\Python27\lib\site-packages\traitsui\editors\__init__.py", line 22, in <module>
    from .api import (toolkit, ArrayEditor, BooleanEditor, ButtonEditor,
  File "C:\Python27\lib\site-packages\traitsui\editors\api.py", line 10, in <module>
    from .code_editor import CodeEditor
  File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 36, in <module>
    class ToolkitEditorFactory ( EditorFactory ):
  File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 48, in ToolkitEditorFactory
    mark_color = Color( 0xECE9D8 )
  File "C:\Python27\lib\site-packages\traits\traits.py", line 487, in __call__
    return self.maker_function( *args, **metadata )
  File "C:\Python27\lib\site-packages\traits\traits.py", line 1183, in Color
    return ColorTrait( *args, **metadata )
  File "C:\Python27\lib\site-packages\traitsui\toolkit_traits.py", line 7, in ColorTrait
    return toolkit().color_trait( *args, **traits )
  File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 109, in toolkit
    _toolkit = _import_toolkit(ETSConfig.toolkit)
  File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 51, in _import_toolkit
    return __import__( name, globals=globals(), level=1 ).toolkit
  File "C:\Python27\lib\site-packages\traitsui\qt4\__init__.py", line 18, in <module>
    import pyface.qt
  File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 35, in <module>
    prepare_pyqt4()
  File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 17, in prepare_pyqt4
    sip.setapi('QString', 2)
  ValueError: API 'QString' has already been set to version 1

---------------------------- End of error message --------------------

My environment(s) are the following:

This issue has been observed at-least in the following two environments:

Environment 1:

Description: Spyder and all other components were installed using the Pythonxy distribution (Py(x,y)-2.7.2.3.exe) on a Windows 7, 32 bit machine). Spyder Version: 2.1.9 Python Version: 2.7.2 (32 bit) Qt Version: 4.7.4, PyQt4 (API v1) 4.8.6 on Windows Mayavi Version: 4.1.0

Environment 2:

Description: On a separate machine running 64 bit Windows 7, I have installed 64-bit version of the Enthought EPD distribution. So it is running 2.7.3 (x64) version of Python distribution. Since the EPD package doesn't include Spyder, I manually installed Spyder after installing PyQt GPLv4.9.4.
Spyder Version: 2.1.11 Python Version: 2.7.3 (64 bit) Qt Version: 4.8.2, PyQt4 (API v2) 4.9.4 on Windows Mayavi Version: 4.2.0

Additional information:

I am sure that the code is fine, as the script produces the expected output when executed "outside" spyder environment. For example, using IPython shell.

Thank you.

like image 410
Indranil Sinharoy Avatar asked Sep 15 '12 22:09

Indranil Sinharoy


3 Answers

Thanks Avaris for your response.

I have a solution for now (I am not sure of it as a "fix"). I modified the following setting in Tools->Preferences->Console->External Modules->Enthought Tool Suite->ETS_TOOLKIT: change from Qt4 to wx. After changing this setting, I am able to execute code with Mayavi library and Mayavi plots directly from within Spyder.

like image 164
Indranil Sinharoy Avatar answered Oct 15 '22 18:10

Indranil Sinharoy


for linux users, the answer by imranal in this issues can be referenced to.

just add export ETS_TOOLKIT=qt4 to a new line in ~/.bashrc will do the trick. don't forget to source the ~/.bashrc file!

like image 32
Scott Yang Avatar answered Oct 15 '22 17:10

Scott Yang


According to this and this the error can be fixed by activating the Ignore API change errors (sip.setapi) checkbox in Preferences > Console > External Modules.

I have tested that it does work with Spyder 2.1.11.

like image 45
Jaime Avatar answered Oct 15 '22 19:10

Jaime