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
Related
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! :)))
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..)
Initially I was getting this error (No Module name was found scipy) So I installed a Scipy wheel file. Now I don't get the same error any more but I get cannot import name "_ccallback_c".
The error seems to be triggered at the fourth line of code. I have done my research and saw that other people suggested to try an environment such as Anaconda. I have seen it work on idle, and that solution isn't ideal for me.
Traceback:
Traceback (most recent call last):
File "C:\Users\joesh\Desktop\Python\Machine Learning\1st tutorial.py", line 4, in <module>
from sklearn import preprocessing, cross_validation, svm
File "C:\Users\joesh\Desktop\Python\lib\site-packages\sklearn\__init__.py", line 134, in <module>
from .base import clone
File "C:\Users\joesh\Desktop\Python\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
File "C:\Users\joesh\Desktop\Python\lib\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\joesh\Desktop\Python\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
And the code:
import pandas as pd
import quandl, math
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
I had the same error on USING Anaconda, so I am not sure if using it would make any difference.
I solved it by just uninstalling scipy and re-installing it using pip:
pip uninstall scipy
you'll get this message:
Uninstalling scipy-1.1.0: Would remove:
c:\users\thesh\appdata\local\programs\python\python36-32\lib\site-packages\scipy-1.1.0.dist-info*
c:\users\thesh\appdata\local\programs\python\python36-32\lib\site-packages\scipy*
Proceed (y/n)?
press y, and after pip is done, type:
pip install scipy
Having just moved to MSVS 2017 for Python (ML) development, I encountered this and other errors related to missing modules. The problem (and all related problems like it) has a frustratingly simple solution: when I originally started coding in Python, I installed everything from the command line - apparently, MSVS 2017 doesn't "see" those installations (and, in fact, they sometimes conflict, since the underlying python may be tapping older libs); so, the solution is to:
Use the command line version of 'pip uninstall ...' where '...' is the library having missing dependencies (scipy, in this case). Then, in the MSVS 2017 command line on the Python environment window (usually, top right in the default display configuration), reload the library (in this case, typing 'scipy' will format a command line for execution [in the list control below the command textbox]) that will read something like 'pip install scipy' (or whatever library needs to be reinstalled for MSVS).
You may have to do this for many (or all) of your previous Python package installations where these missing module errors persist.
Can be resolved, by uninstalling and reinstalling using pip on Anaconda Prompt:
pip uninstall scipy
After the uninstall, you can reinstall with:
pip install scipy
When you installed scipy with pip in a Python version 3.6 and later try to run your code with Python 3.7 you will encounter this problem. So one solution is to uninstall scipy
pip3 uninstall scipy
and reinstall it (using an environment with Python 3.7):
pip3 install scipy
This will make sure that the installed version of scipy is compatible with your version of Python.
PS: When you updated Python from Python 3.6 to Python 3.7 it might be necessary to also reinstall pip, so that pip will use the correct version of Python internally.
I ran into this when I was following these instructions on how to use a virtual environment to use the pre-built version of SciPy. The simplest solution for me was to simply comment out from . import _ccallback_c under scipy\_lib\_ccallback.py.
I first had the error with scipy. So I ran the command python -m pip install -user numpy scipy matplotlib ipython jupyter pandas sympy noseand it worked perfectly. I was installing everything with pip, so I decided to use Anaconda. I installed and checked to add to the PATH. From there, the same code that was executed before normally stopped working and displays the error similar to that of the question. I uninstalled Anaconda and it is now working again.
Erro:
$ winpty python ia.py
Traceback (most recent call last):
File "ia.py", line 11, in <module>
from sklearn import tree #importando a biblioteca e a árvore p/ o classifica
dor
File "C:\Users\ferna\Anaconda3\lib\site-packages\sklearn\__init__.py", line 13
4, in <module>
from .base import clone
File "C:\Users\ferna\Anaconda3\lib\site-packages\sklearn\base.py", line 11, in
<module>
from scipy import sparse
File "C:\Users\ferna\AppData\Roaming\Python\Python36\site-packages\scipy\__ini
t__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\ferna\AppData\Roaming\Python\Python36\site-packages\scipy\_lib\
_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
Código:
from sklearn import tree #importando a biblioteca e a árvore p/ o classificador
#COLLLECT TRAINING DATA
features = [[140,1],[130,1],[150,0],[170,0]]
labels = [0,0,1,1]
# TRAIN CLASSIFIER
clf = tree.DecisionTreeClassifier() #Classificador
clf = clf.fit(features, labels) #algoritmo de decisão p/ encontrar padrões
#MAKE PREDICTIONS
print(clf.predict([[160, 0]])) #entrada de dados para o tratamento
Try this:
python -m pip install --upgrade scipy
After digging in, to give the full background on this, first of all SciPy relies on having NumPy already installed. The SciPy wheel's setup.py file uses NumPy functionality to configure and install the wheel.
SciPy setup.py:
...
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
Secondly, when just trying to use the wheel, if you run into this error, you can see after inspecting the wheel's files that the reason is the binary wheels have a naming convention where the shared object file, here it's called _ccallback_c.so, is instead named based on the architecture that the binary wheel supports. When trying to import the shared object by file name in /_lib/_ccallback.py it can't find it, hence this error (line 1 in /_lib/_ccallback.py) because, instead of being named _ccallback_c.so it's called _ccallback_c.cpython-36m-x86_64-linux-gnu.so or another architecture variation:
from . import _ccallback_c
These file names seem to be an artifact of libraries that are using Cython and Cython's adherence to PEP 3149 (ABI version tagged .so files). But the easiest fix is to change the .whl extension to .zip and rename all those relevant .so files to not contain the architecture snippet. Then change .zip -> .whl and it should be good to go unless it's the wrong architecture for the platform you're using, in which case you need to download the appropriate platform wheel for your platform.
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)
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.