1

I have set up a virtual environment and when I try to run matplotlib on it, I get the following error:

Traceback (most recent call last):
  File "/Users/sumeet/PycharmProjects/VISM/test2.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
    from . import tkagg  # Paint image to Tk photo blitter extension.
  File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/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

From what I understand, I need to

  1. set the environment variables correctly in the activate script
  2. or I can symlink tkinter folder to virtualenv python2.7.

I have followed the steps here: TKinter in a Virtualenv, but I am still getting the error.

Steps that I followed are:

  • To virtualenv/bin/activate, I added TK_LIBRARY=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib TKPATH=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib TCL_LIBRARY=/usr/lib export TCL_LIBRARY TK_LIBRARY TKPATH

  • In virtualenv/lib/python2.7 I ran, ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_tkinter.so

Note that when I am using the system Python as an interpreter on PyCharm, matplotlib works fine which gives me a feeling that Tkinter is available somewhere on my system but I am failing to call it from my virtual environment.

The link above seems to be working for Ubuntu. I am wondering if anyone know what is the analogous version for Mac.

Thanks!

1 Answers1

0

If you are not specific to using tkinter in virtual environment then you can use the following lines of code:

import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt

I hope this solves the problem.

sufi
  • 157
  • 3
  • 10