15

I am working on an Amazon Linux ec2 machine. When I try to run a Python script inside a virtualenv, I get the following message:

File "/home/sp/Envs/crispor/local/lib/python2.7/dist-packages/matplotlib/externals/six.py", line 80, in _import_module
__import__(name)
ImportError: No module named Tkinter

As I understand Tkinter should have been a part of the Python installation. But somehow it is not. These do not work -

sudo yum install python-tk
sudo yum install tkinter

How do I install Tkinter? Or should I be doing that at all give it should have been a part of the Python installation?


See also: "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm .

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Swetabh
  • 1,665
  • 2
  • 16
  • 21
  • As I know `Amazon Linux ec2` works as `headless` machine - it means without monitor - (and without Graphical Environment) so it can't display any GUI - even Tkinter. – furas Nov 18 '16 at 11:16
  • 2
    That's true. Neither do I want to display any GUI. But one of the modules in the project does this: `import matplotlib.pylab as plt`. This cascades to importing Tkinter. – Swetabh Nov 18 '16 at 11:20
  • I use X forwarding from EC2 instances so Tkinter could be useful – mheyman Feb 28 '17 at 17:42

3 Answers3

34

You don't want (and probably you can't) install tkinter in that server. Configure matplotlib to use a non-interactive backend instead.

Put this in your matplotlibrc file:

backend : agg

UPDATE This should not be necessary for matplotlib >= 3.0.0, according to the documentation "[h]eadless linux servers (identified by the DISPLAY env not being defined) will not select a GUI backend".

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
  • 1
    File location: /usr/local/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc However, if you update matplotlib you will lose your edits. -- There are instructions to make a copy of the file so you dont lose your edits. – mbunch Jan 11 '18 at 22:03
14

to add to @Goyo. you can switch the mode to agg in code as well.

import matplotlib
matplotlib.use('agg',warn=False, force=True)
from matplotlib import pyplot as plt
print "Switched to:",matplotlib.get_backend()
z-star
  • 680
  • 5
  • 6
2

Could you give python version information?

1- Try to install this:

yum install python-tools

This package uses tkinder so can help.

2- If you use python3:

sudo yum install python3-tkinter

3- Download and install the package: http://rpm.pbone.net/index.php3?stat=3&search=python27-tkinter&srodzaj=3&dist[]=79

Roberto García
  • 115
  • 1
  • 11
  • 2
    I tried `sudo yum install python-tools`. It tells me 'No package python-tools available' – Swetabh Nov 18 '16 at 11:21
  • Try: yum install python-setuptools – Roberto García Nov 18 '16 at 12:50
  • python-setuptools is already installed on the machine. `Package python26-setuptools-12.2-1.32.amzn1.noarch already installed and latest version` Although the Python inside the virtualenv is Python2.7 – Swetabh Nov 18 '16 at 13:42