当我从 python 3.2.7 传递到 3.3.6 时,我在导入 Basemap 时遇到了同样的错误。
错误消息来自您尝试从mpl_toolkits.basemap导入Basemap,但是mpl_toolkits.basemap模块需要从matplotlib.cbook模块导入dedent函数,但是这个函数不在那里。
所以我想有两种可能的解决方案:注释导入此函数的行或复制它。我选择了第二个选项。
不知道为什么matplotlib.cbook模块中没有dedent函数。
这是 dedent 功能,因为我在我放置的链接上找到了它,它也有用于标题:@deprecated("3.1", alternative="inspect.cleandoc")
def dedent(s):
"""
Remove excess indentation from docstring *s*.
Discards any leading blank lines, then removes up to n whitespace
characters from each line, where n is the number of leading
whitespace characters in the first line. It differs from
textwrap.dedent in its deletion of leading blank lines and its use
of the first non-blank line to determine the indentation.
It is also faster in most cases.
"""
# This implementation has a somewhat obtuse use of regular
# expressions. However, this function accounted for almost 30% of
# matplotlib startup time, so it is worthy of optimization at all
# costs.
if not s: # includes case of s is None
return ''
match = _find_dedent_regex.match(s)
if match is None:
return s
# This is the number of spaces to remove from the left-hand side.
nshift = match.end(1) - match.start(1)
if nshift == 0:
return s
# Get a regex that will remove *up to* nshift spaces from the
# beginning of each line. If it isn't in the cache, generate it.
unindent = _dedent_regex.get(nshift, None)
if unindent is None:
unindent = re.compile("\n\r? {0,%d}" % nshift)
_dedent_regex[nshift] = unindent
result = unindent.sub("\n", s).strip()
return result
我从 matplotlib 站点复制函数 dedent:https://matplotlib.org/3.1.1/_modules/matplotlib/cbook.html#dedent 在模块 init.py - matplolib\cbook 内。
现在它对我有用。
注意将其复制到正确的行,因为它的一些变量在模块中预定义,例如:_find_dedent_regex for the line:
match = _find_dedent_regex.match(s)
和 _dedent_regex 用于行:
unindent = _dedent_regex.get(nshift, None)
if unindent is None:
unindent = re.compile("\n\r? {0,%d}" % nshift)
_dedent_regex[nshift] = unindent
This is where I put the function in the moddule
对于我能做的拼写和/或语法错误,我深表歉意,我会尽我所能纠正那些我会错过并报告给我的。
我希望这是有用的。