I first came across the answer to this question where I found out I needed to install my own backend framework. Since the answer mentioned PyQt4, I chose to go with that. Following links in the doc, I eventually ended up downloaded SIP (pre-req for PyQt4) and then PyQt4 from here. Finally, in my code, I have:
import matplotlib
matplotlib.use('Qt4agg') # need to call use() before importing plt
import matplotlib.pyplot as plt
However I'm still getting this error:
Traceback (most recent call last):
File ".../venv/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py", line 159, in
from PySide import QtCore, QtGui, version, version_info
ImportError: No module named 'PySide'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../program.py", line 7, in
import matplotlib.pyplot as plt File ".../venv/lib/python3.5/site-packages/matplotlib/pyplot.py", line 114,
in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py",
line 18, in
from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py",
line 15, in
from .backend_qt5 import QtCore
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py",
line 31, in
from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, version
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py",
line 162, in
"Matplotlib qt-based backends require an external PyQt4, PyQt5,\n" ImportError: Matplotlib qt-based backends require an external PyQt4,
PyQt5, or PySide package to be installed, but it was not found.
Has anyone experienced this before? Any debugging advice or help on where to go from here?
EDIT: I'll add that trying to import PyQt4 from my virtual environment works, so I'm not sure why matplotlib isn't finding it...
EDIT2: Not sure if it matters but I'm using PyCharm
I struggled with this for days and have finally arrived at a pretty simple solution after looking through loads of different stack overflow posts.
This is my solution for MacOSX and Python3.X, for someone who has already installed python3 via homebrew (i.e. brew install python3) and has virtualenv installed and a virtual environment already created with python3 (e.g. via virtualenvwrapper, mkvirtualenv myvenv -p python3):
(1) Install pyqt and sip using brew:
$ brew install sip --with-python3
$ brew install pyqt --with-python3
(2) Link the pyqt and sip files installed in /usr/local/Cellar/ (the default location for homebrew installations) to the site-packages directory in your virtualenv:
$ ln -s /usr/local/Cellar/sip/{SIPVERSION}/lib/python3.X/site-packages/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.X/site-packages/
$ ln -s /usr/local/Cellar/pyqt/{PYQTVERSION}/lib/python3.X/site-packages/PyQt4/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.X/site-packages/PyQt4
Make sure to modify the text in curly brackets {} as needed for your system, where VIRTUALENVHOME is the path to your virtual environment, VIRTUALENVNAME is the name of it, and SIPVERSION and PYQTVERSION are the versions of sip and pyqt that you downloaded. These can be found by simply looking in their corresponding directories /usr/local/Cellar/sip and /usr/local/Cellar/pyqt (the contents of these should be a single directory with the version number). Also don't forget to plug in the version of python3 you are using! (into all places where it says python3.X)
(3) pip install matplotlib in your virtualenv (if you haven't already) and modify the matplotlibrc so that matplotlib uses the PyQt4 backend instead of the default macosx backend:
# activate your virtual environment
$ workon VIRTUALENVNAME # if you are using virtualenvwrapper
$ #source ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/bin/activate # if you are not using virtualenvwrapper
# if you haven't already, install matplotlib
$ pip install matplotlib
# modify the matplotlibrc file to change the backend it is using
$ nano ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
This will open matplotlibrc in the default terminal text editor (alternatively you can use open -e instead of nano to open it in TextEdit). Go down to the first section after #### CONFIGURATION BEGINS HERE and change the line backend : macosx to backend : Qt4Agg. In the next section, uncomment the line backend.qt4 : PyQt4.
With this done, you should be up and running. You can test whether this worked with the following simple lines of code (assuming you have ipython installed in your virtual environment):
$ ipython -pylab
>>> import numpy as np
>>> plot(np.arange(10))
A plot should appear with a straight line. If this doesn't work, try reinstalling matplotlib in your virtual environment (i.e. pip uninstall matplotlib and pip install matplotlib).
Resolved this issue following this:
pip install matplotlib
There is a directory in you root called ~/.matplotlib.
Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg
Have you though about using conda instead of virtualenv?
conda create -n matplotenv matplotlib
source activate matplotenv
python -c "import matplotlib.pyplot as plt
p = plt.plot([1,2,3])
plt.show()"
source deactivate
I had the same problem, and what I did actually solved it (it might help if you didn't understand everything Jorge said!) :
1. go on anaconda navigator and launch qtconsole
2. type :$ brew install sip --with-python3
$ brew install pyqt --with-python3
3. copy the result of it in python, and execute.
Now try making a graph, it should work!
Related
I am trying to run the PyCharm profiler but I get the following error message:
Traceback (most recent call last):
File "/home/b3053674/ProgramFiles/pycharm-2017.1.4/helpers/profiler/run_profiler.py", line 164, in <module>
profiler.run(file)
File "/home/b3053674/ProgramFiles/pycharm-2017.1.4/helpers/profiler/run_profiler.py", line 89, in run
execfile(file, globals, globals) # execute the script
File "/home/b3053674/Documents/pycotools/pycotools/tasks.py", line 38, in <module>
import viz,errors, misc, _base, model
File "/home/b3053674/Documents/pycotools/pycotools/viz.py", line 53, in <module>
import matplotlib.pyplot as plt
File "/home/b3053674/anaconda2/envs/pycotools/lib/python2.7/site-packages/matplotlib/__init__.py", line 124, in <module>
from . import cbook
ImportError: cannot import name cbook
Snapshot saved to /home/b3053674/.PyCharm2017.1/system/snapshots/pycotools30.pstat
Process finished with exit code 1
I am using a conda environment which I have switched to in the terminal:
$ source activate <env>
then updated matplotlib
$pip install matplotlib --upgrade
and six just for good measure
$pip install six --upgrade
(note that I've also manually deleted these packages from site-directory of my environment and reinstalled just incase --upgrade wasn't behaving)
I've looked here and here and neither solutions solved my problem.
I've also switched the PyCharm Run configurations to use each of my conda environments in turn all of which produce the same error.
Note that the profiler has worked before in the past and the code works fine when I use the normal way of executing the code. Does anybody have any idea of what is going on?
Thanks
I encountered the same error today, although probably for a different reason - the packages for matplotlib and/or six appear to be broken.
Resolved it by downgrading to the previous versions:
conda install six=1.10.0
conda install matplotlib=2.0.2
I had the same issue.
Downgrading to matplotlib 2.0.2 seems to fix it.
pip uninstall matplotlib
pip install matplotlib==2.0.2
I had this error in jupyter but not IPython. Fixed with $ conda install matplotlib=2.0.2 as above but six not downgraded. Don't forget (as I did) to restart the kernel.
I've used the following tutorial to install Python, numpy, scipy and matplotlib:
https://penandpants.com/2012/03/01/install-python-2/
I downloaded each and installed by double-clicking. I'm using a Mac with OS X Yosemite Version 10.10.5
When I try to import numpy, scipy and matplotlib in IDLE I get the follow error message:
`>>> import numpy
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.so: no matching architecture in universal wrapper`
Can anyone please explain to me how to resolve this?
Thanks
use terminal and type
sudo pip install numpy
sudo pip install matplotlib
sudo pip install scipy
for python3+ use pip3
I can't say for sure what's wrong with your installation, but I suspect it's due to a some PATH-conusion... Python 2.7 and Numpy should be installed on your Mac by default.
If you run the command which python, you'll see which executable the python-commands invokes... is it the same as the one you installed?
If you write python in the terminal, and press tab twice, do you see multiple options of installed python versions?
Alternative method of installation
I prefer the Anaconda Python Distribution, which you can download like the full version or minimal version, which is the one I prefer.
If you install this, you should consider uninstalling the one you just installed.
Here's how to install the mini version:
Download the installer, and run it by opening a terminal in the folder with the installer, and running the command bash Miniconda2-latest-MacOSX-x86_64.sh. This will start the text-based installer, and if you read the instructions on screen, you'll be fine.
Notice that you have to scroll to the end of the licence agreement, before you can write yes when asked to accept the terms stated.
Say yes when it asked you if you'd like it to add Anaconda Python to your path.
Anaconda Python is different from other python distributions in the regard that it comes with it's own package and environment manager conda. With conda you can have multiple versions of Python installed at the same time (or several environments of the same Python version, but with different packages or versions of packages).
To create a new environment with the name py27, which is using Python 2.7, write conda create -n py27 python=2.7 --yes. Once created, activate the environment by source activate py27.
With the new environment now active, install the packages you need by using conda: conda install scipy matplotlib --yes.
I'd also recommend installing Ipython, which is an improved shell, and possibly the Jupyter notebook: conda install ipython --yes and conda install notebook --yes.
Hope it helps.
I tried to setup Mayavi on macOS (currently running Sierra) using the following:
brew install --with-qt5 vtk
brew install mayavi
This should theoretically work but now when I try to use Mayavi in my code I get the following error:
Traceback (most recent call last):
File "test.py", line 32, in <module>
mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/figure.py", line 63, in figure
engine = get_engine()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 101, in get_engine
return self.new_engine()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 146, in new_engine
check_backend()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 49, in check_backend
''')
ImportError: Could not import backend for traits
_______________________________________________________________________________
Make sure that you have either the TraitsBackendWx or the TraitsBackendQt
projects installed. If you installed Mayavi with easy_install, try
easy_install <pkg_name>. easy_install Mayavi[app] will also work.
If you performed a source checkout, be sure to run 'python setup.py install'
in Traits, TraitsGUI, and the Traits backend of your choice.
Also make sure that either wxPython or PyQT is installed. wxPython:
http://www.wxpython.org/ PyQT:
http://www.riverbankcomputing.co.uk/software/pyqt/intro
I belive the error lies in the fact that Homebrew only supports Qt5 and PyQT5 while Mayavi is looking for PyQT4 in the background. Is there a way to get Mayavi to work with PyQT5 or is there a way to install PyQT4 with brew?
And of course the problem could lie elsewhere....
Thanks!
An alternative is to use wx instead of qt, see the docs here: docs.enthought.com/mayavi/mayavi/installation.html
Then, set the environment variable export ETS_TOOLKIT=wx - it is worth a try.
I would recommend against using wx; I've run into a mess of issues getting it to run Mayavi examples (not to mention incompatibility with virtual environments). And from the mayavi repo:
Mayavi itself should work with the new wxPython 4.x. However, traitsui, pyface, and other ETS packages do not yet support it so the UI will not work correctly. Older versions should work. PyQt/PySide should work largely out of the box.
Here's how to install Mayavi w/ PyQt4 on macOS Sierra+:
brew install vtk
brew tap cartr/qt4
brew tap-pin cartr/qt4
brew install cartr/qt4/qt
brew install cartr/qt4/pyqt
brew install pyside
pip install mayavi
These steps make sure you get v4, not the default v5. And you may want to do unset ETS_TOOLKIT to make sure mayavi doesn't look for a different backend.
FWIW PyQt5 is not available (at least as a PyPI package) for Python 2x -- I see you're running Python 2.7. You can do pip3 PyQt5, but mayavi dependencies will need extra setup for Python 3 -- specifically pip3 install traitsui.
Um... I wanted to import matplotlib in Python IDLE, but it said
>>> import matplotlib
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import matplotlib
ModuleNotFoundError: No module named 'matplotlib'
I've already tried pip thing and the conda thing in terminal.
It said
# All requested packages already installed.
# packages in environment at /Users/Dan/anaconda:
Then! what's the problem here?
You may have installed Matplotlib to Python 2.7 and not Python 3.6. To install it to Python3 you need to use pip3:
pip3 install matplotlib
Then open python3.6 from your terminal (not idle) and try importing matplotlib from there. This way you can use any text editor you wish, and run your code from the terminal.
I had the same issue using the same command in IDLE. I just figured it out. go to your terminal and enter:
pip3.6 install matplotlib
I was using pip3 instead of pip3.6
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)