问题描述
在OSX(El Capitan,10.11.6)上,使用virtualenv(15.1.0),从带有pip(9.0.1)的文本文件安装需求时出现错误:
On OSX (El Capitan, 10.11.6), using virtualenv (15.1.0), I am getting an error when installing requirements from a text file with pip (9.0.1):
virtualenv env
source env/bin/activate
pip install -r requirements.txt
但在手动遍历每个需求时不是:
but not when looping over each requirement manually:
for r in $(cat requirements.txt); do pip install "$r"; done
这使我认为读取需求文件时pip假定的默认编码可能存在问题. 是否有一种方法(我认为是环境变量)来设置需求文件的默认编码?
This makes me think there might be an issue with the default encoding assumed by pip when reading the requirements file. Is there a way (environment variable, I presume) to set the default encoding of requirement files?
我得到的错误是:
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:
以下测试代码:
The following test code:
#!/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
从命令行:
From the command-line:
$ 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=
推荐答案
根据您的测试,locale.getpreferredencoding()
不存在-它应该看起来像这样:
According to your test the locale.getpreferredencoding()
is absent — it should look like this:
UTF-8
UTF-8
US-ASCII
ascii
utf-8
做locale
地雷看起来几乎像您一样(除了US vs GB和LC_CTYPE
). LC_CTYPE
似乎与众不同,也许值得一试.
Doing locale
mine looks almost like yours (other than US vs GB and LC_CTYPE
). It does seem peculiar that LC_CTYPE
seems different, and maybe worth looking into as to why.
$ 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:
There are a couple of things you can try to correct the issue, the first being your shebang:
#!/usr/bin/python
请尝试将其更改为此,因为#!/usr/bin/env python
可能无法正确设置语言环境.如果这样不起作用,您可以随时尝试在脚本中强制编码:
Try changing it to this perhaps, as #!/usr/bin/env python
might not set the locale properly. If that doesn't work you can always try forcing the encoding within your script:
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
不匹配.
Ultimately you'll want to figure out why locale.getpreferredencoding()
is coming up empty for the first couple tests, and why you have a mismatched LC_CTYPE
set in your locale.
这篇关于使用pip编码需求文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!