简体   繁体   English

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

[英]Tkinter in virtual environment (PyCharm/Mac OS 10.13.4)

I have set up a virtual environment and when I try to run matplotlib on it, I get the following error: 我已经建立了一个虚拟环境,当我尝试在其上运行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

From what I understand, I need to 据我了解,我需要

  1. set the environment variables correctly in the activate script 在激活脚本中正确set环境变量
  2. or I can symlink tkinter folder to virtualenv python2.7 . 或者我可以将symlink tkinter文件夹symlink tkintervirtualenv python2.7

I have followed the steps here: TKinter in a Virtualenv , but I am still getting the error. 我已经按照以下步骤操作: Virtualenv中的TKinter ,但是仍然出现错误。

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 为了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

  • 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 在我运行的virtualenv / lib / python2.7中, 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. 请注意,当我将系统Python用作PyCharm上的解释器时,matplotlib可以正常工作,这使我感到Tkinter在系统上的某个位置可用,但我无法从虚拟环境中调用它。

The link above seems to be working for Ubuntu. 上面的链接似乎适用于Ubuntu。 I am wondering if anyone know what is the analogous version for Mac. 我想知道是否有人知道Mac的类似版本。

Thanks! 谢谢!

If you are not specific to using tkinter in virtual environment then you can use the following lines of code: 如果您不特定于在虚拟环境中使用tkinter,则可以使用以下代码行:

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

I hope this solves the problem. 我希望这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM