-1

I am unable to import matplotlib on this deep learning AMI from aws marketplace

import matplotlib.pyplot as plt

And I get this error

ImportError: No module named 'tkinter'

What I've tried (and the errors returned):

pip install tkinter
> No matching distribution found for TKinter

sudo yum install tkinter
> No package tkinter available.
> Error: Nothing to do
5
  • Did you try sudo yum install python-tkinter?
    – cs95
    Feb 7, 2017 at 20:48
  • yes, same error as sudo yum install tkinter
    – user7531043
    Feb 7, 2017 at 20:51
  • Are you running python2 or python3?
    – cs95
    Feb 7, 2017 at 20:51
  • I am using python2
    – user7531043
    Feb 7, 2017 at 20:58
  • you can't install tkinter with pip, FWIW. Sep 6, 2019 at 19:01

3 Answers 3

0

I personnally had this kind of troubles with tkinter, before to realize it requires a 't' in python3 and a 'T' in python2. so when I need compatibility my codes contain:

import sys
if sys.version_info[0]>2:
    # for Python 3
    import tkinter as tk
else:
    # for Python 2
    import Tkinter as tk

I hope this help

0

First find location of matplotlibrc file

import matplotlib matplotlib.matplotlib_fname()

u'/usr/local/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'

Then go to this file and change the current configuration of backend to backend : agg

This does not solve the problem of import Tkinter, but it does allow the import of pyplot

0

This worked for me:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pylab as plt

Had the same problem after updating to new matplotlib 2.x on python 3.5.x

3
  • yeah, that's basically what I did. Except instead I just set this in the matplotlibrc file. Checkout my answer
    – user7531043
    Feb 9, 2017 at 18:33
  • I thought this might be more convenient than to alter a file. Also it is a solution that only changes the backend within that specific script, is it not? As far as I understand your solution, it would change the backend globally for all scripts until you change the matplotlib file again. Feb 13, 2017 at 11:29
  • yeah, if the goal was to only alter the backend for this script your answer would be best. However, I found my answer worked best for me because I was running into this error in multiple scripts in a project.
    – user7531043
    Feb 13, 2017 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.