This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Tkinter sets an unicode environment variable on win32
Type: behavior Stage: resolved
Components: Tkinter, Windows Versions: Python 2.7, Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: JonFoster, brian.curtin, dotz, flox, ggenellina, ivank, srid, thatch
Priority: normal Keywords: buildbot, needs review, patch

Created on 2009-09-14 09:29 by dotz, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
FixTk.diff ggenellina, 2009-11-14 22:06
Messages (11)
msg92594 - (view) Author: Michał Pasternak (dotz) Date: 2009-09-14 09:29

I was recently playing with txAmpoule & Twisted on win32. When Twisted spawns processess, the 
environment is checked for unicode variables (and an exception is raised in case of). Then it 
came to my attention, that importing Tkinter on win32 sets an environment variable, which value 
is Unicode. Just have a look:

ActivePython (ActiveState Software Inc.) based on
Python 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> import os
>>> os.environ['TK_LIBRARY']
>>> ^Z

Why is it Unicode? Does it really have to be? Do we need that environment variable at all?

On Linux, it is different:

root@foo:~# python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> import os
>>> os.environ['TK_LIBRARY']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/", line 22, in __getitem__
    raise KeyError(key)
KeyError: 'TK_LIBRARY'
msg92666 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-09-16 08:05
I cannot reproduce it with the version:

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit 
(Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
py> import Tkinter
py> import os
py> os.environ['TK_LIBRARY']
py> any(isinstance(value,unicode) for value in os.environ.values())

This might be an ActiveState issue.
msg94641 - (view) Author: Tim Hatch (thatch) * Date: 2009-10-28 22:33
I'm running the exact same version as Gabriel (on Windows 7, 32 bit)
from the installer, and have the same behavior as Michał. 
Checking it appears that on Vista and above, it calls the Win32
API GetFinalPathNameByHandleW to expand symbolic links, but this is a -W
function, not a -A so it deals with unicode.

The blame shows this function was added in response to #3881

For my system, the paths are all lower ascii so changing the end of
convert_path to return str(s) works.  I don't know anything about
filesystem encodings on Windows to do a more correct conversion.
msg95260 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-11-14 22:06
This patch may solve this issue, but I don't have a Vista install to 
test it.
msg95272 - (view) Author: Michał Pasternak (dotz) Date: 2009-11-15 02:17
This patch works OK for me (Vista Home Premium + Python 2.6), thanks!
msg95347 - (view) Author: ivank (ivank) Date: 2009-11-16 14:53
The bug on the Twisted side may be of interest:
msg95355 - (view) Author: Michał Pasternak (dotz) Date: 2009-11-16 17:34
I hit that bug with Twisted too - I tried to use AMP:
msg98666 - (view) Author: Jon Foster (JonFoster) Date: 2010-02-01 15:46
This bug also breaks code that uses the subprocess module, e.g.:

env = os.environ.copy()
subprocess.Popen(... , env=env)

Fails on Windows 7 with an error that the environment can only contain strings.  The offending variables are TK_LIBRARY and TCL_LIBRARY, which have Unicode strings as their values.  I'm using Python 2.6.2.

(The subprocess module should probably be fixed to use CreateProcessW and handle Unicode, but that's a separate issue).
msg100524 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-03-06 10:17
confirmed on Win7 buildbot, when the tcl or tk test is run before test_wsgiref:

FAIL: test_simple_validation_error (test.test_wsgiref.IntegrationTests)
Traceback (most recent call last):
  File "D:\cygwin\home\db3l\buildarea\trunk.bolen-windows7\build\lib\test\", line 159, in test_simple_validation_error
    "AssertionError: Headers (('Content-Type', 'text/plain')) must"
AssertionError: "AssertionError: Environmental variable TCL_LIBRARY is not a string: <type 'unicode'> (value: u'D:\\\\cygwin\\\\home\\\\db3l\\\\buildarea\\\\trunk.bolen-windows7\\\\tcltk\\\\lib\\\\tcl8.5')" != "AssertionError: Headers (('Content-Type', 'text/plain')) must be of type list: <type 'tuple'>"

This error was introduced with patch for #3881.
msg100530 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-03-06 15:06
Fixed with r78722 on trunk. Pending backport to 2.6.
msg101857 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-03-28 08:31
Ported to 2.6 with r79336
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 51155
2010-03-28 08:31:10floxsetstatus: pending -> closed

messages: + msg101857
2010-03-06 15:06:31floxsetstatus: open -> pending
resolution: fixed
messages: + msg100530

stage: needs patch -> resolved
2010-03-06 10:17:44floxsetversions: + Python 2.7
nosy: + flox

messages: + msg100524

keywords: + buildbot
stage: test needed -> needs patch
2010-02-01 15:51:43brian.curtinsetnosy: + brian.curtin
priority: normal
components: + Windows
keywords: + needs review
type: behavior
stage: test needed
2010-02-01 15:46:15JonFostersetnosy: + JonFoster
messages: + msg98666
2009-11-18 16:16:14amaury.forgeotdarclinkissue7344 superseder
2009-11-16 17:34:47dotzsetmessages: + msg95355
2009-11-16 14:53:08ivanksetnosy: + ivank
messages: + msg95347
2009-11-16 14:51:33sridsetnosy: + srid
2009-11-15 02:17:54dotzsetmessages: + msg95272
2009-11-14 22:06:29ggenellinasetfiles: + FixTk.diff
keywords: + patch
messages: + msg95260
2009-10-28 22:33:57thatchsetnosy: + thatch
messages: + msg94641
2009-09-16 08:05:50ggenellinasetnosy: + ggenellina
messages: + msg92666
2009-09-14 09:29:25dotzcreate