问题描述
In [1]: import selectivesearch
---------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-ddcf839e2cec> in <module>()
----> 1 import selectivesearch
/usr/local/lib/python3.4/dist-packages/selectivesearch/__init__.py in <module>()
----> 1 from selectivesearch import selective_search # NOQA
ImportError: cannot import name 'selective_search'
截图
当我们在python2 上使用并没有任何的错误,并能正常使用。这是为什么呢?
问题解决
进入selectivesearch模块安装目录,会有如下3个文件:
__init__.py __pycache__/ selectivesearch.py
我们用文本编辑器打开selectivesearch.py
1. 将第285行:
i, j = sorted(list(S.items()), cmp=lambda a, b: cmp(a[1], b[1]))[-1][0]
替换成:
i, j = sorted(list(S.items()), key = lambda a: a[1])[-1][0]
- 然后将init.py文件中的
from selectivesearch import selective_search # NOQA
改成
from .selectivesearch import selective_search # NOQA
详细原因请查看python3 module中init.py的需要注意的地方
- 由于字典并 python3 和 python2 并不兼容在206行(R = regions.items())下面添加
# 将 dict_items 转换成 list
r = [elm for elm in R]
R = r
这下就可以正常使用了。