Mineta
Mineta

Reputation: 37

How to fix "object has no attribute" error when importing matplotlib.pyplot

I'm recently trying to do something with Tensorflow. The installation seems very fine until I've encountered this problem. When importing matplotlib.pyplot, AttributeError was thrown. Below is the detail:

>>> import matplotlib.pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/syine/venv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2349, in <module>
    switch_backend(rcParams["backend"])
  File "/home/syine/venv/lib/python3.7/site-packages/matplotlib/__init__.py", line 820, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel)
  File "/home/syine/venv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 212, in switch_backend
    newbackend[9:] if newbackend.startswith("module://")
AttributeError: 'object' object has no attribute 'startswith'

Seemingly it's an internal error in the module, however I can't find any hint to solve the issue.

I tried reinstalling matplotlib and all of its dependencies (FreeType, libpng, NumPy, setuptools, cycler, dateutil, kiwisolver, pyparsing), but none of them could solve the problem.

Upvotes: 2

Views: 1935

Answers (2)

Dave Hirschfeld
Dave Hirschfeld

Reputation: 768

Looks like this is a known issue:

https://github.com/matplotlib/matplotlib/issues/14263

It sounds like it may be a problem with your environment and the fix in GitHub will only serve to raise a meaningful error message

Upvotes: 3

Use the function hasattr this will let you check whether a particular attribute belongs to a object or not before referencing it.

Upvotes: -2

Related Questions