Page MenuHomePhabricator

Install python3-tk package on ToolLabs
Closed, ResolvedPublic

Description

I want to make some plots with python3 script using pyplot library. Unfortunately, while matplotlib and pyplot are installed on labs, some dependeces are not:

>>> import matplotlib.pyplot
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 39, in <module>
    import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 98, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 8, in <module>
    import tkinter as Tk, tkinter.filedialog
  File "/usr/lib/python3.4/tkinter/__init__.py", line 41, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

The same line works correctly in python2, but I want to write script exactly in third python. Is it possible to install this package? Thanks!

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

The matplotlib python library seems to be install globally for python2, but I get the same error about missing bindings for libtk:

$ which python
/usr/bin/python
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 98, in <module>    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 8, in <module>
    import Tkinter as Tk, FileDialog
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

Are you using a virtualenv for python2 that has the correct libraries installed somehow? If we can figure out how to make this work using a virtualenv I would rather we do that than install more global Python packages. The little bit I have been reading about it online seems like that may be difficult though. The Python TK bindings are apparently not very friendly to virtualenv installation. That's why I'm curious how you are getting it to work with python2 scripts.

As far as I remember, I've never used virtualenv on tool labs, and bash history proves it. But "import matplotlib.pyplot" line cause no exceptions in python2. On the other hand, I still can't use pyplot, because any operation throws one:

$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot
>>> import matplotlib.pyplot as plt
>>> plt.figure(figsize=(16, 9), dpi=100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
>>> exit()
$ which python
/usr/bin/python

Isorry for misleading you, I just haven't tried to do it in second python before)
Interesting, is it even possible to use GUI library in console application. I don't use any GUI forms, my plots are saved directly to files, but still.

https://matplotlib.org/faq/usage_faq.html#what-is-a-backend documents on how to select a backend (you probably want to use 'AGG' or 'PDF'). Make sure to select one before importing matplotlib.pyplot (i.e., import matplotlib; matplotlib.use('AGG'); from matplotlib import pyplot as plt)

Facenapalm claimed this task.

Many thanks! matplotlib.use('AGG') makes it works in both python2 and python3. I'm sorry for worrying you, should've googled it manually.