Spyder提示ValueError: API 'QString' has already been set to version 1

2013年02月27日 分类:学习笔记Python

在IPython中使用enthought.mayavi正常,但在Spyder中确有问题,提示如下

Traceback (most recent call last):
  File "G:\python\STD\mayavi_mlab.py", line 9, in <module>
    from enthought.mayavi import mlab
  File "C:\Python27\lib\site-packages\enthought\mayavi\mlab.py", line 3, in <module>
    from mayavi.mlab import *
  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

问题出在import这里,

from enthought.mayavi import mlab

因为Enthought在调用PyQt时使用的是API #2,而Spyder默认使用API #1,把他改为API #2即可。

打开Tools -> Preferences,左边选择Console,右边打开External modules选项卡,在下面找到PyQt,把里面的API selection for QString and QVariant object:改为API #2,确定,再次运行时就正常了。

作者:wuyuan 本文来自Wuyuan's Blog 转载请注明,谢谢! 文章地址: https://wuyuans.com/2013/02/spyder-valueerror-api-qstring-has-already-been-set-to-version-1