Windows10使用pip安装python包时报错-UnicodeDecodeError: 'ascii' codec c
本人是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包,出现如下错误
- 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、如果本文对您有用,请支持原创,谢谢
---------------------------------------------------------------------------------
关注微信公众号即可在手机上查阅,并可接收更多测试分享~
Windows10使用pip安装python包时报错-UnicodeDecodeError: 'ascii' codec c的更多相关文章
- windows下pip安装python模块时报错
windows下pip安装python模块时报错总结 装载于:https://www.cnblogs.com/maxaimee/p/6515165.html 前言: 这几天把python版本升级后, ...
- windows下pip安装python模块时报错【转】
windows下pip安装python模块时报错总结 请给作者点赞--> 原文链接 1 权限问题 C:\Users\ljf>pip install xlwt Exception: Trac ...
- pip安装python包时报字符编码错
比如安装scikit-learn时报错: django ascii’ codec can’t encode character 原因是用户目录或用户名存在中文,ascii不能解码,解决办法是在Pyth ...
- 安装python包时报错
pip install numpy 时 报错: Traceback (most recent call last): File "d:\学习\python\python-3.6.5\l ...
- windows下pip安装python模块时报错总结
http://www.cnblogs.com/liaojiafa/p/5100550.html 前言: 这几天把python版本升级后,发现pip安装模块好多都报错(暂不确定是不是因为升级导致的),我 ...
- 运行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.原因 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- 触发transition的几种方式--转
鼠标单击 获取焦点 或元素发生任何改变,怎么说呢,目前的理解是,元素发生了什么改变,使得它跟以前不一样了.比如同样是p元素,先有一个样式.后来这个p被hover了.被focus了.或者通过另外一条途径 ...
- 广搜最短路径变形,(POJ3414)
题目链接:http://poj.org/problem?id=3414 解题报告: 1.每个节点都是一个独立的状态 2.这里的状态转移就是有几种出路,4种:1.倒掉a中的水,2.把a中的水倒到b中去, ...
- ubuntu 报错E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unav E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process us
1.配置xshell,查看虚拟机中ubuntu中网络ip ifconfig 报错 Command 'ifconfig' not found, but can be installed with: su ...
- JavaEE权限管理系统的搭建(一)--------项目中用到的知识点概括
转战Java有一段时间了,.net 已不再开发的新的工程,基本上在维护,最近大半年时间在学习Java,今天抽空将学习的到的知识,应用到了一个权限管理系统的小项目中,特此记录一下.代码如有不对之处,希望 ...
- matlab远程调试
转自:http://blog.163.com/hair_communication/blog/static/20198911920124145414945/ 只是作者好像也是转来的,原来出处好像是百度 ...
- malloc动态分配字符串数组“ 一个月内的提醒”
//输出一个月提醒 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_R ...
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
转载请标明出处: http://blog.csdn.net/forezp/article/details/61472783 本文出自方志朋的博客 一.什么是spring boot Takes an o ...
- 一篇SSM框架整合友好的文章(二)
上一篇讲述了DAO 层,mybatis实现数据库的连接,DAO层接口设计,以及mybtis和spring的整合.DAO层采用接口设计方式实现,接口和SQL实现的分离,方便维护.DAO层所负责的仅仅是接 ...
- Tomcat启动排查
Tomcat启动排查 一.参考 https://blog.csdn.net/baidu_32739019/article/details/64155136
- v-for的显示过滤/排序结果
对于v-for列表渲染指令,项目中很常用的额,但是我们一般可能在从后端接口拿到数据的时候就把数据通过循环整理改造成自己想要的样子了.有时候可能对于不同的列表需求,还要在data里多造一份数据. 这种做 ...