I'm trying to run the following python script (I'm using Vim):
import numpy as np;
import scipy as sp;
from scipy import misc;
import matplotlib.pyplot as plt;
image = misc.imread('test_image.jpg');
np.fliplr(image);
plt.imshow(image);
When I do, I get the following:
Traceback (most recent call last):
File "test.py", line 4, in <module>
import matplotlib.pyplot as plt;
File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 24, in <module>
import matplotlib.colorbar
File "/Library/Python/2.7/site-packages/matplotlib/colorbar.py", line 29, in <module>
import matplotlib.collections as collections
File "/Library/Python/2.7/site-packages/matplotlib/collections.py", line 23, in <module>
import matplotlib.backend_bases as backend_bases
File "/Library/Python/2.7/site-packages/matplotlib/backend_bases.py", line 50, in <module>
import matplotlib.textpath as textpath
File "/Library/Python/2.7/site-packages/matplotlib/textpath.py", line 11, in <module>
import matplotlib.font_manager as font_manager
File "/Library/Python/2.7/site-packages/matplotlib/font_manager.py", line 53, in <module>
from matplotlib import ft2font
ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib/ft2font.so, 2): Library not loaded: #loader_path/../../../libfreetype.6.dylib
Referenced from: /Library/Python/2.7/site-packages/matplotlib/ft2font.so
Reason: image not found
shell returned 1
I tried reinstalling brew, reinstalling freetype, matplotlib, and numpy in brew, and I uninstalled MacPorts with no change in the error. Suggestions?
EDIT: After uninstalling MacPorts and then doing another reinstall of brew, I now get this error instead.
Fatal Python error: PyThreadState_Get: no current thread
Command terminated
The error only appears when I import matplotlib, so I'm guessing the issue is with matplotlib. I will try to reinstall it with brew.
EDIT2: I've been trying from this page to no avail, but I think my error is probably related to that one.
[MacOS X] Ran into the same problem after installing graphviz (to visualize decision tree). Without carefully isolating environment, this new package appears to have put its own favorite version of freetype into my default python runtime library path. Then when doing a simple import import matplotlib.pyplot as plt, I got error messages:
ImportError: dlopen(/Users/shandou/anaconda3/lib/python3.6/site-
packages/matplotlib/ft2font.cpython-36m-darwin.so, 2): Library not
loaded: #rpath/libfreetype.6.dylib
Referenced from: /Users/shandou/anaconda3/lib/python3.6/site-
packages/matplotlib/ft2font.cpython-36m-darwin.so
Reason: Incompatible library version: ft2font.cpython-36m-darwin.so
requires version 22.0.0 or later, but libfreetype.6.dylib provides
version 21.0.0
At first, I could not understand what #rpath is really pointing to. Checked with locate libfreetype and it looks like for my default python environment, I have (1) /Users/shandou/anaconda3/lib/libfreetype.6.dylib and (2) /Users/shandou/anaconda3/pkgs/freetype-2.8.1-0
I tried the following two fixes. The first one solved the immediate need of making matplotlib import work, but later caused problems in sphinx auto doc generation. The second one is a cleaner fix that made both work.
Fix 1: conda uninstall and then pip install matplotlib
Context: I use anaconda python distribution and use conda as my main package manager
Spoiler alert: temporarily fixed import problem, but got me into trouble later when using sphinx
Takeaway: mixing pip and conda installation for major libraries can be problematic
Following #Robbie Capps's suggestion above, I uninstalled matplotlib originally installed with conda and reinstalled it with pip instead. Afterwards matplotlib import works okay and I was able to continue working until later I ran into errors when running sphinx for documenting codes:
File "/Users/shandou/anaconda3/lib/python3.6/site-
packages/matplotlib/backends/backend_macosx.py", line 17, in <module>
from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are using (Ana)Conda please install python.app and
replace the use of 'python' with 'pythonw'. See 'Working with
Matplotlib on OSX' in the Matplotlib FAQ for more information.
That looks hairy, but if I read this correctly, the gist of the message is: sphinx is not happy about me mixing conda and pip install.
So I ended up reverting matplotlib back to conda installation. Sadly the original libfreetype error immediately returns and I was unable to do basic matplotlib import (darn...)
Fix 2: Update freetype library located in runtime path (#rpath in the error message)
Context: I tried updating freetype, libpng, and matplotlib--basically anything that the web would suggest
Spoiler alert: If runtime path library is not updated, I am left with the same error message about incompatible libfreetype
[Step 1] Brew install freetype library:
$ brew install freetype
$ brew link --overwrite freetype
When checking the library version in /usr/local/Cellar/freetype/2.9/lib/, I get output as below:
$ otool -L libfreetype.6.dylib | head -n 2
libfreetype.6.dylib:
/usr/local/opt/freetype/lib/libfreetype.6.dylib (compatibility version 23.0.0, current version 23.0.0)
This is version 21+, so we are one step closer to solve the problem
[Step 2] Copy /usr/local/Cellar/freetype/2.9/lib/libfreetype.6.dylib to python runtime library path
It turns out even after updating freetype library with conda, the runtime library is not updated. The final solution that works for me is to force copy newer freetype lib to the runtime path:
$ cd /Users/shandou/anaconda3/lib/
$ sudo cp /usr/local/Cellar/freetype/2.9/lib/libfreetype.6.dylib .
Only then, the freetype library version incompatibility problem is gone, and both matplotlib import and sphinx are happy
Bottom line: Fix 2 is the cleaner way to go.
It sounds like you already (at least partially) answered your own question, but I solved this same error, in a somewhat different fashion. Summary: I used otool and install_name_tool to manually change the linkage from the *.so file to a different *.dylib. I'm posting it here mostly as an illustration of how build systems can quickly turn into rat's nests if you're not careful.
Background
I am using conda to manage the virtual environment for a software project I am trying to compile from source (GNU Radio). In order to build the graphics submodule of this project (gr-qtgui) natively on my Mac, I believe I committed some pretty heinous crimes against nature -- more on this below.
My basic strategy to avoid build problems is to rely on a single package manager as much as possible. In this case, the honor went to conda, though some of it required brew and pip. As I'm writing this, I'm now wondering whether I should have linked against the Mac frameworks for Qt4, but what I ended up doing was linking against libQtCore.dylib et al. installed by conda in ~/miniconda3/envs/gr/lib.*
Graphics issues & Resolution
After getting GNU Radio to compile, I then conda install'd matplotlib in this virtual environment. I was initially getting a similar error as OP originally mentions:
ImportError: dlopen(/Users/strotmc1/miniconda3/envs/gr/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Library not loaded: #rpath/libfreetype.6.dylib
Referenced from: /Users/strotmc1/miniconda3/envs/gr/lib/python2.7/site-packages/matplotlib/ft2font.so
Reason: Incompatible library version: ft2font.so requires version 21.0.0 or later, but libfreetype.6.dylib provides version 14.0.0
I solved it by navigating to the offending shared object's directory and inspecting it:
>> cd ~/miniconda3/envs/gr/lib/python2.7/site-packages/matplotlib
>> otool -L ft2font.so
ft2font.so:
#rpath/libfreetype.6.dylib (compatibility version 21.0.0, current version 21.0.0)
#rpath/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
>> otool -l ft2font.so
...
Load command 9
cmd LC_LOAD_DYLIB
cmdsize 56
name #rpath/libfreetype.6.dylib (offset 24)
time stamp 2 Wed Dec 31 19:00:02 1969
current version 21.0.0
compatibility version 21.0.0
...
Load command 12
cmd LC_RPATH
cmdsize 272
path /Users/strotmc1/miniconda3/envs/gr/lib (offset 12)
...
In other words, it's linking to the libfreetype.dylib located inside the virtual environment, not on the system. Sort of makes sense as an error, because pyplot is also trying to access the graphics libraries, which were already strangely configured. My solution was to switch to linking against the homebrew version of freetype:
>> install_name_tool -change #rpath/libfreetype.6.dylib /usr/local/Cellar/freetype/2.9/lib/libfreetype.6.dylib ft2font.so
and now matplotlib works!
Moral of the story
I said from the beginning that this story is ugly. After finishing writing this up, I'm left with the following conclusions:
Despite being able to compile large software projects, I still don't really know what the hell I'm doing.
Given the difficulty of writing general-purpose libraries, and how much variation can exist across platforms, and that all users set up their systems differently, I'm amazed that anything ever works.
On the other hand, is this really the best we can do? It seems like complexity is the source of the problem. Does any tool sophisticated enough to manage the complexity necessarily introduce more complexity into the system, and thus more opportunities for failure?
Sorry this turned into more of a musing than an answer -- perhaps someone would be so kind to suggest a better place for this type of rambling answer?
*To be completely frank, I never even considered trying to link against the Qt frameworks located in /Library/Frameworks. I'm not sure if the build tools (here, cmake) are smart enough to find these Frameworks, because I haven't tried. I will consider posing this question to the community.
Fixed it... sort of.
Fixed it.
I uninstalled with brew uninstall matplotlib and then installed with pip install matplotlib. The pip version now works. although I think something is wrong with my code because I don't get an image output.
I uninstalled all of my installations of python and then installed ipython, which worked fine. Also, I was forgetting to add plt.show(); after my imshow(); commands... :-( .... but at least someone else has made this mistake before me ...
Refer to this page for very thorough help with any matplotlib imshow issues:
matplotlib does not show my drawings although I call pyplot.show()
You can try to run python with Anaconda and check the version of freetype in your conda environment.
I got the same type of error when I try to install opencv in conda. And finally it was solved by one command:
conda update freetype
Two steps to solve the problem:
Step 1: Updating freetype and coping the .dylib to the anaconda lib folder:
brew upgrade freetype
cp /usr/local/Cellar/freetype/2.10.4/lib/libfreetype.dylib ~/opt/anaconda3/lib/
Step 2: Following the import sequence as below:
import matplotlib
matplotlib.use('PS')
import matplotlib.pyplot as plt
Found one immediate solution that worked for me.
Due to multiple projects in same environment, my matplotlib was not supporting new changes.
pip3 uninstall matplotlib
pip3 install matplotlib
Check path tou your image (and doublecheck)
This is in your traceback:
"Reason: image not found"
Also there is no need to use ';' in python.
Related
I've installed pydot and checked to make sure of this several times. However, when I attempt to import it in a Juptyer notebook file, I keep receiving a Module Not Found error:
ModuleNotFoundError: No module named 'pydot'
When I run pip3 show pydot, I get the following response:
Name: pydot
Version: 1.4.2
Summary: Python interface to Graphviz's Dot
Home-page: https://github.com/pydot/pydot
Author: Ero Carrera
Author-email: ero.carrera#gmail.com
License: MIT
Location: /Users/parkertemple/Library/Python/3.8/lib/python/site-packages
Requires: pyparsing
Required-by:
After, when I run
python3 myscript.py
I get this:
/usr/local/bin/python3: can't open file '/Users/(my user account)/script.py': [Errno 2] No such file or directory
I'm relatively new to the command line and am struggling a bit. Any help would be greatly appreciated.
I'm using mac os.
Thank you so much! 🙏
It's not uncommon to have these problems when starting out.
Usually this problem relates to Python3 not understanding where the pip directory is making its installations.
Two paths you can consider in fixing this issue:
For Python 3.7 (try replacing it with your python3 version):
export PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python3.7/site-packages:/usr/lib/python3.7/site-packages"
Use a Python virtual environment. This is like making a specific python & pip installation. It's particularly useful because it isolates the modules that your project needs, making it more lightweight, shareable, etc. This will take a little more time to set up; but it can be very important to understand why virtual environments are beneficial to your project.
I know this question has been asked many times before but I cannot find a solution that works for me. I'm running Python 3.5.3 under Blender 2.79 in Windows 10. As I require Blender 2.79, upgrading the Python version is not possible.
I've installed matplotlib and I can import it. However, when I try to import pyplot (e.g. import matplotlib.pyplot as plt), I get a crash to desktop with no error messages of any kind. This happens if I run Python externally or from inside Blender. The weird thing is that I was able to avoid this by changing backends to agg (matplotlib.use('agg')) and this is still working in a previous installation (so I know it's possible to get this to work !), but not in a separate, new installation of Blender. I've tried other backends but they make no difference.
The exact procedure I've tried is as follows :
Downloaded Blender 2.79 from a zip file (I'm using Windows 10) and unpacked it (call this directory /Blender/)
In /Blender/2.79/python/bin I run the command ./python -m ensurepip
In /Blender/2.79/python/scripts I run pip3 install --upgrade pip --user. This gives me pip3 version 20.2.4
In the same directory I run pip3 install --target="/Blender/2.79/python/lib/site-packages" matplotlib --upgrade. The "upgrade" switch is to prevent warnings that the "/bin" directory exists. Doesn't make any difference if I remove it, there's no existing installation of matplotlib. This gives me matplotlib version 3.0.3
Finally I start Python by /Blender/2.79/python/bin/python.exe, and do the above mentioned importmatplotlib.pyplot command which causes the crash.
I've tried this from a completely fresh installation of Blender, which comes with no existing external modules installed. Could there be some conflict with other Python modules elsewhere ? How would I go about diagnosing what's going on ?
Thanks for any ideas !
FIXED ! I remembered I found the solution months ago, but stupidly wrote down the answer in the wrong file...
The problem is that matplotlib is looking for a file that doesn't exist. In c:/users/me/.matplotlib (a hidden file), there's a file ""fontlist-vXXX.json", where XXX is the version number. This is set in line 951 of the file "font_manager.py", located in /python/lib/site-packages/matplotlib. In my case, the font_manager was looking for version 300 but the actual file was 310. Changing the version number in the font_manager.py made everything work correctly.
I realize this seems like a generic question, but all answers pointed to having two simultanious python installations - I already uninstalled the other one.
Currently I run my code from PyCharm 2017.1.5 (windows 10) with Python interpreter set as Python 3.6.1 (C:\Anaconda3\python.exe), i.e. I installed Anaconda3, which includes the matplotlib, and run from PyCharm using the Ananconda3-interpreter.
I've checked in Anaconda Navigator that matplotlib 2.0.2 is installed in the environment.
A minimal (non-working) example:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
Returns the following error:
C:\Anaconda3\python.exe C:/Users/John/Documents/CPU/master/untitled/main11.py
Traceback (most recent call last):
File "C:/Users/John/Documents/CPU/master/untitled/main11.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "C:\Anaconda3\lib\site-packages\matplotlib\colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "C:\Anaconda3\lib\site-packages\matplotlib\collections.py", line 37, in <module>
import matplotlib.lines as mlines
File "C:\Anaconda3\lib\site-packages\matplotlib\lines.py", line 28, in <module>
from matplotlib.markers import MarkerStyle
ModuleNotFoundError: No module named 'matplotlib.markers'
Process finished with exit code 1
This ran fine 2 weeks ago, but not now. To my knowledge, I didn't change or update anything. The module loads correctly, but it seems to be a change in the module content? If so: How did that happen and how can I fix it?
In my case, I could fix it by setting PYTHONPATH to the path to site-packages folder where the needed packages are located, excluding site-pacages.
I use a pyenv virtual environment, whose path is /home/apk/.pyenv/versions/python-3-7-4. When the environment is activated, pip installs packages to /home/apk/.pyenv/versions/python-3-7-4/lib/python3.7/site-packages. Therefore, in terminal, I set:
$ PYTHONPATH=/home/apk/.pyenv/versions/python-3-7-4/lib/python3.7/
The same should be true for Windows installations of python.
If virtual environmets are used, then one could edit activate script to set PYTHONPATH.
After I did it, I checked in a python shell.
$ python
Python 3.7.4 (default, Feb 5 2020, 17:11:33)
[GCC 5.5.0 20171010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/apk/.pyenv/versions/3.7.4/lib/python37.zip', '/home/apk/.pyenv/versions/3.7.4/lib/python3.7', '/home/apk/.pyenv/versions/3.7.4/lib/python3.7/lib-dynload', '/home/apk/.pyenv/versions/python-3-7-4/lib/python3.7']
>>> sys.executable
'/home/apk/.pyenv/versions/python-3-7-4/bin/python'
Good luck!
References
https://github.community/t5/Programming-Help-and-Discussion/Python-ModuleNotFoundError-although-module-is-installed-aiohttp/td-p/28525
#ImportanceOfBeingErnest lead me in the right direction. I post my solution here s.t. others may find the answer. The problem was a corrupted disk sector - an unlikely event of chance.
The problem was indeed in the matplotlib-package itself. Retrospectively, pointers to the issue were that errors in pre-distributed packages should not exist. If they do, external circumstances must have corrupted and the problem is not with the Python-installation itself.
I uninstalled matplotlib through Anaconda Prompt with conda remove matplotlib and re-installed with conda install matplotlib. This gave me this error:
(C:\Anaconda3) C:\Users\John>conda install matplotlib
[...]
ERROR conda.core.link:_execute_actions(337): An error occurred while installing package 'defaults::matplotlib-2.0.2-np112py36_0'.
OSError(22, 'Invalid argument') Attempting to roll back.
OSError(22, 'Invalid argument')
Before #Ernest's comment, I thought it maybe had to do with non-ASCII in PATH or similar.
Instead I tried to reinstall Anaconda3 completely, restarted and found that part of the Anaconda3-folder weren't removed (the one containing the matplotlib).
Deleting it manually gave a Windows error 0x80070570. Following this post on ServerFault (the comment to OP) I ran a check and afterwards a repair from Windows Explorer GUI: Right-click on the drive in This PC --> Properties --> Tab Tools --> Check (repair appears if any errors are found).
After some restarts, reinstalling Anaconda3 from scratch and restarting again, I was able to run my project again!
It is difficult to answer this question directly, however, I have seen a large amount of issues in corporate Windows environments with PyCharm and Anaconda these are some of the issues you may be having
Check you PATH is correctly pointing to all Anaconda locations
import sys
sys.path
Check that your files have not been migrated to C:\Users\username\AppData\Roaming by your IT team
Purge your system for any python distributions. There may be software distributions that you use internally that package their own python distribution. This can often be included in the PATH. Another example could be installing Anaconda to your C:\ but also having it already installed in Users\Local\AppData or 'C:\Program Files' months before and forgotten!
A good way of directly debugging your problem would be to navigate to the following directory in ipython
C:\Anaconda3\lib\site-packages\matplotlib
and they try import matplotlib.markers
If this fails then you could then try
import matplotlib
matplotlib.__file__
you should check that this result gives
'C:\\Anaconda3\\lib\\site-packages\\matplotlib\\__init__.pyc'
as most likely there will be another matplotlib version installed that is taking precedence. This will then fall under one of the issues above to correct.
Got the same type of error while using pip. created new VENV and execute the app agaist that resolved my issue
PyCharm requires installing packages available in the interpreter.
You can find matplotlib and other packages available for an install using the following steps:
Open the File--Settings--Project--Project Interpreter menu.
You should see all packages you currently have installed, and matplotlib should be missing from this list.
Click the + (add) button to the right and install the matplotlib package.
Once complete, close the top dialog box, and you should see matplotlib in the list of installed packages.
In my Anaconda 2.2 64bit with Python 3.4.3 the following line works well:
import shapely
But the following line:
from shapely.geometry.polygon import Polygon
returns the following error:
OSError: [WinError 126] The specified module could not be found
What am I missing?
EDIT
I tried with iNotebook, idle.exe and Eclipse. They all use Anaconda (the only Python installation on my computer) and they all show the same error.
If I type from shapely.geometry import Polygon in Eclipse, then I click on Polygon, then I press F3, Eclipse is able to open the module C:\Anaconda3\Lib\site-packages\shapely\geometry\polygon.py. So Eclipse is able to find it, but the execution fails.
EDIT 2
I just tried the same import on another computer with similar configuration and it works. The "only" difference between the two computers is that one has Windows 7 (it works) and one has Windows 8 (it doesn't).
I installed Anaconda and a few packages in both the computers following the same old checklist. The computer with Windows 8 can see the package from Eclipse, but can't import it.
I had a similar problem and it was due to the way that I improperly installed shapely (although this was on a Windows 7 machine, not Windows 8). For the initial install under which I couldn't import the geometry sub-module I installed shapely with the command:
pip install shapely
However after reading the documentation here more closely I saw that for Windows an executable installer needed to be used. That installer is a wheel file that needs to be launched by pip as well. So I uninstalled the first version of shapely with:
pip uninstall shapely
Then I launched the installer via the wheel file like this:
pip install your/file/path/Shapely‑1.5.9‑cp27‑none‑win32.whl
Note that you must download the wheel that matches the specs of the version of python that your applying the package to. I have 32-bit (win32) python 2.7 (p27) so the package above was the correct one. Note that the reference to 32 or 64 bit in the file name refers to the version of python, not to the version of windows.
Try with below one, it solved the issue for me (Windows 10):
conda install -c conda-forge shapely
shapely package docs
Try this:
from shapely.geometry import Polygon
That's how the docs list it:
http://toblerity.org/shapely/manual.html#polygons
Don't use anything but Python version 3.7. Download it now!
Then go to PyCharm (that's what I'm using) and open it, then download the file for your specific version (from here https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely) mine is Shapely-1.6.4.post1-cp37-cp37m-win_amd64.whl (this is respectively version 3.7 (37) and Windows (64) as seen above)
Now with your newly downloaded file, copy and paste it into where you are working from in PyCharm's folder location. For me this is: C:\Users\lewis\PycharmProjects\Project1\NewProject
Then you should see the newly copied file in PyCharm itself as well as where you put it.
Now right click the file in PyCharm Shapely-1.6.4.post1-cp37-cp37m-win_amd64.whl
and click OPEN IN TERMINAL then type:
pip install Shapely-1.6.4.post1-cp37-cp37m-win_amd64.whl
It should say "successful".
Then go into your working area and type:
from shapely.geometry import point
Run it. It should now work for all!
Be in mind that this will ONLY allow for the importing of Shapely in this one environment. So you'll have to copy the Shapely file into new folder locations if you use in other places.
Specs:
Be in mind I am working with a Windows 10, Python interpreter version 37, PyCharm, Virutalenv environment type. Also, make sure in the settings you are indeed in the Python 37 interpreter and not in a conda environment.
I'm running a Fedora 17 (x64) machine, and I tried installing Mayavi for python via
yum install mayavi
which worked fine, except every time I write a code where I call a mayavi module (like mlab) :
from enthought.mayavi import mlab
it yells with the error
********************************************************************************
WARNING: Imported VTK version (5.8) does not match the one used
to build the TVTK classes (5.6). This may cause problems.
Please rebuild TVTK.
********************************************************************************
I've googled, but I don't know how to rebuild TVTK to match with the version of VTK. I suspect this is causing abnormal behaviour, but I can't be sure. How do you rebuild TVTK?
Looks like the visualization toolkit (http://www.vtk.org/) on your machine is newer than the one used to build the classes that are packaged in mayavi. You'd need to rebuild/install visualization toolkit 5.6 (http://vtk.org/VTK/resources/software.html) or have you tried Mayavi2 (http://docs.enthought.com/mayavi/mayavi/installation.html) - that is probably more up to date, visualization toolkit is currently version 5.10.
As of 4.3.1 do not yet support VTK 6.x. This is being worked on currently on master but is not finished yet.
https://github.com/enthought/mayavi/issues/124
I guess you used yum update but didn't do the trick. You can also check for the version in the repository by doing yum info and looking at the version number it displays.
If that does not satisfies you I'm afraid you will have to "use the source"...
hope this will help
I had the same problem and a simple update of mayavi2 did the trick:
pip install mayavi2 -U