先交待下开发环境:

操作系统:Windows 7

Python版本:2.7.9

Pip版本:6.1.1

其他环境忽略

在windows下使用pip下载python包,出现如下错误

  1. Collecting xxxxxx
  2. Exception:
  3. Traceback (most recent call last):
  4. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\basecommand.py", line 232, in main
  5. status = self.run(options, args)
  6. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\commands\install.py", line 339, in run
  7. requirement_set.prepare_files(finder)
  8. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\req\req_set.py", line 333, in prepare_files
  9. upgrade=self.upgrade,
  10. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\index.py", line 305, in find_requirement
  11. page = self._get_page(main_index_url, req)
  12. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\index.py", line 783, in _get_page
  13. return HTMLPage.get_page(link, req, session=self.session)
  14. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\index.py", line 872, in get_page
  15. "Cache-Control": "max-age=600",
  16. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\sessions.py", line 473, in get
  17. return self.request('GET', url, **kwargs)
  18. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\download.py", line 365, in request
  19. return super(PipSession, self).request(method, url, *args, **kwargs)
  20. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\sessions.py", line 461, in request
  21. resp = self.send(prep, **send_kwargs)
  22. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\sessions.py", line 610, in send
  23. r.content
  24. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\models.py", line 730, in content
  25. self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  26. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\models.py", line 655, in generate
  27. for chunk in self.raw.stream(chunk_size, decode_content=True):
  28. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\packages\urllib3\response.py", line 256, in stream
  29. data = self.read(amt=amt, decode_content=decode_content)
  30. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\packages\urllib3\response.py", line 186, in read
  31. data = self._fp.read(amt)
  32. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\cachecontrol\filewrapper.py", line 54, in read
  33. self.__callback(self.__buf.getvalue())
  34. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\cachecontrol\controller.py", line 217, in cache_response
  35. self.serializer.dumps(request, response, body=body),
  36. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\download.py", line 268, in set
  37. return super(SafeFileCache, self).set(*args, **kwargs)
  38. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\cachecontrol\caches\file_cache.py", line 83, in set
  39. with FileLock(name) as lock:
  40. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\lockfile\mkdirlockfile.py", line 18, in __init__
  41. LockBase.__init__(self, path, threaded, timeout)
  42. File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\lockfile\__init__.py", line 189, in __init__
  43. hash(self.path)))
  44. File "D:\Python27\lib\ntpath.py", line 84, in join
  45. result_path = result_path + p_path
  46. UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128)

'UnicodeDecodeError'这个词已经暴露了这个问题是个编码问题

什么原因导致了这样的问题?在我的电脑上出现这个问题的原因是由于我的用户目录是中文的,pip在下载的时候调用了这样一行代码

  1. temp_dir = tempfile.mkdtemp('-unpack', 'pip-')

pip把下载的临时文件存放在了用户临时文件中,这个目录一般是C:\Users\用户名\AppData\Local\Temp,目录名中有中文,显然ascii这种编码是不支持的

那问题要怎么解决呢?有两种方法解决:

1、把上面的temp_dir那段代码修改一个不包含中文的目录,修改这段代码的文件位置在D:\Python27\Lib\site-packages\pip-6.1.1-py2.7.egg\pip\download.py(位置由个人python安装目录决定)

2、修改编码为gbk,修改D:\Python27\Lib\ntpath.py(位置由个人python安装目录决定)文件中的def join(path, *paths)函数,在函数内第一行加入

  1. # Join two (or more) paths.
  2. def join(path, *paths):
  3. """Join two or more pathname components, inserting "\\" as needed."""
  4. reload(sys)
  5. sys.setdefaultencoding('gbk')
  6. result_drive, result_path = splitdrive(path)
  7. for p in paths:
  8. p_drive, p_path = splitdrive(p)
  9. if p_path and p_path[0] in '\\/':
  10. # Second path is absolute
  11. if p_drive or not result_drive:
  12. result_drive = p_drive
  13. result_path = p_path
  14. continue
  15. elif p_drive and p_drive != result_drive:
  16. if p_drive.lower() != result_drive.lower():
  17. # Different drives => ignore the first path entirely
  18. result_drive = p_drive
  19. result_path = p_path
  20. continue
  21. # Same drive in different case
  22. result_drive = p_drive
  23. # Second path is relative to the first
  24. if result_path and result_path[-1] not in '\\/':
  25. result_path = result_path + '\\'
  26. result_path = result_path + p_path
  27. ## add separator between UNC and non-absolute path
  28. if (result_path and result_path[0] not in '\\/' and
  29. result_drive and result_drive[-1:] != ':'):
  30. return result_drive + sep + result_path
  31. return result_drive + result_path

