Can't import matplotlib.pyplot, font related IOError

I recently installed Anaconda distribution for Python. When I try to import matplotlib.pyplot, I get a "Permission denied" error because the font manager tries to access one of the fonts on my computer.

Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 19, in <module>
import matplotlib.font_manager as font_manager
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1412, in <module>
_rebuild()
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1397, in _rebuild
fontManager = FontManager()
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1052, in __init__
self.ttflist = createFontList(self.ttffiles)
File "/Users/lck/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py", line 579, in createFontList
font = ft2font.FT2Font(fpath)
IOError: [Errno 13] Permission denied: u'/Library/Fonts/Finale Lyrics Italic.ttf'

      

How can I get matplotlib.pyplot to load and not stop at this error with "Permission denied" permission? I don't need any particular font (for example, I don't need to use "Finale Lyrics Italics" - any font is fine). Any thoughts would be greatly appreciated!

+3


source to share


2 answers


The obvious thing to do here is fix the problem, not work around it. You know the path to the offending file; just chmod

it.

But if you need to work around this instead (for example, you are deploying your program to many machines, any of which could have this problem) ... well, if you look at the source , the problem is in font_manager.createFontList

. For fonts other than AFM, the constructor is FT2Font

wrapped in try

, which processes RuntimeError

and UnicodeError

, but not IOError

. *

You could argue that this is a bug in matplotlib. I'm not sure about this, but if you think so, file a bug and post to the mailing list .

But you need to fix it anyway, whether you are using it locally or sending it upstream. The patch is simple. In this function, just change this:

try:
    font = ft2font.FT2Font(fpath)
except RuntimeError:
    verbose.report("Could not open font file %s"%fpath)
    continue

      

... in:



try:
    font = ft2font.FT2Font(fpath)
except (RuntimeError, IOError):
    verbose.report("Could not open font file %s"%fpath)
    continue

      

So there are two ways to do this.

If you want to fix your copy matplotlib

, develop a repo on Github, make a branch, edit your copy of the file, commit your fork, make sure you have all dependencies up to date, and either pip install .

from the top level of your fork, or install directly from git . (If you filed a bug, you must also create a pull request or create a patch file and upload it to the bug report.) **

If you want to monkeypatch this from your own code, copy the entire function createFontList

into your code, edit the copy, then add matplotlib.font_manager.createFontList = createFontList

after the definition.

* Instead, you can schedule ft2font.FT2Font

to lift RuntimeError

in this case, but implemented in C , not Python, so it will hurt more.

** Since user3267581 suggests instead of editing and restoring the project, you can just edit the file .py

in your package sites.Of course this will only work on one machine, makes it easy to forget the workaround if you need it later, and may require you to know something about how site packages work, but if that all sounds okay, it's obviously a lot less work.

+2


source


I have the same problem on Linux Mint 17.2. I do this in my terminal: sudo chmod 644 / my-fonts-path / *

For you try: sudo chmod 644 / Library / Fonts / Final / *



More information can be found here http://ubuntuforums.org/showthread.php?t=1976037

+4


source







All Articles