Windows下使用pip安装python包是报错-UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0

解决pip中文路径错误
本文介绍了解决在Windows环境下使用pip安装Python包时遇到的UnicodeDecodeError问题的方法,包括修改临时目录和更改编码。

原文链接http://blog.youkuaiyun.com/all_over_servlet/article/details/45112221

先交待下开发环境:

  操作系统:Windows 7

  Python版本:2.7.9

  Pip版本:6.1.1

  其他环境忽略

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

Collecting xxxxxx
  Exception:
  Traceback (most recent call last):
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\basecommand.py", line 232, in main
      status = self.run(options, args)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\commands\install.py", line 339, in run
      requirement_set.prepare_files(finder)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\req\req_set.py", line 333, in prepare_files
      upgrade=self.upgrade,
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\index.py", line 305, in find_requirement
      page = self._get_page(main_index_url, req)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\index.py", line 783, in _get_page
      return HTMLPage.get_page(link, req, session=self.session)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\index.py", line 872, in get_page
      "Cache-Control": "max-age=600",
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\sessions.py", line 473, in get
      return self.request('GET', url, **kwargs)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\download.py", line 365, in request
      return super(PipSession, self).request(method, url, *args, **kwargs)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\sessions.py", line 461, in request
      resp = self.send(prep, **send_kwargs)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\sessions.py", line 610, in send
      r.content
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\models.py", line 730, in content
      self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\models.py", line 655, in generate
      for chunk in self.raw.stream(chunk_size, decode_content=True):
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\packages\urllib3\response.py", line 256, in stream
      data = self.read(amt=amt, decode_content=decode_content)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\requests\packages\urllib3\response.py", line 186, in read
      data = self._fp.read(amt)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\cachecontrol\filewrapper.py", line 54, in read
      self.__callback(self.__buf.getvalue())
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\cachecontrol\controller.py", line 217, in cache_response
      self.serializer.dumps(request, response, body=body),
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\download.py", line 268, in set
      return super(SafeFileCache, self).set(*args, **kwargs)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\cachecontrol\caches\file_cache.py", line 83, in set
      with FileLock(name) as lock:
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\lockfile\mkdirlockfile.py", line 18, in __init__
      LockBase.__init__(self, path, threaded, timeout)
    File "D:\Python27\lib\site-packages\pip-6.0.8-py2.7.egg\pip\_vendor\lockfile\__init__.py", line 189, in __init__
      hash(self.path)))
    File "D:\Python27\lib\ntpath.py", line 84, in join
      result_path = result_path + p_path
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128)
'UnicodeDecodeError'这个词已经暴露了这个问题是个编码问题

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

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)函数,在函数内第一行加入

# Join two (or more) paths.
def join(path, *paths):
    """Join two or more pathname components, inserting "\\" as needed."""
    reload(sys)
    sys.setdefaultencoding('gbk')
    result_drive, result_path = splitdrive(path)
    for p in paths:
        p_drive, p_path = splitdrive(p)
        if p_path and p_path[0] in '\\/':
            # Second path is absolute
            if p_drive or not result_drive:
                result_drive = p_drive
            result_path = p_path
            continue
        elif p_drive and p_drive != result_drive:
            if p_drive.lower() != result_drive.lower():
                # Different drives => ignore the first path entirely
                result_drive = p_drive
                result_path = p_path
                continue
            # Same drive in different case
            result_drive = p_drive
        # Second path is relative to the first
        if result_path and result_path[-1] not in '\\/':
            result_path = result_path + '\\'
        result_path = result_path + p_path
    ## add separator between UNC and non-absolute path
    if (result_path and result_path[0] not in '\\/' and
        result_drive and result_drive[-1:] != ':'):
        return result_drive + sep + result_path
    return result_drive + result_path
注意:
    reload(sys)
    sys.setdefaultencoding('gbk')
这两行代码是我后加入的


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


总结:

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

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

3、如果本文对您有用,请支持原创,谢谢


在tegan1007环境下,`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xac in position 6217: invalid start byte` 错误通常是由于文件实际编码与读取时指定的 `utf-8` 编码不匹配导致的。以下是一些解决办法: ### 明确指定正确的编码 可以尝试使用其他常见的编码方式来读取文件,如 `gbk`、`latin-1` 等。示例代码如下: ```python try: with open('your_file.txt', 'r', encoding='gbk') as file: content = file.read() except UnicodeDecodeError: print("使用 gbk 编码读取失败,尝试其他编码。") ``` ### 以二进制模式读取文件 以二进制模式读取文件,然后手动解码,这样可以避免自动解码时出现的错误。示例代码如下: ```python with open('your_file.txt', 'rb') as file: binary_content = file.read() try: content = binary_content.decode('utf-8') except UnicodeDecodeError: try: content = binary_content.decode('gbk') except UnicodeDecodeError: print("无法使用 utf-8 或 gbk 解码。") ``` ### 使用 `chardet` 库检测编码 `chardet` 库可以自动检测文件的编码方式。首先需要安装该库: ```bash pip install chardet ``` 然后使用以下代码检测并读取文件: ```python import chardet with open('your_file.txt', 'rb') as file: binary_content = file.read() result = chardet.detect(binary_content) encoding = result['encoding'] print(f"检测到的编码为: {encoding}") try: content = binary_content.decode(encoding) except UnicodeDecodeError: print("解码失败。") ```
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值