Maplotlib and Basemap: cannot import name 'dedent'
Asked Answered
M

4

9

I'm trying to draw a network on a basemap overlay. I have packages:

  • basemap=1.3.0=py36ha7665c8_0
  • matplotlib=3.3.1=0
  • matplotlib-base=3.3.1=py36hba9282a_0
  • networkx=2.5=py_0

When I run only the line
from mpl_toolkits.basemap import Basemap

I get
from matplotlib.cbook import dedent
ImportError: cannot import name 'dedent'

I've tried several different versions of the packages but cannot manage to find the right functioning combination.
Anyone have any ideas on combinations of matpltlib and basemap that work? Or another way to plot my network over a basemap?

Marijn answered 11/9, 2020 at 0:9 Comment(2)
Can you please try to downgrade your matplotlib ?Luffa
github.com/matplotlib/basemap/issues/439Smallage
T
16

The best that worked for me is:

downgrade to :

pip install -U matplotlib==3.2
Tarn answered 17/11, 2020 at 23:33 Comment(0)
F
8

I was have the same error with importing Basemap from when I pass from python 3.2.7 to 3.3.6.

The error message comes from the fact that you try to import Basemap from mpl_toolkits.basemap, but the mpl_toolkits.basemap module requires to import the dedent function from the matplotlib.cbook module, but this function is not there.

So I guess there were two possible solution : to comment the line which import this function or to copy it. I chose the second option.

I don't know why the dedent function is not present in the matplotlib.cbook module.

Here it is the dedent funtion as I found it on the link I put, it was also have for header : @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

I copy the function dedent from the site of matplotlib : https://matplotlib.org/3.1.1/_modules/matplotlib/cbook.html#dedent inside the module init.py - matplolib\cbook. And now it is working for me.

Be aware to copy it at the correct line, because some of its variable pre-defined in the module such as : _find_dedent_regex for the line :

    match = _find_dedent_regex.match(s)

and _dedent_regex for the lines :

    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

I deeply apologize for spelling and/or grammar errors that I could do, I will do my best to correct those I would have missed and reported to me.

I hope this was usefull.

Favorite answered 31/10, 2020 at 13:10 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Pithy
C
1

Matplotlib has removed cbook.dedent since 3.3.0.

Upgrade basemap to fix this: pip install -U basemap

Crews answered 28/5, 2022 at 3:57 Comment(0)
B
0

I tried to find a solution online that does not require altering any files. While in an Anaconda environment, use:

conda install matplotlib=3.2

Please note that support for Basemap is discontinued and should be avoided.

Berchtesgaden answered 6/2, 2022 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.