LCK
LCK

Reputation: 33

Can't import matplotlib.pyplot, IOError related to a font

I recently installed the Anaconda distribution of Python. When I try to import matplotlib.pyplot, I receive a "Permission denied" error as 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 "Permission denied" font error? I don't need any particular font (eg, I don't need to use "Finale Lyrics Italics" - any font is fine). Any thoughts would be much appreciated!

Upvotes: 3

Views: 2997

Answers (2)

Ulinuha
Ulinuha

Reputation: 61

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

For you, try : sudo chmod 644 /Library/Fonts/Finale/*

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

Upvotes: 4

abarnert
abarnert

Reputation: 365975

The obvious thing to do here is to actually fix the problem instead of working around it. You know the path to the offending file; just chmod it.

But if you need to work around it instead (e.g., you're deploying your program to lots of machines, any of which may have this problem)… well, if you look at the source, the problem is in font_manager.createFontList. For non-AFM fonts, the FT2Font constructor is wrapped in a try that handles RuntimeError and UnicodeError, but not IOError.*

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

But either way, you need a fix, whether you just use it locally, or also submit it upstream. The patch is simple. In that function, just change this:

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

… to:

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 patch your copy of matplotlib, fork the repo on Github, make a branch, edit your copy of the file, commit to your fork, make sure you have all the dependencies up to date, and either pip install . from the top level of your fork or install directly from git. (If you've filed a bug, you should also create a pull request, or create a patchfile and upload it to the bug report.)**

If you instead want to monkeypatch it from your own code, copy the entire createFontList function into your code, edit the copy, then add matplotlib.font_manager.createFontList = createFontList after the definition.

* You could instead patch ft2font.FT2Font to raise a RuntimeError in this case, but that's implemented in C, not Python, so it's going to be more of a pain.

** As user3267581 suggests, instead of editing and rebuilding the project, you could just edit the .py file in your site-packages. Of course this will only work on one machine, will make 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 all of that sounds OK, it's obviously a lot less work.

Upvotes: 2

Related Questions