Virtualenv: Tkinter - fails in a virtual env

Created on 14 Mar 2011  ·  12Comments  ·  Source: pypa/virtualenv

When a virtualenv which shares the global environment site-packges is used for Tk based application using the global Tk.

Tkinter cannot initialise correctly in a virtualenv as it fails to correctly search for it's resources - which are still in the global python environment.

\Documents and Settings\Roger\Desktop\windows-install-bits>cd \venvtest

C:\venvtest>Scripts\activate.bat
(venvtest) C:\venvtest>python
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> f= Tk()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Tk' is not defined
>>> f= Tkinter.Tk()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python26\Lib\lib-tk\Tkinter.py", line 1643, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, want
objects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
    c:/Python26/lib/tcl8.5 c:/venvtest/lib/tcl8.5 c:/lib/tcl8.5 c:/venvtest/libr
ary c:/library c:/tcl8.5.2/library c:/tcl8.5.2/library



This probably means that Tcl wasn't installed properly.

>>> ^Z


(venvtest) C:\venvtest>deactivate

What I would expect is for a virtual env to work the same as the globale environment like so:--

C:\venvtest>c:\Python26\python.exe
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> f = Tkinter.Tk()
>>> ^Z


C:\venvtest>

After some experimentation I have found that copying c:\python26\tcl{tcl,tk}8.5 directories into the c:\venvtest\Libs solve the issues.


bug

Most helpful comment

Still a problem in virtualenv 15.0.1.
... just sayin'

All 12 comments

Actually a better way of fixing this would be to set the TCL_LIBRARY path in
activate.bat , or change FixTk.py to know about virtualenv's.


Original Comment By: Roger Gammans

I too am just setting the environment variable TCL_LIBRARY path in activate.bat. So all virtualenv needs to do is add that line to activate.bat automagically.

There is a thread on stackoverflow on this issue as well:
http://stackoverflow.com/questions/15884075/tkinter-in-a-virtualenv

I also just set the environment variable in activate.bat. However would be great if this would be done automatically.

Still a problem in virtualenv 15.0.1.
... just sayin'

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

Looks like it's still a problem

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Just add a comment if you want to keep it open. Thank you for your contributions.

This is still a problem. I hit it most recently on Mac -- the fix for https://github.com/pypa/virtualenv/issues/93 did not resolve the issue on Mac.

Someone need to reactivate this issue. Just because it has sat around without getting any traction, that doesn't mean it's no longer an issue. It just means the project maintainers have not prioritized it like they should.

On Windows, still an issue with Python 2.7 but seems solved in Python 3.8 with the venv module.

Please feel free to file a new issue, with the error message, reproduction instructions and information on how you installed Python.

It just means the project maintainers have not prioritized it like they should.

Considering project maintainers are not paid, and do it for free in their personal time I don't believe you can talk about they should. If this issue is a problem for you, please consider filling in a PR with a fix. All the code is open-source and freely available to you.

Was this page helpful?
0 / 5 - 0 ratings