安装pip时出现UnicodeDecodeError错误

10

当我尝试使用pip在我的新笔记本电脑(Win 7 64)上安装beautifulsoup4时,我遇到了这个错误:

Cleaning up...
Exception:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pip\basecommand.py", line 122, in main
    status = self.run(options, args)
  File "C:\Python27\lib\site-packages\pip\commands\install.py", line 278, in run

    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
e=self.bundle)
  File "C:\Python27\lib\site-packages\pip\req.py", line 1234, in prepare_files
    req_to_install.assert_source_matches_version()
  File "C:\Python27\lib\site-packages\pip\req.py", line 464, in assert_source_ma
tches_version
    % (display_path(self.source_dir), version, self))
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9e in position 58: ordinal
 not in range(128)

Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\pip.exe\__main__.py", line 9, in <module>
  File "C:\Python27\lib\site-packages\pip\__init__.py", line 185, in main
    return command.main(cmd_args)
  File "C:\Python27\lib\site-packages\pip\basecommand.py", line 161, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9e in position 73: ordinal
 not in range(128)

我使用命令pip install beautifulsoup4

你有想法问题可能出在哪里吗?

注:这个笔记本之前被用于演示,因此安装了多种语言的程序。


2
你尝试过使用 easy_install 吗? - Selva
@Selva 在阅读了您的建议后,我尝试了一下,它确实有效!谢谢您!但我仍然想知道为什么 PIP 会报错。 - Milano
1
https://python-packaging-user-guide.readthedocs.org/zh_CN/latest/technical.html#pip-vs-easy-install 这会给你一些思路。 - Selva
在我的情况下,将cmd.exe终端更改为UTF-8是有效的。 - José
5个回答

26

或者,您可以在pip安装之前在系统上运行以下命令,然后轻松愉快:

export LC_ALL="en_US.UTF-8"

6
  1. 你能详细说明为什么你认为这有助于解决原帖提出的问题吗?也许这对其他人有所帮助。
  2. 这看起来像是一个Bash命令。原帖作者正在使用Windows,所以这对他没有用处。
- m00am
1
在亚马逊 Linux 上,类似的解决方案对我也起作用了。我在控制台中设置了以下环境变量:LANG=en_US.UTF-8 - ulysses
这对我也起作用了。不过我必须先将区域设置添加到/etc/local.gen中并首先运行locale-gen! - tudor -Reinstate Monica-
如果你收到一个警告 cannot change locale,可能需要安装 en_US.UTF-8 -- https://askubuntu.com/a/227513 - MatrixManAtYrService
这在 Windows 上不起作用。 - Louis Yang

7

这是一个与pip相关的已知问题, 目前(截至2014年11月26日)还没有发布修复版本。由于您正在使用Python 2.7,您可以手动修补pip


5

这是因为您当前的工作目录包含非 ASCII 字符,例如:

$ pwd
$ /home/abuser/Загрузки

这个问题在 pip 1.5.6 中仍然存在,这太糟糕了。因此,您可以避免修补pip,只需更改您的工作目录。


2
不幸的是,这对我没有起作用。我改变了目录回到 C:\ 但仍然出现错误。在我的情况下,我的用户名有西里尔字母。 - asmaier
似乎使用非ASCII字符的用户名是我在pip上遇到的另一个问题:https://github.com/pypa/pip/issues/2501 - asmaier

1
在我的情况下,这是由于 requirements.txt 文件中存在非美国ASCII字符所致。

0
在我的情况下,是因为pip在读取存储库的setup.cfg文件时遇到了编码问题。 最初,当我使用默认终端设置为gitbash的PyCharm venvpy创建者时,我经常遇到这个错误。 然后我在powershell中手动创建了虚拟环境,并且没有遇到这个错误,尽管我收到了以下警告: 警告:由于编码错误,请忽略setup.cfg中的distutils配置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接