Python3 ImportError: No module named '_tkinter' on Ubuntu - python

The current version of Python3 is 3.5.2 when I import matplotlib it retuned the following error
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python3.5/site-packages/six.py", line 92, in __get__
result = self._resolve()
File "/usr/local/lib/python3.5/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python3.5/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
And import tkinter
Python 3.5.2 (default, Jan 19 2017, 11:29:22)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
>>>
It seems that tkinter has already been intalled.
I have installed tk and tcl by
sudo apt-get install tk-dev
sudo apt-get install tk8.6-dev
And OS is Ubuntu 14.04.
I think it is the reason that TK was not configured on Python3, but I'm not sure. Many people said I should rebuild and reinstall Python3 with tk, however I don't think it is an elegant way to solve this problem.
How can I fix this problem?

If you are having trouble with a matplotlib backend try selecting a different one.
Matplotlib caters for many different scenarios and uses.
On Linux, I use the following code to select whichever backend is available and works first.
import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
try:
matplotlib.use(gui,warn=False, force=True)
from matplotlib import pyplot as plt
break
except:
continue
or if you are going to be creating an image file rather than displaying it
Use:
matplotlib.use('agg')
from matplotlib import pyplot as plt
Edit:
Based on your comments try this and see if you get a result that works.
import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
print ("I will test for", gui_env)
for gui in gui_env:
print ("testing", gui)
try:
matplotlib.use(gui,warn=False, force=True)
from matplotlib import pyplot as plt
print (" ",gui, "Is Available")
plt.plot([1.5,2.0,2.5])
fig = plt.gcf()
fig.suptitle(gui)
plt.show()
print ("Using ..... ",matplotlib.get_backend())
except:
print (" ",gui, "Not found")

Related

Unable to import matplotlib.pyplot

I have installed python 2.7.10 in CentOS 6.5. Then I installed matplotlib using pip. However, I am getting the following error when trying to import matplotlib.pyplot.
import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/local/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
tkinter seems to be installed.
"Package tkinter-2.6.6-66.el6_8.x86_64 already installed and latest version"
I have _tkinter.so module in python2.6/lib-dynload, but nothing related to tkinter under python2.7/lib-dynload.
/usr/lib64/python2.6/lib-dynload/_tkinter.so
/usr/local/lib/python2.7/lib-tk/test/test_tkinter
Also, I verified sys.path in python to get the following:
Python 2.7.10 (default, Jun 1 2017, 18:51:46)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages
This may be a problem with your python library path. Check to see where tkinter is installed, and make sure that you have that available to your Python interpreter. Can you import anything else from tkinter?
Try to install tkinter as they said ”no module named _tkinter“.
You can do so with that command:
sudo apt-get install python-tk

matplotlib imported but pyplot cannot be imported

