在Windows上:Python编辑器无法从mayavi导入mlab,命令提示符即可

时间:2016-01-19 13:21:20

标签: python-2.7 sublimetext2 spyder mayavi

我是Python的新手,但我在Windows上使用mayavi软件包时遇到了一些麻烦。我已经安装了Python(x,y),我正在使用Sublime Text 2和Spyder编辑器。到目前为止,我发现从mayavi网站运行此代码:

 $scope.refreshData = function (termObj) {
   $scope.gridOptions2.data = $scope.data;
    if (termObj.length > 2) {

        while (termObj) {
            var oSearchArray = termObj.split(' ');
            $scope.gridOptions2.data = $filter('filter')($scope.gridOptions2.data, oSearchArray[0], undefined);
            console.log($scope.gridOptions2.data)
            oSearchArray.shift();
            termObj = (oSearchArray.length !== 0) ? oSearchArray.join(' ') : '';
        }
    }
    else {
        $scope.gridOptions2.data = $scope.gridData;
        $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.OPTIONS);
    }
    $scope.$apply();
};

导致以下错误:

from numpy import pi, sin, cos, mgrid
dphi, dtheta = pi/250.0, pi/250.0
[phi,theta] = mgrid[0:pi+dphi*1.5:dphi,0:2*pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
x = r*sin(phi)*cos(theta)
y = r*cos(phi)
z = r*sin(phi)*sin(theta)
# View it.
from mayavi import mlab
s = mlab.mesh(x, y, z)
mlab.show()

奇怪的是,当在命令提示符下直接运行mayavi代码时,它确实有效!那么为什么我在Sublime Text 2和Spyder中都会出现错误?

感谢您抽出宝贵时间查看我的问题

编辑:仅在完全导入numpy而不是pi,sin,cos和mgrid命令时:错误更改为:

Traceback (most recent call last):
  File "C:\Dropbox\...\test.py", line 23, in <module>
    from mayavi import mlab
  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 36, in <module>
    from .editors.api import ArrayEditor
  File "C:\Python27\lib\site-packages\traitsui\editors\__init__.py", line 23, in <module>
    from .api import ArrayEditor
  File "C:\Python27\lib\site-packages\traitsui\editors\api.py", line 24, 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 489, in __call__
    return self.maker_function( *args, **metadata )
  File "C:\Python27\lib\site-packages\traits\traits.py", line 1203, 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 122, in toolkit
    _toolkit = _import_toolkit( toolkit_name )
  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\wx\__init__.py", line 26, in <module>
    import toolkit
  File "C:\Python27\lib\site-packages\traitsui\wx\toolkit.py", line 61, in <module>
    from helper \
  File "C:\Python27\lib\site-packages\traitsui\wx\helper.py", line 43, in <module>
    from pyface.timer.api \
  File "C:\Python27\lib\site-packages\pyface\timer\api.py", line 17, in <module>
    from .timer import Timer
  File "C:\Python27\lib\site-packages\pyface\timer\timer.py", line 8, in <module>
    from pyface.toolkit import toolkit_object
  File "C:\Python27\lib\site-packages\pyface\toolkit.py", line 73, in <module>
    _init_toolkit()
  File "C:\Python27\lib\site-packages\pyface\toolkit.py", line 45, in _init_toolkit
    be = import_toolkit(tk)
  File "C:\Python27\lib\site-packages\pyface\toolkit.py", line 31, in import_toolkit
    __import__(be + 'init')
  File "C:\Python27\lib\site-packages\pyface\ui\qt4\init.py", line 18, in <module>
    from pyface.qt import QtCore, QtGui, qt_api
  File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 33, in <module>
    prepare_pyqt4()
  File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 17, in prepare_pyqt4
    sip.setapi('QDate', 2)
ValueError: API 'QDate' has already been set to version 1
[Finished in 1.5s with exit code 1]

这使问题看起来与this question.中的问题非常相似。然而,对于Sublime Text 2来说,解决方案似乎并不可行。 Spyder解决方案对我不起作用,因为'Enthought Tool Suite'选项显示为灰色。任何帮助仍然受到赞赏!

1 个答案:

答案 0 :(得分:2)

对于遇到同样问题的其他人,我找到了解决方案。对我有用的是包括以下代码:

from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'qt4'

例如:

from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'qt4' # Force PyQt4 utilization

import numpy

dphi, dtheta = numpy.pi/250.0, numpy.pi/250.0
[phi,theta] = numpy.mgrid[0:numpy.pi+dphi*1.5:dphi,0:2*numpy.pi+dtheta*1.5:dtheta]
m0 = 4; m1 = 3; m2 = 2; m3 = 3; m4 = 6; m5 = 2; m6 = 6; m7 = 4;
r = numpy.sin(m0*phi)**m1 + numpy.cos(m2*phi)**m3 + numpy.sin(m4*theta)**m5 + numpy.cos(m6*theta)**m7
x = r*numpy.sin(phi)*numpy.cos(theta)
y = r*numpy.cos(phi)
z = r*numpy.sin(phi)*numpy.sin(theta)

# View it.
from mayavi import mlab
s = mlab.mesh(x, y, z)
mlab.show()

给出正确的结果。