Consider this example:
>>> import subprocess as sp
>>> sp.Popen("notepad2.exe",env={"PATH":"C:\\users\\guillermo\\smallapps\\bin"})
<subprocess.Popen object at 0x030DF430>
>>> sp.Popen("notepad2.exe",env={"PATH":u"C:\\users\\guillermo\\smallapps\\bin"})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\subprocess.py", line 633, in __init__
errread, errwrite)
File "C:\Python26\lib\subprocess.py", line 842, in _execute_child
startupinfo)
TypeError: environment can only contain strings
I’ve traced back the error to this CPython code:
http://hg.python.org/cpython/file/ca54c27a9045/Modules/_winapi.c#l511
I’m unable to udnerstand what PyUnicode_Check does, though:
http://hg.python.org/cpython/file/26af48f65ef3/Objects/unicodeobject.c#l73
As the error message says, the environment must only contain strings. Your first
Popencall satisfies this condition, but the second one doesn’t because you are mappingPATHto a Unicode object created with theu"..."syntax. Use only byte strings when providing environment dicts toPopenand you will not get this error.Note that, judging by the traceback, you are using Python 2.6, so the linked code does not in fact apply, because it comes from Python 3.3.0 beta2.
PyUnicode_Checkchecks that the object is a unicode object, which makes sense in Python 3, where strings are (internally implemented as) unicode objects. In Python 2.6, however, the equivalent line is usingPyString_Check, which would make it fail in your second example.