matplotlib.pyplot Error "ImportError: cannot import name '_path'" - python

I came across an ImportError in the importing of matplotlib.pyplot on Windows 10 Pro
Here is the full error:
Traceback (most recent call last):
File "C:\Users\****\Dropbox\Code\Python\lib_test.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\pyplot.py", line 31, in <module>
import matplotlib.colorbar
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py", line 16, in <module>
from .path import Path
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\path.py", line 21, in <module>
from . import _path, rcParams
ImportError: cannot import name '_path'
Here is the script:
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()
If anyone could help me, much appreciated.

It can be a problem related to matplotlib version. I had the same problem and solved it uninstalling the existing version of matplotlib (in my case with conda but the command is similar substituing pip to conda) so:
firstly uninstalling with:
conda uninstall matplotlib (or pip uninstall matplotlib)
and after the successful removal of the existing version (mine was 2.2.2), the following command installed a successive version, that can be specified with '==' after the module name:
conda install matplotlib==3.0.2 (or pip install matplotlib==3.0.2)
and the error disappears.
The uninstall command removed also seaborn installation so it was necessary to install it after matplotlib but it was easy...
Yeah!:)

Since you're probably like me and doing a lot of installing/importing, it can be just a mishap in the original matplotlib install... so try doing the ol' get out, walk around to the back side and give it a kick...
conda uninstall matplotlib
#let it do its thing
conda install matplotlib
#let it do its thing
Then test it on the code you're trying to do and see if it works. In my case, it came down to a "_Path" issue in my original install that messed up things. After the uninstall/install, I was fine.