I have two pythons in my mac, they are python 3.4 and 2.7
I can import matplotlib in python 3.4 but as I run
from matplotlib import pyplot
I get the error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from matplotlib import pyplot
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/backend_bases.py", line 62, in <module>
import matplotlib.textpath as textpath
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/textpath.py", line 18, in <module>
from matplotlib.mathtext import MathTextParser
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/mathtext.py", line 63, in <module>
import matplotlib._png as _png
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/_png.so, 2): Library not loaded: libpng15.15.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib-1.5.dev1-py3.4-macosx-10.6-intel.egg/matplotlib/_png.so
Reason: image not found
I am beginner. Can anybody help me figure out what is going on and what should I do?
This is your problem.
Library not loaded: libpng15.15.dylib
Use this sequence of commands to get to where you want.
pip uninstall matplotlib
rm -rf /tmp/pip-build-root/
brew update
brew install libpng --universal
brew install freetype --universal
pip install matplotlib
(venv) venv ~ python
Python 3.4.3 (default, Mar 23 2015, 04:19:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot
>>>
Simple solution to the issue that worked for me:
1- in terminal type $open -a Finder /usr
2- right click on local and do get info
3- unlock the lock at the bottom
4- click + sign and add your user to the list and give read/write privileges
5- click on the gear sign at the bottom and choose apply to enclosed items to recurse under that directory and assign privileges too all directories beneath it.

I uninstalled matplotlib, tried to reinstall it and it failed to be imported. [OS X]

What I have understood since trying to fix this is that I installed matplotlib but python does not utilize it to plot something.
I encountered some error messages like this:
from matplotlib import pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/pyplot.py", line 28, in <module>
import matplotlib.colorbar
File "/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/backend_bases.py", line 61, in <module>
import matplotlib.textpath as textpath
File "/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/textpath.py", line 15, in <module>
import matplotlib.font_manager as font_manager
File "/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/font_manager.py", line 58, in <module>
from matplotlib import ft2font
ImportError: dlopen(/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/ft2font.so, 2): Library not loaded: libfreetype.6.dylib
Referenced from: /Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-macosx-10.5-x86_64.egg/matplotlib/ft2font.so
Reason: image not found
I googled image not found error, matplotlib reinstall and etc... but nothing has worked.
Does this have something to do with version of os x? I am using yosemite.
Here information.
>>> import pkgutil
>>> package = pkgutil.get_loader("matplotlib")
>>> package.filename
'/usr/local/lib/python2.7/dist-packages/matplotlib'
>>>
So where your "matplotlib" ?
COPY :
/Users/changhyunahn/anaconda/lib/python2.7/site-packages/matplotlib-1.5.dev1-p‌‌​​y2.7-macosx-10.5-x86_64.egg
TO : /usr/local/lib/python2.7/site-packages/
OR TO:
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/

Python 2.5.... MatPlotLib doesn't install PyLab

I have NumPy and SciPy installed and working. I'm using Python 2.5 (MIT Intro to Programming course) so I've been installing the packages with .exe files instead of Pip or Anaconda. With MatPlotLib, I get the following errors during installation:
could not create: matplotlib-py2.5
could not create: python 2.5 matplotlib-1.0.0
could not set key value: "C:\Python25\Removematplotlib.exe" -u "C:\Python25\matplotlib-wininst.log"
I have a MatPlotLib folder in Python's Lib/site-packages directory. Importing matplotlib into IDLE works fine. But when I try to import pylab:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pylab
File "C:\Python25\Lib\site-packages\pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python25\Lib\site-packages\matplotlib\pylab.py", line 216, in <module>
from matplotlib import mpl # pulls in most modules
File "C:\Python25\Lib\site-packages\matplotlib\mpl.py", line 1, in <module>
from matplotlib import artist
File "C:\Python25\Lib\site-packages\matplotlib\artist.py", line 6, in <module>
from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath
File "C:\Python25\Lib\site-packages\matplotlib\transforms.py", line 34, in <module>
from matplotlib._path import affine_transform
ImportError: DLL load failed: The specified module could not be found.
I've looked around on different message boards but can't find anyone else with this issue.
Any ideas?

Matplotlib - Python Error

Setup:
Processor : x86_64
Operating System: GNU/Linux
Distro: Fedora Release 13 (Goddard).
Python 2.6.4
/usr/bin/python
Here is the following issue:
I am trying to generate a few graphs using Matplotlib and Python. As I hear from a majority of the solutions3.. there seem to be compatibility issues between matplotlib and 64bit architectures.
I have installed Matplotlib from here
Here is the error that I get:
Traceback (most recent call last):
File "plot-thread-characterization.py", line 24, in <module>
import matplotlib.pyplot as plt
File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 78, in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 8, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
RuntimeError: could not open display
Thanks.
matplotlib is failing to connect to any X server for its GTK display.
There are a couple of options here:
Run a local X server and enable X11 forwarding in your ssh client, to display the output on your local machine. You can verify this is working by checking that the $DISPLAY environment variable is set on the server.
Call matplotlib.use(...) to specify a different display back-end, for example rendering to pdf file, before importing pyplot, e.g.
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
See http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear for more details.

Categories

Resources