Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pathlib library error in pathlib.Path.home() : type object 'Path' has no attribute 'home'

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'
like image 372
Alex Avatar asked Feb 28 '19 21:02

Alex


1 Answers

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.

like image 108
Will P Avatar answered Sep 19 '22 11:09

Will P