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

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!

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

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.

Related

Python Error: PermissionError: [WinError 5] Access is denied

So I am currently trying to use Tesseract (pytesseract wrapper) in Python 3.5. Now Im at the office so my guess is that there are some goofy permissions not set and thats why I get this error trying to run some pretty simple code. Now I do have admnin permissions on this machine and can change file permissions... any idea what I can do to get this to run?
If anything it will help me wrap my head around system permissions in general as I work with different OS.
import pytesseract
from PIL import Image
test = Image.open('test.png')
print (pytesseract.image_to_string(test))
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
========= RESTART: C:\Users\dmartin\CheckScanScript\TextFromImage.py =========
Traceback (most recent call last):
File "C:\Users\dmartin\CheckScanScript\TextFromImage.py", line 4, in <module>
print (pytesseract.image_to_string(test))
File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
config=config)
File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\dmartin\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
I've had same problem. I solved this. First you must add path C:\Program Files (x86)\Tesseract-OCR\ in environment variables. Second I noticed if my code in differen disk, programm can't load language from folder tessdata. So I move my code from Disk D to Disk C, and it's finally work.
I faced this same issue and adding complete path for the pytesseract executable has worked for me. So , if you have installed pytesseract in your "C:\Program Files (x86)\Tesseract-OCR\tesseract" make sure in your code you are adding below path:-
C:\Program Files (x86)\Tesseract-OCR\tesseract\tesseract.exe
Your code would look like below
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract\\tesseract.exe'
pytesseract.pytesseract.tesseract_cmd = tesseract_cmd
print(pytesseract.image_to_string(Image.open('test.png')))
Hope this works for you too.
I solved it by give Permission to the file by code:
import stat
import os
os.chmod("file",stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH|stat.S_IXUSR|stat.S_IRUSR|stat.S_IWUSR|stat.S_IWGRP|stat.S_IXGRP)
os.remove("file")
I had the same issue and I resolved it by running IDLE as an Administrator and then opening the .py file thru IDLE.
Run Python or Python IDE as Admin and Set tesseract_cmd, pytesseract.pytesseract.tesseract_cmd, TESSDATA_PREFIX and tessdata_dir_config as follows:
from PIL import Image
import pytesseract
tesseract_cmd = 'D:\\Softwares\\Tesseract-OCR\\tesseract'
pytesseract.pytesseract.tesseract_cmd = 'D:\\Softwares\\Tesseract-OCR\\tesseract'
TESSDATA_PREFIX= 'D:\Softwares\Tesseract-OCR'
tessdata_dir_config = '--tessdata-dir "D:\\Softwares\\Tesseract-OCR\\tessdata"'
print(pytesseract.image_to_string( Image.open('D:\\ImageProcessing\\f2.jpg'), lang='eng', config=tessdata_dir_config))
For me what fixed it was inserting the direct path to tesseract.exe
What it looks like for me is:
import pyautogui
from PIL import Image
from pytesseract import *
pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
Note that you will have to find the path yourself!
I had the same error. For me, it turned out the file mentioned in the (access denied) error was still held by the system in the background from previous run. I ended the process in the Task Manager of Windows and problem solved.

Error getting Scapy to work on Windows: "'module' object has no attribute 'ex_name'"

I'm trying to run a Python script that involves ARP sniffing and is apparently dependent on the Scapy library being present. I have absolutely no idea what I'm doing but I'm reasonably good at Googling, following directions, and copying/pasting. I have it up and running on my Mac, but I'm stuck on what I hope is the last hurdle in getting Scapy working on my Windows computer (which is ultimately the one that needs to be running this script).
I followed all of the instructions at http://www.secdev.org/projects/scapy/doc/installation.html#windows, except that I chose Python 2.7 and used the newer 2.7-compatible versions of everything listed there. I used “python setup.py install” (successfully, as best I could tell) on all installs except Pypcap and Libdnet, which I installed via the Exe as an Administrator as instructed.
Unfortunately, when I type "scapy" into the command prompt to test if it works, I get the following information & error message:
C:\scapy-2.3.1>scapy
INFO: Can't import python gnuplot wrapper . Won't be able to plot.
INFO: Can't import PyX. Won't be able to use psdump() or pdfdump().
Traceback (most recent call last):
File "C:\Python27\Scripts\\scapy", line 25, in <module>
interact()
File "C:\Python27\lib\site-packages\scapy\main.py", line 278, in interact
scapy_builtins = __import__("all",globals(),locals(),".").__dict__
File "C:\Python27\lib\site-packages\scapy\all.py", line 16, in <module>
from arch import *
File "C:\Python27\lib\site-packages\scapy\arch\__init__.py", line 79, in <module>
from windows import *
File "C:\Python27\lib\site-packages\scapy\arch\windows\__init__.py", line 214, in <module>
ifaces.load_from_dnet()
File "C:\Python27\lib\site-packages\scapy\arch\windows\__init__.py", line 173, in load_from_dnet
self.data[i["name"]] = NetworkInterface(i)
File "C:\Python27\lib\site-packages\scapy\arch\windows\__init__.py", line 93, in __init__
self.update(dnetdict)
File "C:\Python27\lib\site-packages\scapy\arch\windows\__init__.py", line 107, in update
self._update_pcapdata()
File "C:\Python27\lib\site-packages\scapy\arch\windows\__init__.py", line 118, in _update_pcapdata
win_name = pcapdnet.pcap.ex_name(guess)
AttributeError: 'module' object has no attribute 'ex_name'
Can anyone help me out? If you need more information please let me know.
I am running Windows 10.
Thanks in advance,
- Ethan
I had same problem.
To solve it, i downloaded dnet-1.12.win32-py2.7.exe
and pcap-1.1.win32-py2.7.exe.
You might want to try with Scapy's current development version (from the Github repository). Support for Windows has been updated recently and should work without the need of Libdnet.
If that's not the case, you should probably open an issue.
Try it with scapy3k. Install python3 (e.g. I use Anaconda 3.5), and WinPcap driver. You do not need dnet or pypcap. Install using pip install scapy-python3 or from http://github.com/phaethon/scapy