Dunno what mpip is,, or why,, try plain old pip (or in your case perhaps pip3 ? This may have more to do with how the base Python was installed though,, why I suggest using Anaconda to install all (which automatically installs MPL as well..)

Related

Matplotlib cannot be used even installed - errors

Hi everybody since im new to python 3 I tried to install Matplotlib in python but after installment there are multiple occuring errors
after importing matplotlip:
import matplotlib.pyplot as plt
and run the program I get:
Traceback (most recent call last):
File "C:/Users/Panos/Desktop/Python/for_loop/for_loop.py", line 34, in <module>
import matplotlib.pyplot as plt
File "E:\Program Files\Python\lib\site-packages\matplotlib\__init__.py", line 174, in <module>
_check_versions()
File "E:\Program Files\Python\lib\site-packages\matplotlib\__init__.py", line 159, in _check_versions
from . import ft2font
ImportError: DLL load failed while importing ft2font: The specified module could not be found.
have anyone faced the same problem with matplotlib package using PyCharm as well?
other packages as Numpy or e.g. Pandas are working fine..
#Pan Pan try to install matplotlib 3.0.3 it fixed ft2font problem for some cases
pip uninstall matplotlib
pip install matplotlib==3.0.3
well I just tried a new install of an oldest version and it finally worked!
I didn't use pip install - I just went to PyCharms interpreter and selected matplotib package (using "+" button in the bottom left corner).
Then I checked to specify the version of the matlibplot package and after several efforts of installing-uninstalling matplotlib version the --> 3.1.3 worked for me without errors
from matplotlib import ft2font
import matplotlib.pyplot as plt
Hope that helps people with the same issue
#Amin Kh thank you for your initial suggestion! :)))

Matplotlib issue on OS X ("ImportError: cannot import name _thread")

At some point in the last few days, Matplotlib stopped working for me on OS X. Here's the error I get when trying to import matplotlib:
Traceback (most recent call last):
File "/my/path/to/script/my_script.py", line 15, in <module>
import matplotlib.pyplot as plt
File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 34, in <module>
from matplotlib.figure import Figure, figaspect
File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 40, in <module>
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/Library/Python/2.7/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
from ._subplots import *
File "/Library/Python/2.7/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "/Library/Python/2.7/site-packages/matplotlib/axes/_axes.py", line 22, in <module>
import matplotlib.dates as _ # <-registers a date unit converter
File "/Library/Python/2.7/site-packages/matplotlib/dates.py", line 126, in <module>
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
File "/Library/Python/2.7/site-packages/dateutil/rrule.py", line 14, in <module>
from six.moves import _thread
ImportError: cannot import name _thread
The only system change I can think of was the Apple-forced NTP update and maybe some permission changes I did in /usr/local to get Brew working again.
I tried reinstalling both Matplotlib and Python-dateutil via Pip, but this did not help. Also tried a reboot. I'm running Python 2.7.6, which is located in /usr/bin/python. I'm running Yosemite (OS X 10.10.1).
sudo pip uninstall python-dateutil
sudo pip install python-dateutil==2.2
I had the same error message this afternoon as well, although I did recently upgrade to Yosemite. I'm not totally sure I understand why reverting dateutil to a previous version works for me, but since running the above I'm having no trouble (I generally use pyplot inline in an ipython notebook).
This problem is fixed in the latest six and dateutil versions. However, in OS X, even if you update your six to the latest version, you might not actually update it correctly. This is what happened to me:
After doing a pip2 install six -U, the new six module was installed in /Library/Python/2.7/site-packages/. However, when I loaded six in a python 2.7 terminal, and checked its path, this is what I got:
import six
print six.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc
So, python was using an old version of six, which I removed by typing:
rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.*
This fixed this issue for me.
Installing the python-dateutil==2.2 did not work for me.
But a quick-and-dirty workaround did work! I replace six.py in python 2.7 with the six.py from python 3.4 (virtualenv). Since, I have the problem in 2.7 but not 3.4.
UPDATE
I had the same problem again after reinstalling python (and after upgrading to El Capitan). Un-obvious thing is that this error occurs only in the IPython shell and notebook (when I do import matplotlib.pyplot as plt) but works fine from a Python shell.
So a better solution (that did work in my case) without a dirty work-around is to force install both six and ipython. Here is what I did to have this fixed :
$ pip install --ignore-installed six
$ pip install --ignore-installed ipython
It is possible that you have a perfectly installed version of any packages you have installed, but the version used by default is not the one you want. You can see the list of paths that python search from in order to find its packages as follows:
>>> import sys
>>> sys.path
In order to let python search first the most updated version of certain package, instead of removing the system version, what can be done is to set the system variable PYTHONPATH in the ~/.bash_profile (or ~/.bashrc if linux) config file to the path where the new packages are installed:
export PYTHONPATH=/Library/Python/2.7/site-packages
An alternative is to modify the python path inside your python script by adding the path at the beginning of the path list:
import sys
sys.path.insert(1,'/Library/Python/2.7/site-packages')
This needs to be done for every script you need a certain package version. You might want for some reason use an older version that you have installed.
BTW all my installations with easy_install, or pip, or from sources go to /Library/Python/2.7/site-packages
This worked en EL Capitan, and now also in macOS Sierra (10.12.2)

'ImportError: No module named pytz' when trying to import pylab?

As far as I can tell, I don't even need pytz for what I'm doing.
I'm re-learning python for image processing using the O'Reilly book 'Programming Computer Vision with Python' for work (and I'm also new to mac, so on both counts I apologise if this is a stupid question).
Along with a 'empire.jpg' picture, I'm trying to run the script on page 16 which goes as follows:
from PIL import Image
from pylab import *
# read image to array
im = array(Image.open('empire.jpg')) # plot the image
imshow(im)
# some points
x = [100,100,400,400]
y = [200,500,200,500]
# plot the points with red star-markers
plot(x,y,'r*')
# line plot connecting the first two points
plot(x[:2],y[:2])
# add title and show the plot
title('Plotting: "empire.jpg"')
show()
but I get the following output with an error:
File "plotch1.py", line 2, in <module>
from pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/pylab.py", line 208, in <module>
from matplotlib import mpl # pulls in most modules
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/mpl.py", line 4, in <module>
from matplotlib import axes
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 18, in <module>
from matplotlib import dates as mdates
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/dates.py", line 82, in <module>
import pytz
ImportError: No module named pytz
I'm using OS X 10.9.4.
I've installed matplotlib and numpy from macpython.org and I've installed scipy 0.11.0 all for python 2.5.
Do I even need pytz? If not, how can I get around this error?
pylab requires pytz.
The easiest way to install a package in Python is to run pip install pytz.
Today, Python comes with pip pre-installed, but use these instructions if you need to install it: Installation: Do I need to install pip?
Firstly if you have installed pip then remove it by deleting the folder of pip inside python directory. Then install pip by this command:
$ sudo easy_install pip
Then you need to install pytz again by this command:
$ sudo pip install pytz
Don't update pip to 10th version because it might contain bugs which is causing problems.

Installing Pylab/Matplotlib

I'm trying to write a program that plots a graph, which made me look into
Matplotlib.
I found a tutorial that started out with this little program, that worked fine:
from pylab import *
def main():
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C,S = np.cos(X), np.sin(X)
plot(X,C)
plot(X,S)
show()
if __name__ == '__main__':
main()
Then I tried to run it on another computer, where it did not work at all. I tried to download Pylab and Matplotlib. When I had installed Matplotlib it demanded something called dateutil, when I got dateutil it demanded something called six. I downloaded six, but it didn't work properly.
It doesn't feel like I'm on the right track. What should I do to get a proper installation?
EDIT:
I'm using Python 2.7 on Windows 7.
The error I get is
Traceback (most recent call last):
File "C:\Users\Python\mscript\listdb2.py", line 19, in <module>
from pylab import *
File "C:\Python27\lib\site-packages\pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python27\lib\site-packages\matplotlib\pylab.py", line 226, in <module>
import matplotlib.finance
File "C:\Python27\lib\site-packages\matplotlib\finance.py", line 21, in <module>
from matplotlib.dates import date2num
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 119, in <module>
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
File "C:\Python27\lib\site-packages\dateutil\rrule.py", line 18, in <module>
from six import advance_iterator, integer_types
ImportError: No module named six
The file six.py is located in C:\python27\Lib\site-packages\six\six.py
The six directory also contains a file called test_six.py. If I try to run this program I also get an error:
Traceback (most recent call last):
File "test_six.py", line 5, in <module>
import.py
ImportError: No module named py
If Anaconda installed and it is already in your environment path, you can get it simply using
conda install matplotlib
in command line and then call in Python with
from pylab import *
This work for me fine as "pip install" and "easy_install" both on Win and Linux caused a lot of issues
Installing packages on *nix is easy using pip. Pip allows you to easily install packages from the Python Package Index (PyPI) with a simple pip install matplotlib. This should install all dependencies, but if it does not then you can install them manually (for instance pip install python-dateutil).
Using pip with Windows is possible though slightly more difficult for packages that require compilers and such. However, installing Python programs on Windows is simple if you use these Windows binaries provided by Christoph Gohlke.
The particular ones for matplotlib can be found here. Note that you can also find similar binaries for python-dateutil, six, etc if necessary.
*As an aside: I would strongly suggest you look into using the full matplotlib.pyplot API rather than pylab. It's much more powerful and useful, but this is just some aside advice :) *

Error when "import matplotlib.pyplot as plt"

I did not have any problem to use "plt", but it suddenly shows an error message and does not work, when I import it. Please see the below.
>>> import matplotlib
>>> import matplotlib.pyplot as plt
Output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 6, in <module>
from matplotlib.figure import Figure, figaspect
File "/usr/lib64/python2.6/site-packages/matplotlib/figure.py", line 18, in <module>
from axes import Axes, SubplotBase, subplot_class_factory
File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 2, in <module>
import math, sys, warnings, datetime, new
File "new.py", line 12, in <module>
import matplotlib.pyplot as plt
AttributeError: 'module' object has no attribute 'pyplot'
This package is suddenly corrupted. So, I tried to install as below. I use Ubuntu.
In [1]: sudo apt-get install python-matplotlib
File "<ipython-input-1-2400ac877ebd>", line 1
sudo apt-get install python-matplotlib
^
SyntaxError: invalid syntax
If I need to reinstall, what are the detailed instructions on how to do it?
I am very new to Python. So, my problem might be too simple to be solved. But I cannot.
Try to type sudo apt-get install python-matplotlib in a terminal. In your message you typed it in the IPython console. This is a Bash command, not a Python one.
You need to quit IPython before. For that use Ctrl + D.
I have this problem before.
What the conditions of mine is that I have install multiple versions of Python and NumPy in my OS. That's why the Python interpreter can't find the right NumPy to support the Matplotlib library.
The method I chose to solve this problem was modifying the $PYTHONPATH variable by editing the file .bashrc in my home path.
Change the order of the Python install path as shown, like putting the path "/usr/lib/python2.7/dist-packages" in the front of the $PYTHONPATH.
The correct answer to resolve this issue was found at Pylance reports that the import "matplotlib.pyplot" could not be resolved from source via a comment by jakebailey.
He says, "Did you install matplotlib into the environment while VS Code was running? If you reload, does this message go away?"
Visual Studio Code doesn't refresh anything. So, after you install anything via pip or pip3, it is a good idea to:
Save your work.
Close Visual Studio Code.
Restart Visual Studio Code.
This fixed it for me.
Something is wrong with Tkinter I guess. Try adding
import matplotlib as mpl
mpl.use('TkAgg')
in the front of import matplotlib.pyplot as plt.
For terminal:
If you are using Python 2, then write this:
pip install matplotlib
If you are using Python 3, then write this:
pip3 install matplotlib
Try to do this:
python3 -m pip install matplotlib

Categories

Resources