[英]how to fix UnicodeDecodeError: 'ascii' codec can't decode byte on windows
i'm using python 2.7 on a windows 10. iv'e tried to install openpyxl using "pip install openpyxl" and iv'e got an trace of errors, ending in "UnicodeDecodeError: 'ascii' codec can't decode byte".我在 Windows 10 上使用 python 2.7。我尝试使用“pip install openpyxl”安装 openpyxl,但我得到了一些错误,以“UnicodeDecodeError: ‘ascii’ codec can't decode byte”结尾。 I searched this site for solutions, and i tried to upgrade pip, as suggested in one of them.
我在此站点上搜索了解决方案,并尝试按照其中之一的建议升级 pip。 When i typed "pip install --upgrade pip" i got the same error (i pasted the error trace below).
当我输入“pip install --upgrade pip”时,我得到了同样的错误(我粘贴了下面的错误跟踪)。
C:\Users\Gal>pip install --upgrade pip
Exception:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "c:\python27\lib\site-packages\pip\commands\install.py", line 299, in
run
requirement_set.prepare_files(finder)
File "c:\python27\lib\site-packages\pip\req\req_set.py", line 370, in
prepare_files
ignore_dependencies=self.ignore_dependencies))
File "c:\python27\lib\site-packages\pip\req\req_set.py", line 458, in
_prepare_file
req_to_install, finder)
File "c:\python27\lib\site-packages\pip\req\req_set.py", line 407, in
_check_skip_installed
finder.find_requirement(req_to_install, self.upgrade)
File "c:\python27\lib\site-packages\pip\index.py", line 442, in
find_requirement
all_candidates = self.find_all_candidates(req.name)
File "c:\python27\lib\site-packages\pip\index.py", line 400, in
find_all_candidates
for page in self._get_pages(url_locations, project_name):
File "c:\python27\lib\site-packages\pip\index.py", line 545, in _get_pages
page = self._get_page(location)
File "c:\python27\lib\site-packages\pip\index.py", line 648, in _get_page
return HTMLPage.get_page(link, session=self.session)
File "c:\python27\lib\site-packages\pip\index.py", line 757, in get_page
"Cache-Control": "max-age=600",
File "c:\python27\lib\site-packages\pip\_vendor\requests\sessions.py",
line 487, in get
return self.request('GET', url, **kwargs)
File "c:\python27\lib\site-packages\pip\download.py", line 378, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "c:\python27\lib\site-packages\pip\_vendor\requests\sessions.py",
line 475, in request
resp = self.send(prep, **send_kwargs)
File "c:\python27\lib\site-packages\pip\_vendor\requests\sessions.py",
line 617, in send
r.content
File "c:\python27\lib\site-packages\pip\_vendor\requests\models.py", line
741, in content
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or
bytes()
File "c:\python27\lib\site-packages\pip\_vendor\requests\models.py", line
664, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "c:\python27\lib\site-
packages\pip\_vendor\requests\packages\urllib3\response.py", line 353, in
stream
data = self.read(amt=amt, decode_content=decode_content)
File "c:\python27\lib\site-
packages\pip\_vendor\requests\packages\urllib3\response.py", line 310, in
read
data = self._fp.read(amt)
File "c:\python27\lib\site-
packages\pip\_vendor\cachecontrol\filewrapper.py", line 54, in read
self.__callback(self.__buf.getvalue())
File "c:\python27\lib\site-
packages\pip\_vendor\cachecontrol\controller.py", line 297, in
cache_response
self.serializer.dumps(request, response, body=body),
File "c:\python27\lib\site-packages\pip\download.py", line 281, in set
return super(SafeFileCache, self).set(*args, **kwargs)
File "c:\python27\lib\site-
packages\pip\_vendor\cachecontrol\caches\file_cache.py", line 99, in set
with self.lock_class(name) as lock:
File "c:\python27\lib\site-
packages\pip\_vendor\lockfile\mkdirlockfile.py", line 19, in __init__
LockBase.__init__(self, path, threaded, timeout)
File "c:\python27\lib\site-packages\pip\_vendor\lockfile\__init__.py",
line 242, in __init__
hash(self.path)))
File "c:\python27\lib\ntpath.py", line 85, in join
result_path = result_path + p_path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xee in position 0:
ordinal not in range(128)
How can i solve this problem?我该如何解决这个问题?
The answer above mine (the answer of Gal Avineri) was almost correct.我上面的答案(Gal Avineri 的答案)几乎是正确的。 You should go to the
ntpath
file and change the line.您应该转到
ntpath
文件并更改该行。
result_path = result_path + p_path
to the line,到线,
result_path = result_path + p_path.decode('latin1')
I found the problem.我发现了问题。
I saw that the last error came from ntpath, trying to decode a byte using ascii.我看到最后一个错误来自ntpath,试图使用ascii解码一个字节。 That meant that either result_path or p_path were string that were'nt encoded in ascii.
这意味着 result_path 或 p_path 是未以 ascii 编码的字符串。
when i printed their types, it showed that result_path was unicode and p_path was a string.当我打印它们的类型时,它显示 result_path 是 unicode 而 p_path 是一个字符串。 when i printed p_path, i recognized it was encoded in latin1.
当我打印 p_path 时,我意识到它是用 latin1 编码的。
So i replaced the line:所以我替换了这一行:
result_path = result_path + p_path result_path = result_path + p_path
to the line:到线:
result_path = result_path + decode(p_path,'latin1') result_path = result_path + decode(p_path,'latin1')
and that solved it :D这解决了它:D
Above answers gave me the right clue but neither solution worked for me.以上答案给了我正确的线索,但两种解决方案都不适合我。 Turns out I had the issue due to an 'å' in my username in windows.
原来我遇到了这个问题,因为我在 Windows 中的用户名中有一个“å”。
I solved it by changing the line to:我通过将行更改为:
result_path = result_path + p_path.encode('utf-8')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.