Gtk* backend requires pygtk to be installed - python

From within a virtual environment, trying to load a script which uses matplotlib's GTKAgg backend, I fail with the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 16, in <module>
raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.
The code which I ran in order to produce that ImportError is as follows:
import matplotlib as mpl
mpl.use('GTKAgg')
import matplotlib.pyplot as plt
When running the very same code after deactivating my virtual environment, everything goes well.
I assumed this may be due to version differences; indeed, such differences exist on my machine. However, the version in the virtual environment is newer (1.2.0 versus 1.1.1rc), so I am not expecting less support.
In case it is not clear: my question is how to allow importing pyplot with GTKAgg backend on a new version of matplotlib, or at least an attempt to understand the reasons for this import failure.

You probably created your virtual evn by something like:
$ virtualenv ~/.virtualenvs/my_env
by default this can see none of your system-installed packages (including pygtk) so when you try to run mpl it rightly complains that you do not have pygtk installed because (with in the context of the virtualenv) you do not.
You can either build and install pygtk within your virtualenv or you can use
$ virtualenv --system-site-packages ~/.virtualenvs/my_env
(doc) which will make your virtualenv inherit from your global packages.

I'm not sure if you're on Ubuntu, but to solve this I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:
$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install
Changing backends and using --system-site-packages didn't work for me.

Related

tkinter on python3.6 with multiple versions on arch

I am trying to test out tensorflow object detection. Because tensorflow requires python3.6 and my system came with python3.7.1 installed by default I built python3.6.7 from source. I then created a venv with python3.6.7 and install all my dependencies but I end up getting an error when trying to import matplotlib and get the error of:
File "detection.py", line 9, in <module>
from matplotlib import pyplot as plt
File "/home/johnmadden/repos/tfobj/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2374, in <module>
switch_backend(rcParams["backend"])
File "/home/johnmadden/repos/tfobj/lib/python3.6/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
backend_mod = importlib.import_module(backend_name)
File "/home/johnmadden/repos/tfobj/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/johnmadden/repos/tfobj/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
from . import _backend_tk
File "/home/johnmadden/repos/tfobj/lib/python3.6/site-packages/matplotlib/backends/_backend_tk.py", line 5, in <module>
import tkinter as Tk
File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I looked online and found out that I needed to install tk with
pacman -S tk
And I still get the same error, but when I go to python3.7.1 it import sucessfully. I think that it is trying to install it for python3.7.1 and not python3.6.7. I know that on ubuntu people can do
sudo apt-get install python3-tk
How would this be accomplished on arch?
Found my problem, I created venv before installing tk so it was still missing it. I just recreated the virtual environment.
The actual answer is that tensorflow does not require python 3.6 -- Arch Linux provides a tensorflow package that has been patched to build against python 3.7, and you should use that instead. If you build your own python and tensorflow, then you run the risk of messing up part or all of the installation (in this case, failing to get tkinter correctly set up).
The first thing you should always do is check if the software you want is already available in the distribution repositories -- if it is, then the package is definitely intended to work with the ecosystem provided by those distribution repositories.
In this case, you can see that the tensorflow package has a number of patches to fix build failures on python 3.7: https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/tensorflow&id=ef98a003237250ba0853990da1742ee27c0e554d#n37

Julia PyPlot not working [Conda, Python issues]

