在windows下的虚拟环境中使用tk,要留神了
Posted 小苹果的苹果树
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在windows下的虚拟环境中使用tk,要留神了相关的知识,希望对你有一定的参考价值。
事情是这样的:有朋友在学习用matplotlib作图,遇到一些问题问我,我也没用过,就想先装一个试试。然后呢,我就随手创建了个虚拟环境。结果,涉及到matplotlib的第一行就报错了:
Traceback (most recent call last):
File "D:/workspace/PyTest/get_data.py", line 22, in <module>
plt.scatter(X[:50, 0], X[:50, 1], color="red", marker="o", label="setosa")
File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 3420, in scatter
ax = gca()
File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 950, in gca
return gcf().gca(**kwargs)
File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 586, in gcf
return figure()
File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 535, in figure
**kwargs)
File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\backends\\backend_tkagg.py", line 81, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\backends\\backend_tkagg.py", line 89, in new_figure_manager_given_figure
window = Tk.Tk()
File "C:\\Python35\\Lib\\tkinter\\__init__.py", line 1868, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can\'t find a usable init.tcl in the following directories:
C:/Python35/lib/tcl8.6 D:/workspace/PyTest/py3env/lib/tcl8.6 D:/workspace/PyTest/lib/tcl8.6 D:/workspace/PyTest/py3env/library D:/workspace/PyTest/library D:/workspace/PyTest/tcl8.6.4/library D:/workspace/tcl8.6.4/library
This probably means that Tcl wasn\'t installed properly.
经过分析,我想这是因为没有找到tkinter,而matplotlib需要用到这个。可是这东西装python时候是默认的,我肯定会装的。我翻了翻我的电脑,Tcl确实是装了的:
可是路径不对。经群里的同学指导,我还特意在主环境下测试了一下tk和tcl环境:
import tkinter root = tkinter.Tk() print(root.tk.exprstring(\'$tcl_library\')) print(root.tk.exprstring(\'$tk_library\'))
在主环境中结果也是正常的:
C:\\Python35\\tcl\\tcl8.6
C:/Python35/tcl/tk8.6
可是虚拟环境中就不正常了。
Traceback (most recent call last): File "D:/workspace/PyTest/testtk.py", line 2, in <module> root = tkinter.Tk() File "C:\\Python35\\Lib\\tkinter\\__init__.py", line 1868, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: Can\'t find a usable init.tcl in the following directories: C:/Python35/lib/tcl8.6 D:/workspace/PyTest/py3env/lib/tcl8.6 D:/workspace/PyTest/lib/tcl8.6 D:/workspace/PyTest/py3env/library D:/workspace/PyTest/library D:/workspace/PyTest/tcl8.6.4/library D:/workspace/tcl8.6.4/library This probably means that Tcl wasn\'t installed properly.
这可就尴尬了。
但是,我又注意到一个细节:Tcl在查找环境的时候,会查找本机的主python环境,比如刚才那段报错中,Tcl查找了这样两个环境:
C:/Python35/lib/tcl8.6
D:/workspace/PyTest/py3env/lib/tcl8.6
其中C盘是我的主环境,D:/workspace/PyTest/py3env是虚拟环境。
然后我开始翻代码,看tcl查询的路径是啥。找不到。网上介绍了添加环境变量,TCL_LIBRARY和TK_LIBRARY,我试了也不行(可能这两个变量仅限于Linux系统),在path里面添加也不行。
走投无路的情况下我开始动歪脑筋,把Tcl8.6整个文件夹从C:\\Python35\\tcl\\复制到了C:\\Python35\\Lib中,然后,居然……
Traceback (most recent call last): File "D:/workspace/PyTest/ML/mcpperceptron/get_data.py", line 22, in <module> plt.scatter(X[:50, 0], X[:50, 1], color="red", marker="o", label="setosa") File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 3420, in scatter ax = gca() File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 950, in gca return gcf().gca(**kwargs) File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 586, in gcf return figure() File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\pyplot.py", line 535, in figure **kwargs) File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\backends\\backend_tkagg.py", line 81, in new_figure_manager return new_figure_manager_given_figure(num, figure) File "D:\\workspace\\PyTest\\py3env\\lib\\site-packages\\matplotlib\\backends\\backend_tkagg.py", line 89, in new_figure_manager_given_figure window = Tk.Tk() File "C:\\Python35\\Lib\\tkinter\\__init__.py", line 1868, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: Can\'t find a usable tk.tcl in the following directories: C:/Python35/lib/tcl8.6/tk8.6 C:/Python35/lib/tk8.6 D:/workspace/PyTest/py3env/lib/tk8.6 D:/workspace/PyTest/lib/tk8.6 D:/workspace/PyTest/py3env/library This probably means that tk wasn\'t installed properly.
报的错从“找不到Tcl”变成了“找不到Tk”!
难道说成功了?于是,我又如法炮制,把tk8.6文件夹从C:\\Python35\\tcl\\复制到了C:\\Python35\\Lib\\tcl8.6中。
然后就真的成功了!
这叫什么鬼问题嘛!╭(╯^╰)╮
好吧,就是提醒注意一下,在windows中的虚拟环境下,如果使用tk,注意tcl和tk的路径。可以把整个目录复制到tcl和tk的搜索路径中。至于这叫什么操作,我也不知道。
以上是关于在windows下的虚拟环境中使用tk,要留神了的主要内容,如果未能解决你的问题,请参考以下文章
windows环境下的eclipse操作虚拟机里面的hadoop相关配置
windows下python虚拟环境virtualenv安装和使用