Gal Avineri Gal Avineri - 8 months ago 55
Python Question

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".
I searched this site for solutions, and i tried to upgrade pip, as suggested in one of them.
When i typed "pip install --upgrade pip" i got the same error (i pasted the error trace below).

C:\Users\Gal>pip install --upgrade pip
Traceback (most recent call last):
File "c:\python27\lib\site-packages\pip\", line 215, in main
status =, args)
File "c:\python27\lib\site-packages\pip\commands\", line 299, in
File "c:\python27\lib\site-packages\pip\req\", line 370, in
File "c:\python27\lib\site-packages\pip\req\", line 458, in
req_to_install, finder)
File "c:\python27\lib\site-packages\pip\req\", line 407, in
finder.find_requirement(req_to_install, self.upgrade)
File "c:\python27\lib\site-packages\pip\", line 442, in
all_candidates = self.find_all_candidates(
File "c:\python27\lib\site-packages\pip\", line 400, in
for page in self._get_pages(url_locations, project_name):
File "c:\python27\lib\site-packages\pip\", line 545, in _get_pages
page = self._get_page(location)
File "c:\python27\lib\site-packages\pip\", line 648, in _get_page
return HTMLPage.get_page(link, session=self.session)
File "c:\python27\lib\site-packages\pip\", line 757, in get_page
"Cache-Control": "max-age=600",
File "c:\python27\lib\site-packages\pip\_vendor\requests\",
line 487, in get
return self.request('GET', url, **kwargs)
File "c:\python27\lib\site-packages\pip\", line 378, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "c:\python27\lib\site-packages\pip\_vendor\requests\",
line 475, in request
resp = self.send(prep, **send_kwargs)
File "c:\python27\lib\site-packages\pip\_vendor\requests\",
line 617, in send
File "c:\python27\lib\site-packages\pip\_vendor\requests\", line
741, in content
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or
File "c:\python27\lib\site-packages\pip\_vendor\requests\", line
664, in generate
for chunk in, decode_content=True):
File "c:\python27\lib\site-
packages\pip\_vendor\requests\packages\urllib3\", line 353, in
data =, decode_content=decode_content)
File "c:\python27\lib\site-
packages\pip\_vendor\requests\packages\urllib3\", line 310, in
data =
File "c:\python27\lib\site-
packages\pip\_vendor\cachecontrol\", line 54, in read
File "c:\python27\lib\site-
packages\pip\_vendor\cachecontrol\", line 297, in
self.serializer.dumps(request, response, body=body),
File "c:\python27\lib\site-packages\pip\", line 281, in set
return super(SafeFileCache, self).set(*args, **kwargs)
File "c:\python27\lib\site-
packages\pip\_vendor\cachecontrol\caches\", line 99, in set
with self.lock_class(name) as lock:
File "c:\python27\lib\site-
packages\pip\_vendor\lockfile\", line 19, in __init__
LockBase.__init__(self, path, threaded, timeout)
File "c:\python27\lib\site-packages\pip\_vendor\lockfile\",
line 242, in __init__
File "c:\python27\lib\", 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?

Answer Source

I found the problem.

I saw that the last error came from ntpath, trying to decode a byte using ascii. That meant that either result_path or p_path were string that were'nt encoded in ascii.

when i printed their types, it showed that result_path was unicode and p_path was a string. when i printed p_path, i recognized it was encoded in latin1.

So i replaced the line:

result_path = result_path + p_path

to the line:

result_path = result_path + decode(p_path,'latin1')

and that solved it :D