Python error "TypeError: sort() takes at most 2 arguments (3 given)"

Python error "TypeError: sort() takes at most 2 arguments (3 given)"

我收到 "TypeError: sort() takes at most 2 arguments (3 given)" 运行 以下摘自 this tutorial 的脚本:

import numpy as np
from mayavi import mlab

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)
sf = mlab.pipeline.scalar_field(s)
mlab.pipeline.volume(sf)

我使用的 python、numpy 和 mayavi 版本分别是 3.5.2 (Anaconda 4.1.1)、1.11.1 和 4.5.0。 Mayavi 是使用以下命令安装的:

conda install --channel https://conda.anaconda.org/menpo mayavi

我已经尝试了一些mayavi的其他API,它们工作正常。有谁知道可能出了什么问题?完整回溯如下。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-6743e5b203bd> in <module>()
----> 1 mlab.pipeline.volume(sf)

C:\Anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\mayavi\tools\pipe_base.py in the_function(*args, **kwargs)
     36 def make_function(factory_class):
     37     def the_function(*args, **kwargs):
---> 38         factory = factory_class(*args, **kwargs)
     39         return factory._target
     40 

C:\Anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\mayavi\tools\pipe_base.py in __init__(self, parent, **kwargs)
    144             self._do_redraw = not scene.disable_render
    145             scene.disable_render = True
--> 146         if issubclass(self._target.__class__, Filter):
    147             self._engine.add_filter(self._target, obj=parent)
    148         else:

C:\Anaconda3\lib\site-packages\traits\trait_types.py in create_default_value(self, *args, **kw)
   2799                 raise TraitError('Unable to locate class: ' + args[0])
   2800 
-> 2801         return klass( *args[1:], **kw )
   2802 
   2803     #: fixme: Do we still need this method using the new style?...

C:\Anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\mayavi\core\module.py in __init__(self, **traits)
     56 
     57         # Let the module setup its pipeline.
---> 58         self.setup_pipeline()
     59 
     60     def __get_pure_state__(self):

C:\Anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\mayavi\modules\volume.py in setup_pipeline(self)
    305         vp = self._volume_property = tvtk.VolumeProperty()
    306 
--> 307         self._ctf = ctf = default_CTF(0, 255)
    308         self._otf = otf = default_OTF(0, 255)
    309         vp.set_color(ctf)

C:\Anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\mayavi\modules\volume.py in default_CTF(x1, x2)
    123                     val_range=(1.0, 1.0),
    124                     n=10,
--> 125                     mode='sqrt')
    126 
    127 

C:\Anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\mayavi\modules\volume.py in make_CTF(x1, x2, hue_range, sat_range, val_range, n, mode)
    101             v = val_range[0] + dval*x
    102             r, g, b, a = [sqrt(c) for c in hsva_to_rgba(h, s, v, 1.0)]
--> 103             ctf.add_rgb_point(mins+x*ds, r, g, b)
    104     elif mode == 'linear':
    105         for i in range(n+1):

c:\anaconda3\lib\site-packages\mayavi-4.5.0-py3.5-win-amd64.egg\tvtk\util\ctf.py in add_rgb_point(self, *args)
    208         ret = super(ColorTransferFunction, self).add_rgb_point(*args)
    209         self.nodes.append(args[0])
--> 210         self.nodes.sort()
    211         return ret
    212 

C:\Anaconda3\lib\site-packages\traits\trait_handlers.py in sort(self, cmp, key, reverse)
   2565     def sort ( self, cmp = None, key = None, reverse = False ):
   2566         removed = self[:]
-> 2567         list.sort( self, cmp = cmp, key = key, reverse = reverse )
   2568 
   2569         if (getattr(self, 'name_items', None) is not None and

TypeError: sort() takes at most 2 arguments (3 given)
*********************************************************************************

显然您的 traits 版本不支持 Python 3. In Python 3 the cmp argument was removed from list.sort.