ImportError: cannot import name cbook

前端 未结 5 1104
星月不相逢
星月不相逢 2020-12-07 00:39
>>> import matplotlib
Traceback (most recent call last):
  File \"\", line 1, in 
  File \"/usr/local/lib/python2.7/dist-packages         


        
5条回答
  •  旧时难觅i
    2020-12-07 01:07

    I hit this issue today due to a bad dependency.

    If you have both backports.shutil_get_terminal_size and backports.functools_lru_cache installed, you can encounter this.

    Matplotlib has a brittle workaround for a cyclic import:

    # cbook must import matplotlib only within function
    # definitions, so it is safe to import from it here.
    from . import cbook
    

    Until PR #10483, matplotlib dependended on backports.functools_lru_cache.

    However, ipython depends on backports.shutil_get_terminal_size, and that package doesn't set up a namespace package properly.

    If you have this problem, you'll see these symptoms:

    >>> import backports
    
    >>> >import backports.functools_lru_cache
    ImportError: No module named functools_lru_cache
    

    The problem with backports.shutil_get_terminal_size is that it doesn't define a namespace package, so it breaks any other backports.foo packages.

    Reinstalling matplotlib fixes this because it changes the order in sys.path, putting backports.functools_lru_cache first, and that package defines a proper namespace.

    You can also fix this by reinstalling backports.shutil_get_terminal_size.

提交回复
热议问题