Python Virtualenv 中的 TKinter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15884075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TKinter in a Virtualenv
提问by Jasper van den Bosch
Trying to run python code with TKinter-based widgets from a virtualenv.
尝试使用来自 virtualenv 的基于 TKinter 的小部件运行 python 代码。
user@computer:~/myproject$ env/bin/python Python
2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last): File "<stdin>", line 1, in <module> 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
What is the proper way to install TK to the virtualenv?
将 TK 安装到 virtualenv 的正确方法是什么?
采纳答案by A. Rodas
Set the environment variable TCL_LIBRARY in your activatescript. On Windows (Python 2.7 with Tcl 8.5), just add this line to Scripts\activate.bat:
在activate脚本中设置环境变量 TCL_LIBRARY 。在 Windows(带有 Tcl 8.5 的 Python 2.7)上,只需将此行添加到Scripts\activate.bat:
set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
@Jasper van den Bosch's edit: On Ubuntu, the modification to the script activateis the following:
@Jasper van den Bosch 的编辑:在 Ubuntu 上,对脚本的修改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
The reference of this can be found on this question on askubuntu
可以在 askubuntu上的这个问题上找到这个参考
回答by detachedhead
This is really an update to the great answer from A. Rodas for use with Python 3.4 and Tcl 8.6 (I don't have enough reputation points to comment).
这确实是对 A. Rodas 与 Python 3.4 和 Tcl 8.6 一起使用的出色答案的更新(我没有足够的声誉点来评论)。
Set the environment variable TCL_LIBRARY in your activate script. On Windows (Python 3.4 with Tcl 8.6), just add this line to Scripts\activate.bat:
在您的激活脚本中设置环境变量 TCL_LIBRARY。在 Windows(带有 Tcl 8.6 的 Python 3.4)上,只需将此行添加到 Scripts\activate.bat:
set "TCL_LIBRARY=C:\Python34\tcl\tcl8.6"
I came across this issue while using Windows 7, Python 3.4, and ggplot in a virtual environment.
我在虚拟环境中使用 Windows 7、Python 3.4 和 ggplot 时遇到了这个问题。
回答by markk
Also an update from answer by A.Rodas- I have tried doing that in Windows 7 using Windows Powershell but wasn't able to get it done (I also do not have enough reputation points to comment
也是A.Rodas回答的更新- 我曾尝试使用 Windows Powershell 在 Windows 7 中执行此操作,但无法完成(我也没有足够的声誉点来评论
I realized even when I added the line
set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
and the corresponding one for the tk library, to the activate.batscript, the tcl link was not getting updated, and what I needed to do was just go to the directory where it's looking for the tcl files, and copy the corresponding tcl and tk files to that directory. Now the file is in the default location so you don't need to update activate.bateverytime you create a new environment
我意识到即使我将set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
tk 库的行和相应的行添加
到activate.bat脚本中,tcl 链接也没有更新,我需要做的就是转到它正在寻找 tcl 文件的目录,并且将相应的 tcl 和 tk 文件复制到该目录。现在该文件位于默认位置,因此您无需在activate.bat每次创建新环境时更新
回答by camel_case
To get this working in powershell, you have to edit the activate.ps1 file instead of activate.bat. Just add the following to activate.ps1:
$env:TCL_LIBRARY = "C:\Python27\tcl\tcl8.5"
要使其在 powershell 中工作,您必须编辑 activate.ps1 文件而不是 activate.bat。只需将以下内容添加到 activate.ps1 中:
$env:TCL_LIBRARY = "C:\Python27\tcl\tcl8.5"
回答by RJ87
I manage to integrate tkinterin python3 to virtualenv by symlink tkinterfolder to virtualenv python3. I works for me. But I don't know if it's the right way.
我设法tkinter通过symlink tkinter文件夹将python3集成到virtualenv到virtualenv python3. 我为我工作。但我不知道这是否正确。
- install tkinter
- 安装tkinter
sudo apt-get install python3-tk
- go to your virtualenv's python lib folder
- 转到您的 virtualenv 的 python lib 文件夹
cd ~/.virtualenvs/cv/lib/python3.4/
- link the tkinter
- 链接 tkinter
ln -s /usr/lib/python3.4/tkinter tkinter
Hope this helps.
希望这可以帮助。
In later versions of python, this may result in a
在 Python 的更高版本中,这可能会导致
ModuleNotFoundError: No module named '_tkinter'
In this case, ensure to also symlink/usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so
as
path/to/virtualenv/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so
using
在这种情况下,确保符号链接也/usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so
为
path/to/virtualenv/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so
使用
ln -s /usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so _tkinter.cpython-36m-x86_64-linux-gnu.so
from within your virtualenv lib/python3.x/lib-dynload/directory.
从您的 virtualenvlib/python3.x/lib-dynload/目录中。
回答by maniac
For me the solution was copying the folder tcl from
对我来说,解决方案是从中复制文件夹 tcl
C:\Users\{myUser}\Python3.5\tcl
to
到
C:\Users\{myUser}\{myVirtualEnv}
replace {myUser} and {myVirtualEnv} with your specific directory names.
用您的特定目录名称替换 {myUser} 和 {myVirtualEnv}。
回答by JShare
I am using python2.7 with a virtualenv on a machine running linux mint. I received the exact same error as mentioned by the OP when running an application that required matplotlib in my virtualenv. "ImportError: No module named _tkinter, please install the python-tk package"
我在运行 linux mint 的机器上使用带有 virtualenv 的 python2.7。在我的 virtualenv 中运行需要 matplotlib 的应用程序时,我收到了与 OP 完全相同的错误。“导入错误:没有名为 _tkinter 的模块,请安装 python-tk 包”
I ended up deleting and recreating my virtual environment using the suggestions in the above posts. Here are my steps:
我最终使用上述帖子中的建议删除并重新创建了我的虚拟环境。这是我的步骤:
- if your virtual environment is activated, then run this command to freeze the requirements to a file that you can access later:
pip freeze > requirements.txt - if your virtual environment is activated, then deactivate it using:
deactivate - delete your virtualenv folder.
- install python-tk using:
sudo apt-get install python-tk - recreate your virtualenv using:
virtualenv <nameofyourenv> --system-site-packages - next, activate your virtualenv:
source <virtual environment folder>/bin/activate - restore all your packages that you froze earlier from the requirements.txt file:
pip install -r <path to requirements.txt file>
- 如果您的虚拟环境已激活,则运行此命令将需求冻结到您可以稍后访问的文件中:
pip freeze > requirements.txt - 如果您的虚拟环境已激活,请使用以下方法停用它:
deactivate - 删除您的 virtualenv 文件夹。
- 使用以下命令安装 python-tk:
sudo apt-get install python-tk - 使用以下命令重新创建您的 virtualenv:
virtualenv <nameofyourenv> --system-site-packages - 接下来,激活您的 virtualenv:
source <virtual environment folder>/bin/activate - 从 requirements.txt 文件恢复您之前冻结的所有包:
pip install -r <path to requirements.txt file>
now, when I run the same code as before, it has no problem importing _tkinter. Hope this helps! Thanks to the everyone's suggestions above. It really helped me a lot.
现在,当我运行与以前相同的代码时,导入 _tkinter 没有问题。希望这可以帮助!感谢楼上大家的建议。它真的帮了我很多。
回答by u7192265
clone the cpython project
克隆 cpython 项目
git clone [email protected]:python/cpython.git
cdto the cpython directory.
git checkoutthe desired version of your virtual env(for me it is 3.4),
and build it with
cd到 cpython 目录。
git checkout所需版本的虚拟环境(对我来说是 3.4),并使用
./configure
make
make test
sudo make install
you will find an so file _tkinter.cpython-xxx.soin a subdir of the build/directory, copy it to your venv's lib-dynloaddir. (for me it is ~/tf1.1py3.4/lib/python3.4/lib-dynload/)
您会_tkinter.cpython-xxx.so在该build/目录的子目录中找到一个 so 文件,将其复制到您的 venvlib-dynload目录中。(对我来说是~/tf1.1py3.4/lib/python3.4/lib-dynload/)
回答by Max
In case this helps those scratching their heads after reading through all the answers here - it also seems that you might need to re-create your virtualenv in case you created it before installing python3.6-tk. None of the solutions with specifying TK_PATHand TCL_PATHseemed to work, even when using the paths given by tkinter outside the virtual environment (see this post). To be sure, just delete the venv and create a new one.
万一这有助于那些在阅读这里的所有答案后摸不着头脑的人 - 如果您在安装python3.6-tk. 即使在虚拟环境之外使用 tkinter 给定的路径时,也没有任何带有指定TK_PATH和的解决方案TCL_PATH似乎有效(请参阅此帖子)。可以肯定的是,只需删除 venv 并创建一个新的。