This is the error I get when I run my program. I am using Ubuntu 16.04 and had Python 2.7.12 installed. I installed anaconda hoping that would help solve the problem but it didn't. Now python -V gives me Python 2.7.14 :: Anaconda, Inc.
When I run Conda.list() inside julia, I do get "matplotlib" and "basemap"
(includes mpl_toolkits, I think) and when I run conda list outside julia (bash?) I get a longer list which also contains "matplotlib" and "basemap".
sys:1: UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.
The backend was *originally* set to u'Qt5Agg' by the following code: File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
line for line in traceback.format_stack()
WARNING: No working GUI backend found for matplotlib WARNING: Couldn't initialize pyplot. (might need to install it?) INFO: To do a standard install of pyplot, copy and run this:
if !Plots.is_installed("PyPlot")
Pkg.add("PyPlot") end withenv("PYTHON" => "") do
Pkg.build("PyPlot") end
# now restart julia!
ERROR: LoadError: InitError: PyError (ccall(#pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)
The Python package matplotlib.pyplot could not be found by pyimport. Usually this means that you did not install matplotlib.pyplot in the Python version being used by PyCall.
PyCall is currently configured to use the Python version at:
/home/myusername/anaconda2/bin/python
and you should use whatever mechanism you usually use (apt-get, pip, conda, etcetera) to install the Python package containing the matplotlib.pyplot module.
One alternative is to re-configure PyCall to use a different Python version on your system: set ENV["PYTHON"] to the path/name of the python executable you want to use, run Pkg.build("PyCall"), and re-launch Julia.
Another alternative is to configure PyCall to use a Julia-specific Python distribution via the Conda.jl package (which installs a private Anaconda Python distribution), which has the advantage that packages can be installed and kept up-to-date via Julia. As explained in the PyCall documentation, set ENV["PYTHON"]="", run Pkg.build("PyCall"), and re-launch Julia. Then, To install the matplotlib.pyplot module, you can use `pyimport_conda("matplotlib.pyplot", PKG)`, where PKG is the Anaconda package the contains the module matplotlib.pyplot, or alternatively you can use the Conda package directly (via `using Conda` followed by `Conda.add` etcetera).
) <type 'exceptions.ImportError'> ImportError("/lib/x86_64-linux-gnu/libz.so.1: version `ZLIB_1.2.9' not found (required by /home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/../../.././libpng16.so.16)",) File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 29, in <module>
import matplotlib.colorbar File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
import matplotlib.contour as contour File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
import matplotlib.font_manager as font_manager File "/home/myusername/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
Stacktrace: [1] pyerr_check at /home/myusername/.julia/v0.6/PyCall/src/exception.jl:56 [inlined]....
LD_LIBRARY_PATH="/home/myusername/.julia/v0.6/Conda/deps/usr/lib/" julia
did it!
Thanks Github!
I am also using Ubuntu 16.04 and use pyplot with julia. I installed matplotlib with pip and use it with the TK backend (the most light-weight and easy to install backend). Please try these shell commands to install python-tk and matplotlib in your home directory:
sudo apt-get install python-tk
unset PYTHON # to use the default python interpreter /usr/bin/python
pip2 install --user --upgrade matplotlib
julia --eval 'Pkg.build("PyCall")'
julia --eval 'Pkg.build("PyPlot")'
To use the default python interpreter, make sure that you do not set the environment variable PYTHON. The following should return nothing:
julia --eval '#show get(ENV,"PYTHON",nothing)'
If PyCall are PyPlot are not already installed, then use add instead of build:
julia --eval 'Pkg.add("PyCall")'
julia --eval 'Pkg.add("PyPlot")'

import matplotlib.pyplot gives ImportError: dlopen(…) Library not loaded libpng15.15.dylib

I am aware that this exact same question has been asked before. I did follow the instructions given in the answer there, and it didn't solve my problem (and I don't have enough reputation to just comment on the Q or A in that thread). Anyway, here's what's going on:
I try to do:
import matplotlib.pyplot
And in return I get:
Traceback (most recent call last):
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3032, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-eff513f636fd>", line 1, in <module>
import matplotlib.pyplot as plt
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 22, in <module>
from matplotlib.mathtext import MathTextParser
File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py", line 63, in <module>
import matplotlib._png as _png
ImportError: dlopen(/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: libpng15.15.dylib
Referenced from: /Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/_png.so
Reason: image not found
My Python version:
2.7.7 |Anaconda 2.0.1 (x86_64)| (default, Jun 2 2014, 12:48:16) [GCC 4.0.1 (Apple Inc. build 5493)]
EDIT:
cel's suggestion worked! I just tried "conda remove matplotlib", "pip install matplotlib", and then "conda install matplotlib", and presto! Man, you have no idea how long this problem has vexed me. Bless you all.
Some python packages link dynamically against native c libraries. After an update of one of those libraries, links can break and give you weird error messages about missing dynamic libraries, as seen in the error message in the question.
Basically, after an update of a native library sometimes you also have to rebuild python packages (here matplotlib).
The above statement is true in general. If you are using conda as your python distribution things are usually less complicated:
For extension packages conda also maintains required c libraries. As long as you use only conda install and conda update for installing those packages you should not run into these issues.
For numpy, scipy, matplotlib and many more I would suggest to try conda search <library name> first to see if there's a conda recipe that matches your needs. For most users conda install <library name> will be a better option than pip install.
To make sure that only conda's version is installed you can do
conda remove matplotlib
pip uninstall matplotlib
conda install matplotlib
Afterwards this issue should not appear anymore.
I ran into this problem as well. I updated my Anaconda-Navigator and the next time I opened a project with matplotlib.pyplot, I ran into a similar problem. What worked for me was:
conda install libpng
I had this problem, but it was because I had set
export DYLD_LIBRARY_PATH="/Users/charlesmartin14/anaconda/lib":$DYLD_LIBRARY_PATH
removing this setting and restarting the shell fixed it

ImportError: No module named backend_tkagg

I have such imports and code:
import pandas as pd
import numpy as np
import statsmodels.formula.api as sm
import matplotlib.pyplot as plt
#Read the data from pydatasets repo using Pandas
url = './file.csv'
white_side = pd.read_csv(url)
#Fitting the model
model = sm.ols(formula='budget ~ article_size',
data=white_side,
subset=white_side['producer'] == "Peter Jackson")
fitted = model.fit()
print fitted.summary()
After execution of this code I have such errors:
/usr/bin/python2.7 /home/seth/PycharmProjects/osiris_project/PMN_way/start.py
Traceback (most recent call last):
File "/home/seth/PycharmProjects/osiris_project/PMN_way/start.py", line 5, in <module>
import matplotlib.pyplot as plt
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 98, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
ImportError: No module named backend_tkagg
Process finished with exit code 1
I`m using openSUSE and pycharm community edition latest version with installed pandas, numpy, etc
How can i fix this problem?
I've seen this before, also on openSUSE (12.3). The fix is to edit the default matplotlibrc file.
Here's how you find where the default matplotlibrc file lives, and where it lives on my machine:
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
The backend setting is the first configuration option in this file. Change it from TkAgg to Agg, or to some other backend you have installed on your system. The comments in the matplotlibrc file list all backends supported by matplotlib.
The backend specified in this file is only the default; you can still change it at runtime by adding the following two lines, before any other matplotlib import:
import matplotlib
matplotlib.use("Agg") # or whichever backend you wish to use
I use openSuse 13.1 and had the same error "ImportError: No module named backend_tkagg".
I solved it by using this suggestion: http://forums.opensuse.org/showthread.php/416182-Python-matplolib.
I've installed the python-matplotlib-tk package, and now it is working just fine.
E.g. you can use: zypper install python-matplotlib-tk
I tried various solutions, only this works for me:
sudo pip install matplotlib --upgrade
I was able to fix this by putting
import matplotlib.backends.backend_tkagg
above
import matplotlib.pyplot as plt
Note, I received the same error while trying to run an executable generated using Py2exe.
Here is what I got when I ran TheProgram.exe from the command prompt:
>>TheProgram.exe
Traceback (most recent call last):
File "ThePythonScriptToMakeIntoExe.py", line 14, in <module>
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\Python34\lib\site-packages\matplotlib\backends\__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
ImportError: No module named 'matplotlib.backends.backend_tkagg'
I add my answer for those who might encounter this problem using CARLA Simulator.
Using Python 3.6, and installing matplotlib v 2.2.5 solved this problem for me.
To remove matplotlib and install the right version you can run
pip uninstall -r requirements.txt
Then add matplotlib==2.2.5 to the requirement file and run pip install -r requirements.txt --user to install the correct versions.
If you are missing the requirements.txt file, here you are
Pillow>=3.1.2
numpy>=1.14.5
protobuf>=3.6.0
pygame>=1.9.4
matplotlib==2.2.5
future>=0.16.0
scipy>=0.17.0
In case you have different python versions and you would like to use a specific python version using py -<version> will use the right python version.
WARNING: Changing Path variable of the current Python version might be dangerous and specially for linux users it might cause that you can not open the terminal again. This is because different Python versions might not be compatible with the current pip version you are using. If it is too late for you and you already messed up with your terminal you can visit This tutorial to fix your issue.
After all of the fixes you might still get this error:
TypeError: blit() got an unexpected keyword argument 'colormode'
So you might want to edit the live_plotter.py file and remove colormode attribute from all the lines which are passing it to blit() method.
I faced this issue before, I solved it by using an old version of matplotlib which is version 3.0.0
if you don't have this version try pip install matplotlib==3.0.0
hope this was helpful!
I faced this issue while doing live plotting using the 2.2.2 version. Tried the following:
pip install matplotlib==3.0.0 --user
It's still an older version but it worked.
I solved the problem by setting the variable before starting python
export MPLBACKEND=Agg; python3
Taken from here.

Possible conflicting python installations

I'm not sure if the default python installation is the one that I've been installing modules to, and if that may be the cause of a conflicting Unicode byte size compatibility error. In short, I've installed Numpy 1.7 using Python 2.7.3 and when I try to install this other program that uses Python and Numpy as dependencies, I get this error:
Traceback (most recent call last):
File "setup.py", line 20, in <module>
from weblogolib import __version__
File "/home/chris/Documents/IS/Bioinformatics-Software/weblogo-3.3/weblogolib/__init__.py", line 108, in <module>
from numpy import array, asarray, float64, ones, zeros, int32,all,any, shape
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS4_AsUnicodeEscapeString
So I guess I have a conflicting unicode byte size (2-byte vs. 4-byte). I went to check to see if I had conflicting versions of Python that could be messing this up.
python --version
Python 2.7.3
But this seems at odds with
which python
/usr/local/bin/python
When I go to /usr/local/bin I find these files (relevant to python):
python
python2
python2.7
python-config
python2-config
python2.7-config
Now I've installed numpy into the dist-packages directory of /usr/lib/python2.7/dist-packages which corresponds to what I get for python --version. But the fact that when I try which python and get a directory for python and not python2.7 concerns me that this might be conflicting when I try to install the program that uses python and numpy as dependencies.
So I guess to clarify my question(s): Are these normal files to find for a python installation or have I somehow installed three different versions? Could they be causing my error with the unrecognized symbol? Is there a way to uninstall if they are indeed extraneous versions?
Thanks for any help you can provide!
Oh and here is a link to a previous question I had, where I edited the PYTHONPATH while trying to fix an ImportError I was getting, if that might be affecting things....ImportError: No module named numpy
Here are the results of trying virtualenv:
chris#ubuntu:~/Documents/IS/Bioinformatics-Software$ virtualenv weblogo-3.3
New python executable in weblogo-3.3/bin/python
Installing setuptools.............done.
Installing pip...............done.
chris#ubuntu:~/Documents/IS/Bioinformatics-Software$ cd weblogo-3.3
chris#ubuntu:~/Documents/IS/Bioinformatics-Software/weblogo-3.3$ source bin/activate
(weblogo-3.3)chris#ubuntu:~/Documents/IS/Bioinformatics-Software/weblogo-3.3$ pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/lib/python2.7/dist-packages
Cleaning up...
The problem indeed seems to be a mismatch of Python and Numpy compile settings.
/usr/local/bin is where custom Python is installed, you should try to run using /usr/bin/python instead.
Another solution is to use a virtualenv. Try this:
virtualenv myproject
cd myproject
source bin/activate
pip install numpy
Basically virtualenv sets up a different Python installation with its own packages in the "myproject" directory. Running the "activate" command tells the system that you want to use this installation instead of the default system. This lets you have a different Python environment for different projects. Using virtualenv, each project can have its own versions of Python packages even if they're incompatible with other projects or system packages.
Note you will have to repeat the "source" command each time you open a new shell and want to use that virtual environment. Also you might have to install the virtualenv command by using your OS package manager. If this isn't possible (e.g. you don't have root access) or your OS version is too old for some reason, you can also download it manually from https://pypi.python.org/packages/source/v/virtualenv/
If you do ls -l /usr/local/bin/python* you should see that python and python2 are actually symlinks to python2.7, and likewise python-config and python2-config are symlinks to python2.7-config.
What OS are you on? This is more a question for superuser, but try something like this. Ditch easy_install and use pip if you haven't already.
On Ubuntu:
sudo apt-get install python-setuptools
sudo easy_install pip
pip install --user numpy

Categories