虚拟环境中的Tkinter(PyCharm / Mac OS 10.13.4)

时间:2018-05-07 15:16:24

标签: python tkinter virtualenv

我已经设置了一个虚拟环境,当我尝试在其上运行matplotlib时,出现以下错误:

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

根据我的理解,我需要

  1. set激活脚本中的环境变量
  2. 或我可以symlink tkinter将文件夹发送到virtualenv python2.7
  3. 我已按照此处的步骤操作:TKinter in a Virtualenv,但我仍然收到错误。

    我遵循的步骤是:

    • 对于virtualenv / bin / activate,我补充道 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

    • 在virtualenv / lib / python2.7中,我跑了,ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_tkinter.so

    请注意,当我使用系统Python作为PyCharm的解释器时,matplotlib工作正常,这让我觉得Tkinter在我的系统某处可用,但我无法从我的虚拟环境中调用它。

    上面的链接似乎适用于Ubuntu。我想知道是否有人知道什么是Mac的类似版本。

    谢谢!

1 个答案:

答案 0 :(得分:0)

如果您不特定于在虚拟环境中使用tkinter,则可以使用以下代码行:

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

我希望这可以解决问题。