考慮這個例子:如果「env」參數包含unicode對象,爲什麼Popen在Windows上失敗?
>>> 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
我追溯到錯誤此CPython的代碼:
http://hg.python.org/cpython/file/ca54c27a9045/Modules/_winapi.c#l511
我無法udnerstand什麼PyUnicode_Check
做,雖然:
http://hg.python.org/cpython/file/26af48f65ef3/Objects/unicodeobject.c#l73
所以你建議使用python 3將解決此錯誤,請諮詢。由於我太面臨這個問題https://github.com/django-dbbackup/django-dbbackup/issues/147 –