8

So I've just updated matplotlib to version 3.0.2 . I can import matplotlib just fine, but when I try to import matplotlib.pyplot, I get :

---------------------------------------------------------------------------
AttributeError                            
Traceback (most recent call last) <ipython-input-3-864e826dab68> in <module>()
----> 1 import matplotlib.pyplot

/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py in <module>()
     30 from cycler import cycler
     31 import matplotlib
---> 32 import matplotlib.colorbar
     33 import matplotlib.image
     34 from matplotlib import rcsetup, style

/usr/local/lib/python3.6/dist-packages/matplotlib/colorbar.py in <module>()
     30 import matplotlib.collections as collections
     31 import matplotlib.colors as colors
---> 32 import matplotlib.contour as contour
     33 import matplotlib.cm as cm
     34 import matplotlib.gridspec as gridspec

/usr/local/lib/python3.6/dist-packages/matplotlib/contour.py in <module>()
     16 import matplotlib.colors as mcolors
     17 import matplotlib.collections as mcoll
---> 18 import matplotlib.font_manager as font_manager
     19 import matplotlib.text as text
     20 import matplotlib.cbook as cbook

/usr/local/lib/python3.6/dist-packages/matplotlib/font_manager.py in <module>()
    133 
    134 if not USE_FONTCONFIG and sys.platform != 'win32':
--> 135     OSXFontDirectories.append(str(Path.home() / "Library/Fonts"))
    136     X11FontDirectories.append(str(Path.home() / ".fonts"))
    137 

AttributeError: type object 'Path' has no attribute 'home'

I'm on Ubuntu 18.04 LTS, on Jupyter Lab 0.35.4 with python3.6.7.

Side information/question : Before installing jupyter lab this morning, I was using jupyter notebook with python3.6.0. Now the kernels says it is using python3.6.7, although I cannot seem to find it anywhere on my system.

That being told, when I import anything else that doesn't rely on matplotlib.pyplot, everything works perfectly. If I try seaborn, for example, it returns me to the same attribute error.

EDIT In fact, the error happens with the pathlib library. It also happens whether or not I'm in jupyter. To replicate it :

from pathlib import Path
Path.home() 

and the error is the same as before :

AttributeError: type object 'Path' has no attribute 'home'
Alex
  • 117
  • 2
  • 11
  • The error would suggest that you use a python version < 3.5. [doc](https://docs.python.org/3/library/pathlib.html#pathlib.Path.home) However, there is at least one other user [with the same problem](https://longervision.github.io/2019/01/09/Bugs/matplotlib-path-home/). – ImportanceOfBeingErnest Feb 28 '19 at 23:13
  • Yeah ! Saw that, however I was thinking that there might be an other way before editing the matplotlob files. – Alex Feb 28 '19 at 23:46
  • I would by all means not recommend editing the matplotlib source. Rather you should try to find out why `from pathlib import Path; Path.home()` fails for you. That is independent of matplotlib. – ImportanceOfBeingErnest Mar 01 '19 at 00:58
  • Sorry for the delay, it would seem it is a rather uncommon error for pathlib since I cannot find information on it ! – Alex Mar 01 '19 at 15:33
  • I think I would start by finding out if it works outside of jupyter lab or not. Of course any further information you can provide about your system and environment might help someone to identify any potential problem. For sure the question might attract more attention if it is asked outside the context of matplotlib. – ImportanceOfBeingErnest Mar 01 '19 at 15:45
  • Thank you for the tip ! I'll change my question to something about pathlib ! good idea – Alex Mar 01 '19 at 19:30
  • Late reply, probably not the best answer, but i was due anyway : I've formatted my computer and reinstalled in virtual environments. A bit drastic, but no more problems ! – Alex Mar 12 '19 at 19:05
  • Please see my answer for a quick, easy, and targeted fix, that doesn't require formatting your computer, and deals with the root cause. Good Luck. – Elegant Code Feb 12 '21 at 22:27

4 Answers4

2

I hit this recently where matplotlib was trying to import and use pathlib and failing because my virtualenv was 3.6 but it was somehow getting a 3.4 pathlib and hitting either the exception you hit or another exception where the mkdir() it was calling was missing the exists_ok optional parameter.

It turned out that one of the modules I was using, openapi-spec-validator version 0.2.6, downloads a copy of pathlib (of the 3.4 type) and sticks it in the base site-packages folder where it can easily end up being used by matplotlib instead of the standard library version. In my case upgrading to 0.2.7 fixed it as that version doesn't download and place the file there.

However, if you're not using openapi-spec-validator and you hit this problem check to see if there are extra copies of pathlib.py floating around your environment.

Will P
  • 21
  • 2
  • Hi ! Thank you for your late reply ! In the end, maybe it is a bit drastic, but I had to do it anyway, I ended up formatting my computer and started a clean install with everything in virtual environments. Everything good so far ! – Alex May 30 '19 at 14:30
2

If your environment include multiple versions of python, check which version of pathlib your environment is currently using by pip show.

pip show pathlib

If it is pointing the pathlib of python <3.5, modify the PYTHONPATH to point pathlib of expected version of python.

You can further check the list of directory of python package by sys.path. If it shows directories of an unexpected version of python earlier than that of an expected version, that's the reason why your environment import older pathlib.

import sys
sys.path
hornistyf
  • 21
  • 1
1

I was able to fix this problem by performing pip uninstall pathlib while in the environment from which I using python and getting this error.

This should work for anyone using python >=3.5

The cause of the problem is that from python 3.5 and later, pathlib comes bundled with all the standard libraries of python. This will be the newer version that has Path.home(). However, if for some reason, like me, you have pathlib also installed as an independent package via pip, it will be the older version that doesn't have pathlib.Path.home(), and therefore breaks when home() is called.

Before uninstalling pathlib with pip uninstall pathlib make sure that:

  1. You actually have python >= 3.5 by running python --version.
  2. That you actually have a redundant installation of the "old" pathlib by running pip show pathlib
Dharman
  • 30,962
  • 25
  • 85
  • 135
Elegant Code
  • 678
  • 6
  • 18
0

what about the classic trick of uninstalling matplotlib and installing it again?

pip uninstall matplotlib==3.0.2
pip install matplotlib==3.0.2

Also, if you are not doing it already, I strongly suggest you should check using virtual environments (e.g. this guide) as you will have absolute control on which packages you install/modify/uninstall in a particular virtualenv (not affecting your development environment for all your projects).

glhuilli
  • 46
  • 7
  • Thank you for your reply! BTW, you link has an extra http:// . I've tried uninstalling and reinstalling matplotlib and also pathlib for good measures. I've been able to replicate the error with : `from pathlib import Path` then `Path.home()` but there is no such attribute in `Path` – Alex Feb 28 '19 at 22:33
  • thanks! fixed the link. What do you get when you run `Path.cwd()`? – glhuilli Feb 28 '19 at 22:46
  • returns my home folder (`/home/galex`) – Alex Feb 28 '19 at 23:08