注意:

  1. reload(sys)
  2. sys.setdefaultencoding('gbk')

这两行代码是我后加入的

一切准备就绪,重新执行pip安装试试吧

总结:

1、据说python3的默认编码为'utf-8',可能不存在这种问题,没有实际测试

2、这次我直接修改了python和pip中的源码,体现了python是脚本语言的特性

Windows下使用pip安装python包是报错-UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0的更多相关文章

  1. Python报错“UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)”的解决办法

    最近在用Python处理中文字符串时,报出了如下错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ...

  2. python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0 解决方案

    环境:mac+python 2.7 场景描述:在使用python修改excel内容修改表格内容为中文保存时报以下错误 此时已经设置了utf-8了 但保存时仍然报错错 此时将python中的中文使用un ...

  3. Python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)

    解决办法: 在报错的页面添加代码: import sys reload(sys) sys.setdefaultencoding('utf8')

  4. 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence

    python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...

  5. 安装mysql-python报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal not in range(128)

    安装mysql-python报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 65: ordinal n ...

  6. python 网络爬虫报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position”解决方案

    Python3.x爬虫, 发现报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1:invalid sta ...

  7. python2.7安装第三方库错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0

    开发环境:win10, x64, pycharm社区版,python2.7.13 python2经常会遇见乱码的问题,并且一遇到中文就乱码.所以我们在安装的时候要注意,无论是解释器interpreto ...

  8. 【python】pip安装报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 7: ordinal not in range(128)

    刚安装完python,准备pip安装第三方库的时候出现了一个错误: UnicodeDecodeError: ‘ascii’ code can’t decode byte 0xef in positio ...

  9. [python]解决Windows下安装第三方插件报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0:

    系统:win7IDE:pycharm Python版本:2.7 安装第三方插件是报错:  报错原因与编码有关,pip把下载的临时文件存放在了用户临时文件中,这个目录一般是C:\Users\用户名\Ap ...

随机推荐

  1. 函数及while实例

    输入1,输出:if 输入2,输出:elif 输入其他数值,输出:else 输入非数字,输出:except def greeting3(name3, lang3=1): if lang3 == 1: r ...

  2. [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. git 下载单个文件 已经添加多个远程服务器

    175down vote It is possible to do (in the deployed repository) git fetch followed by git checkout or ...

  4. vb6.0的安装

    vb6.0古老的编程软件

  5. zw版【转发·台湾nvp系列Delphi例程】HALCON Regiongrowing

    zw版[转发·台湾nvp系列Delphi例程]HALCON Regiongrowing procedure TForm1.Button1Click(Sender: TObject);var img : ...

  6. 20155305乔磊2016-2017-2《Java程序设计》第五周学习总结

    20155305乔磊2016-2017-2<Java程序设计>第五周学习总结 教材学习内容总结 try.catch 1.求平均数程序示例 import java.util.Scanner; ...

  7. 错误源:.net SqlClient data provider

    下午在做毕业设计的时候,想删除数据库的一条数据,结果发现删除的时候老是出现 ======错误源:.net SqlClient data provider==== 这样的错误:本来以为是我还在运行着项目 ...

  8. iOS可执行文件__TEXT段限制 以及 Android 65K函数限制

    1.先看下苹果关于 .ipa上传的大小规定: 最大构建版本文件大小 解压 XXX.ipa size Payload/xxx.app/xxx  32位  32位 + 64位 有些2dx.u3d游戏 或是 ...

  9. php array 根据value获取key,in_array()判断是否在数组内实例

    php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...

  10. django 项目中使用多数据库 multiple databases

    假如在一个django项目中使用到了不只一个数据库, 其实这在大一点的工程中很常见,比如主从库 那么会涉及到如下一些东西 1, 定义 在settings中的DATABASE中定义会使用到的数据,比如除 ...