python - pyinstaller "RuntimeWarning: Parent module 'PyInstaller.hooks.hook-PIL' not found while handling absolute import" and "tcl" related errors

I get a warning message while trying to create exectable file using pyinstaller. This warning appeared after installing Pillow. Previously i nevre got any warnings and was able to make it through.
the warning i get by pyinstaller is:
7314 INFO: Analyzing main.py
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1dev_-py2.7.egg/PyInstaller/hooks/hook-PIL.Image.py:14: RuntimeWarning: Parent module 'PyInstaller.hooks.hook-PIL' not found while handling absolute import
from PyInstaller.hooks.shared_PIL_Image import *
Also when i tried to run the executable's exe/consol version of my code that lies inside the dist folder created by the pyinstaller (dist/main/main), these are displayed..
Traceback (most recent call last):
File "<string>", line 26, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1dev_-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 276, in load_module
exec(bytecode, module.__dict__)
File "/Users/..../build/main/out00-PYZ.pyz/PIL.PngImagePlugin", line 40, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1dev_-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 276, in load_module
exec(bytecode, module.__dict__)
File "/Users/..../build/main/out00-PYZ.pyz/PIL.Image", line 53, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1dev_-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 276, in load_module
exec(bytecode, module.__dict__)
File "/Users/..../build/main/out00-PYZ.pyz/FixTk", line 74, in <module>
OSError: [Errno 20] Not a directory: '/Users/.../dist/main/tcl'
logout
[Process completed]
so, i tried by uninstalling pillow, installing tk tcl dev version. And then installed pillow. Even that didnt helped.
I also tried reinstalling pyinstaller,. didnt help too
Update 1:
It seems Pyinstaller.hooks.hook-PIL.py file was missing in the Pyinstaller/hooks directory. And it was missing on all platforms(Mac, windows and linux). This is the warning/error message that i get on windows, which is the same i got on mac and on linux.
Later i found a link which said, its just to need Python import machinery happy. so i created as said so. Then i dont get the same error on all platforms, But on mac i still get the PILImagePlugin,Image and FixTk errors
Solution for tcl:
I found what was going wrong,.. Every problem that i faced on OSX was the OS itself(exactly the macport). Python by default comes with the mac OS. And this version of python may be useful for just learning basic python, but is not suitable for Development purpose.
Installing brew's python helped. I followed this SO link. After doing these i was still getting errors. Later i had to change the paths on /etc/paths. Basically rearranging them should work. But still then i wasn't getting it right.
Then i had to change the .bash_profile, which worked for most users, But still i was getting mac's version of python and pip, not the brews version of python.
Finally i had to restart the machine for a couple of times and do the /etc/paths and .bash_profile steps repeatedly to get the system wide effect to accept brews version of python and pip
Solution for PIL:
just adding a file called hook-PIL.py with an empty content would serve the purpose. I found a link which was having the hook files content of pyinstaller.
The location to create
for mac : /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyInstaller-2.1.1dev_-py2.7.egg/PyInstaller/hooks/ Actually for mac this step wouldn’t be required. When we install python through brew and change the path, everything that you try to install later either through pip install or from source packages tend to choose a different path. And everything will be taken care of.
for windows:C:\Python27\lib\site-packages\PyInstaller-2.1.1.dev0-py2.7.egg\PyInstaller\hooks
**Please check if this is a valid path on your machine before creating the file and then create the file. And im not sure or i don't know if just adding an empty file is the right way. But it worked for me

Unable to import scipy but can import numpy in IronPython

