V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
burgleaf
V2EX  ›  Python

请教一个pip install 出现报asciii码错误的问题。

  •  
  •   burgleaf · 2013-11-26 12:27:20 +08:00 · 32159 次点击
    这是一个创建于 3775 天前的主题,其中的信息可能已经有所发展或是发生改变。
    系统版本:win8.1 x64
    python版本:2.7.5
    pip版本:pip-1.4.1
    异常如下:
    ---------------------------------------------------------------------------------------------------------
    >pip install sh
    Downloading/unpacking sh
    Cleaning up...
    Exception:
    Traceback (most recent call last):
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\basecomm
    and.py", line 134, in main
    status = self.run(options, args)
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\commands
    \install.py", line 236, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
    e=self.bundle)
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\req.py",
    line 1092, in prepare_files
    self.unpack_url(url, location, self.is_download)
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\req.py",
    line 1238, in unpack_url
    retval = unpack_http_url(link, location, self.download_cache, self.download_
    dir)
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\download
    .py", line 621, in unpack_http_url
    temp_location = os.path.join(temp_dir, filename)
    File "D:\developer\Python27\lib\ntpath.py", line 108, in join
    path += "\\" + b
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 9: ordinal
    not in range(128)

    Traceback (most recent call last):
    File "D:\developer\Python27\Scripts\pip-script.py", line 9, in <module>
    load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\__init__
    .py", line 148, in main
    return command.main(args[1:], options)
    File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\basecomm
    and.py", line 169, in main
    text = '\n'.join(complete_log)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 34: ordinal
    not in range(128)
    -------------------------------------------------------------------------------------------------------

    不知道是什么原因,以前pip是好使的。突然有一天我兴致冲冲地想用一下python就出现这个问题了。不止是安装sh,安装Django,安装任何包都出现这个问题安装不了,网上搜也搜不出答案,所以只好把问题贴在这求大神们指教。
    8 条回复    2016-01-01 21:25:58 +08:00
    c19
        1
    c19  
       2013-11-26 15:41:54 +08:00
    burgleaf
        2
    burgleaf  
    OP
       2013-11-26 15:56:20 +08:00
    Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on wi
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'

    >>> s1 = s.decode('utf-8')
    >>> print s1
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'gbk' codec can't encode character u'\uff61' in position 1:
    illegal multibyte sequence

    @c19 谢谢你。玩python时间不长,以上是我的输出,应该是和这个帖子一样的问题吧。应该怎么破?
    c19
        3
    c19  
       2013-11-26 19:36:18 +08:00
    @burgleaf ***见stackoverflow被采纳答案。***
    print尝试把unicode编码成环境编码。你得环境编码是GBK。windows?
    Linux临时环境变量设置如下:
    c19@acer:~$ export LANG=zh_CN.GBK
    c19@acer:~$ python
    Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
    [GCC 4.8.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
    >>> s1 = s.decode('utf-8')
    >>> s1
    u'(\uff61\uff65\u03c9\uff65\uff61)\uff89'
    >>> print(s1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
    >>>
    burgleaf
        4
    burgleaf  
    OP
       2013-11-26 21:58:02 +08:00   ❤️ 8
    @c19 灰常感谢!原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码。解决办法是:
    python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py
    内容写:
    import sys
    sys.setdefaultencoding('gb2312')
    python会自动运行这个文件。
    pfe361c
        5
    pfe361c  
       2013-11-27 11:01:33 +08:00
    mark
    tanlianming
        6
    tanlianming  
       2013-12-17 16:37:10 +08:00
    mark
    stargazer
        7
    stargazer  
       2014-08-26 14:37:20 +08:00
    mark
    hongfeiyu
        8
    hongfeiyu  
       2016-01-01 21:25:58 +08:00
    赞赞赞,来到这我解决了心头大患
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3238 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:54 · PVG 21:54 · LAX 06:54 · JFK 09:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.