在OSX(El Capitan,10.11.6)上,使用virtualenv(15.1.0),我在使用pip(9.0.1)从文本文件安装需求时出错:
virtualenv env
source env/bin/activate
pip install -r requirements.txt
但不是在手动循环每个需求时:
for r in $(cat requirements.txt); do pip install "$r"; done
这让我觉得在阅读需求文件时pip假定的默认编码可能存在问题。 有没有办法(环境变量,我推测)设置需求文件的默认编码?
我得到的错误是:
Exception:
Traceback (most recent call last):
File "/path/to/env/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/path/to/env/lib/python2.7/site-packages/pip/commands/install.py", line 312, in run
wheel_cache
File "/path/to/env/lib/python2.7/site-packages/pip/basecommand.py", line 295, in populate_requirement_set
wheel_cache=wheel_cache):
File "/path/to/env/lib/python2.7/site-packages/pip/req/req_file.py", line 84, in parse_requirements
filename, comes_from=comes_from, session=session
File "/path/to/env/lib/python2.7/site-packages/pip/download.py", line 422, in get_file_content
content = auto_decode(f.read())
File "/path/to/env/lib/python2.7/site-packages/pip/utils/encoding.py", line 31, in auto_decode
return data.decode(locale.getpreferredencoding(False))
LookupError: unknown encoding:
以下测试代码:
#!/usr/bin/env python
import sys
import locale
print sys.stdin.encoding
print locale.getpreferredencoding()
print locale.getpreferredencoding(False)
print sys.getdefaultencoding()
print sys.getfilesystemencoding()
返回:
None
US-ASCII
ascii
utf-8
从命令行:
$ locale
LANG="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_CTYPE="utf-8"
LC_MESSAGES="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_ALL=
答案 0 :(得分:0)
根据您的测试,locale.getpreferredencoding()
不存在 - 它应该如下所示:
UTF-8
UTF-8
US-ASCII
ascii
utf-8
我的locale
看起来像你的几乎(除了US vs GB和LC_CTYPE
之外)。 LC_CTYPE
似乎有所不同似乎很奇怪,也许值得研究为什么。
$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
有几件事你可以尝试纠正这个问题,首先是你的shebang:
#!/usr/bin/python
尝试将其更改为此,因为#!/usr/bin/env python
可能无法正确设置区域设置。如果这不起作用,您可以尝试在脚本中强制编码:
import locale
loc = locale.getlocale()
locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
locale.setlocale(locale.LC_ALL, loc) # restore saved locale
# OR
locale.getpreferredencoding(do_setlocale=True) or "utf-8"
# OR
if locale.getpreferredencoding() == '':
locale.setlocale(locale.LC_ALL,'UTF-8')
最终,您需要弄清楚为什么locale.getpreferredencoding()
在前几次测试中显示为空,以及为什么您的语言环境中设置了不匹配的LC_CTYPE
。