Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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!

share|improve this question
    
Well, you could work around the problem by patching matplotlib (either on-disk, or monkeypatching it), or by forcibly building a font cache for it, or whatever… but it would be a lot simpler to just fix the problem by chmoding the font file(s). (If you don't know how to do that, that's probably a question for SuperUser, not StackOverflow.) If you really want to work around it instead, then someone could probably show you how to do so here, but it's going to be harder and clunkier. – abarnert Nov 12 '14 at 19:54
up vote 2 down vote accepted

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.

share|improve this answer
1  
He could also patch the code he already has, instead of going through all the Github thing. If you do import matplotlib; print matplotlib.__file__ to figure where it is. – Santiago Nov 12 '14 at 20:24
1  
@user3267581: Yeah; that can lead into having to know how to find .pyc files for different versions and look inside .egg/.zip archives and understand more about file permissions and so on. Also, it's harder to reproduce across multiple machines (even if that just means "my current machine, and the new laptop I'll have 18 months from now when I've forgotten how to solve it"). If it's applicable for the OP, great, it can save him some time (so +1); but I wouldn't want to write it as a general solution. – abarnert Nov 12 '14 at 20:34
1  
Thank you! My problem was solved immediately by following your suggestion to change the except RuntimeError: to except (RuntimeError, IOError): I didn't want to chmod that font file (even if that's the better fix vs workaround) because I suspected there were going to be many, many more individual font files whose permissions I'd also have to change. (The comment button suggests I shouldn't write a simple comment like "thanks!" but I am very appreciative of your help.) – LCK Nov 13 '14 at 5:44

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.