本人是Windows10,用的方法2解决的

原文链接http://blog.csdn.net/all_over_servlet/article/details/45112221

先交待下开发环境:

操作系统: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)
  47.  
     

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

  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是脚本语言的特性

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

---------------------------------------------------------------------------------

关注微信公众号即可在手机上查阅,并可接收更多测试分享~

Windows10使用pip安装python包时报错-UnicodeDecodeError: 'ascii' codec c的更多相关文章

  1. windows下pip安装python模块时报错

    windows下pip安装python模块时报错总结  装载于:https://www.cnblogs.com/maxaimee/p/6515165.html 前言: 这几天把python版本升级后, ...

  2. windows下pip安装python模块时报错【转】

    windows下pip安装python模块时报错总结 请给作者点赞--> 原文链接 1 权限问题 C:\Users\ljf>pip install xlwt Exception: Trac ...

  3. pip安装python包时报字符编码错

    比如安装scikit-learn时报错: django ascii’ codec can’t encode character 原因是用户目录或用户名存在中文,ascii不能解码,解决办法是在Pyth ...

  4. 安装python包时报错

    pip install numpy  时  报错: Traceback (most recent call last):  File "d:\学习\python\python-3.6.5\l ...

  5. windows下pip安装python模块时报错总结

    http://www.cnblogs.com/liaojiafa/p/5100550.html 前言: 这几天把python版本升级后,发现pip安装模块好多都报错(暂不确定是不是因为升级导致的),我 ...

  6. 运行python代码报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 91: ordinal not in range(128)的解决办法

    1.通过搜集网上的资料,自己多次尝试,问题算是解决了,在代码中加上如下几句即可: import sys reload(sys) sys.setdefaultencoding('utf-8') 2.原因 ...

  7. 【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 ...

  8. python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib

    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...

  9. python读取txt文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte sequence

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

随机推荐

  1. [转载]开启debug调试模式

    debug+trace模式可以查看开发过程中TP的错误信息,可以更好地帮助开发者debug.但是debug模式的开启还不是简单的在配置文件中中设置就可以的,经过查资料摸索,找到一种有效的方法. 首先在 ...

  2. 使用selenium grid与BrowserMobProxyServer联合使用

    背景:项目主要是做埋点数据,要使用 BrowserMobProxyServer,它相当于做一个代理,在你访问一个网页时,通过代理,获取打开网页的数据,对比你需要对比数据,所以这个工具提供获取页面请求的 ...

  3. NYOJ(680),摘枇杷,(暴力,或者二分搜索)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=680 很巧妙的一个题目,就是看你的逆向思维,result 一定是max(a[i])~sum ...

  4. AJAX(三):GET与POST

    1.使用场景get是最常见的请求类型,最常用于向服务器查询某些信息仅次于get的是post请求,通常用于向服务器发送应该被保存的数据 2.使用get请求经常会发生一个错误,就是查询字符串的个是有问题, ...

  5. Qlikview 数据加载方法罗列

    以下是通常会用到的数据加载的方法,供大家参考: 1. 从文件加载: Data: Load *,RowNo() as InputKey; SQL SELECT ID,TEST,DATECREATED F ...

  6. AngularJS 外部文件中的控制器

    在大型的应用程序中,通常是把控制器存储在外部的文件中. <!DOCTYPE html><html><head><meta http-equiv="C ...

  7. 利用sysbench进行MySQL OLTP基准测试

      Preface       In order to know clearly about the real performance threshold of database server,we ...

  8. spring框架中@PostConstruct的实现原理

    在spring项目经常遇到@PostConstruct注解,首先介绍一下它的用途: 被注解的方法,在对象加载完依赖注入后执行. 此注解是在Java EE5规范中加入的,在Servlet生命周期中有一定 ...

  9. C#基础-面向对象-继承

    继承 新建一个基类 对Person类3个字段进行重构->封装字段 效果如下: public string Name { get => name; set => name = valu ...

  10. CSS设置背景透明字体不透明

    写CSS时给容器设置透明度的时候如果使用background-color: #000000; opacity: 0.5;这时会出现容器里的文字也跟着透明.解决办法是不用十六进制的色值和透明度分开写,使 ...