I am trying to port my code from Python(v2.7) to IronPython so as to be able to leverage the multiple cores I have at my disposal on my lab computers.
I first installed IronPython from: http://ironpython.net/
Then I installed (or at least I think I have) the packages numpy and scipy following the instructions given here: https://www.enthought.com/repo/.iron/
I also tried the last command given on the page (> ipy -X:Frames -c "import scipy") and, since the command prompt didn't spit out an error, I assumed that everything went well.
But strangely enough, I can import numpy but I cannot import scipy. The following is the error message I get:
IronPython 2.7.4 (2.7.0.40) on .NET 4.0.30319.18444 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.pi
3.141592653589793
>>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\scipy\__init__.p
y", line 124, in <module>
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\numpy\_import_to
ols.py", line 15, in __init__
AttributeError: 'module' object has no attribute '_getframe'
What have I missed here? I searched Google for AttributeError: 'module' object has no attribute '_getframe', but was not able to find a solution to my problem.
Have you tried to use an interactive window? You can add the "-X:Frames" like this
1. Go to: Tools -> Options -> Python Tools -> Interactive Windows -> IronPython 2.7
2. Set "Interpreter Options" to "-X:Frames"
Also, check if the -X:Frames is on the output when you run this command
PS> ( Get-WmiObject Win32_Process -Filter "Name like '%ipy%'" ).CommandLine
If not, then something goes wrong with "-X:Frames".

How to properly install matplotlib on mac 10.7.5? And why is the command "conda" not found after Anaconda is successfully installed?

I tried for a long time to work with python on this Mac. I tried to remove python completely from my mac. Then I realized it was hard to get rid of this default python. Fortunately, I read in the internet that it's a bad idea to remove that one python that come with Mac before I figured out how to remove it. Then I also installed python, numpy, scipy, matplotlib using *.dmg packages. After all the effort, I still get this error message every time I import pylab. Please help me? Anyone?
$python
Python 2.7.4 (v2.7.4:026ee0057e2d, Apr 6 2013, 11:43:10)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pylab
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/pylab.py", line 221, in <module>
`from matplotlib import mpl # pulls in most modules
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/mpl.py", line 2, in <module>
`from matplotlib import axis
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/axis.py", line 14, in <module>
`import matplotlib.text as mtext
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/text.py", line 31, in <module>
`from matplotlib.backend_bases import RendererBase
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/backend_bases.py", line 48, in <module>
`import matplotlib.textpath as textpath
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/textpath.py", line 9, in <module>
`from matplotlib.mathtext import MathTextParser
File "/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/mathtext.py", line 52, in <module>
`import matplotlib._png as _png
ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/_png.so, 2): Library not loaded: /opt/local/lib/libpng14.14.dylib
Referenced from: /Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/_png.so
Reason: image not found
Besides, I followed the instruction on http://ipython.org/install.html to install Anaconda. After Anaconda is installed I went to the next step
$ conda update conda
and got this
"-bash: conda: command not found"
I searched in the net, but it seems no one else has this problem. Can anyone give me a hint here? Many thanks!
In my directory /usr/bin I have
python python-config python2.5 python2.5-config python2.6 python2.6-config python2.7 python2.7-config
With command
which python
I got /Library/Frameworks/Python.framework/Versions/2.7/bin/python
with
which ipython
I got /Library/Frameworks/Python.framework/Versions/2.7/bin/ipython
For numpy I downloaded the package numpy-1.7.0-py2.7-python.org-macosx10.6.dmg, scipy-0.12.0-py2.7-python.org-macosx10.6.dmg for scipy, and matplotlib-1.2.0-py2.7-python.org-macosx10.6.dmg for matplotbib.I downloaded *10.6.dmg because that's what I found...I can not find something like *10.7.dmg
I don't have .bashrc or .bashrc_profile. But I think .profile will do, and it reads like this
# MacPorts Installer addition on 2012-03-07_at_18:55:26: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.
export TERM="xterm-color"
alias ls="ls -G"
export PS1="[\[\e[33m\]\u#\H \[\e[32m\]\w\[\e[0m\]]\n[\[\e[31m\]\!\[\e[0m\]] > "
# Setting PATH for Python 2.7
# The orginal version is saved in .profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
When I type in
$port
it says -bash: port: command not found
Should that worry me?
Shall I also out comment the line
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
as well?
You appear to have MacPorts. Perhaps you can use MacPorts to install the Python of your choice, and then use the corresponding pip (probably /opt/local/bin/pip to install the necessary Python packages.
If MacPorts is something you're not using anymore, I suggest to use homebrew instead: http://mxcl.github.io/homebrew/ . Pay attention to the message you get after installing Python: it tells you that some Python scripts will be installed in /usr/local/share/python. In your case, this may not be an issue (the packages you listed don't install Python scripts afaik).
Keep in mind that using homebrew and MacPorts together may still mess with Python and its packages.
Finally, you need to make sure you don't keep on using the other python executable. Therefore, in your .profile, comment out the last two lines like this:
#PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
#export PATH
Update
Then, to get rid of the MacPorts reference and ensure your PATH picks up /usr/local/bin, change your .profile further to
# MacPorts Installer addition on 2012-03-07_at_18:55:26: adding an appropriate PATH variable for use with MacPorts.
#export PATH=/opt/local/bin:/opt/local/sbin:$PATH # <- comment out
# Set /usr/local/bin explicitly for Homebrew
export PATH=/usr/local/bin:$PATH
Start in a new Terminal (or tab) to have your settings updated.

Categories

Resources