Python 2.7:LookupError:未知编码:cp65001

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35176270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:05:27  来源:igfitidea点击:

Python 2.7 : LookupError: unknown encoding: cp65001

pythonpython-2.7encodingpip

提问by Himanshu Bhandari

I have installed python 2(64 bit), on windows 8.1 (64 bit) and wanted to know pip version and for that I fired pip --versionbut it is giving error.

我已经在 Windows 8.1(64 位)上安装了 python 2(64 位)并想知道 pip 版本,为此我解雇了pip --version但它给出了错误。

    C:\Users\ADMIN>pip --version
Traceback (most recent call last):
  File "c:\dev\python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\dev\python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\dev\Python27\Scripts\pip.exe\__main__.py", line 5, in <module>
  File "c:\dev\python27\lib\site-packages\pip\__init__.py", line 15, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "c:\dev\python27\lib\site-packages\pip\vcs\mercurial.py", line 10, in <module>
    from pip.download import path_to_url
  File "c:\dev\python27\lib\site-packages\pip\download.py", line 35, in <module>
    from pip.utils.ui import DownloadProgressBar, DownloadProgressSpinner
  File "c:\dev\python27\lib\site-packages\pip\utils\ui.py", line 51, in <module>
    _BaseBar = _select_progress_class(IncrementalBar, Bar)
  File "c:\dev\python27\lib\site-packages\pip\utils\ui.py", line 44, in _select_progress_class
    six.text_type().join(characters).encode(encoding)
LookupError: unknown encoding: cp65001

Note : The same command works fine for python 3. I have uninstalled both and installed again but still no success.

注意:相同的命令适用于 python 3。我已经卸载并再次安装,但仍然没有成功。

采纳答案by Andriy Ivaneyko

The error means that Unicode characters that your script are trying to print can't be represented using the current console character encoding.

该错误意味着您的脚本尝试打印的 Unicode 字符无法使用当前的控制台字符编码来表示。

Also try to run set PYTHONIOENCODING=UTF-8after execute pip --version withoutreloading terminal if everything going well add PYTHONIOENCODINGas env variable with value UTF-8. See How to set the path and environment variables in Windowsarticle to get info how to add Windows variable.

如果一切顺利,也尝试set PYTHONIOENCODING=UTF-8在执行 pip --version 后运行而不重新加载终端,如果一切顺利添加PYTHONIOENCODING为具有 value 的 env 变量UTF-8。请参阅如何设置 Windows文章中的路径和环境变量以获取有关如何添加 Windows 变量的信息。

NOTE: For PowerShell use $env:PYTHONIOENCODING = "UTF-8"

注意:对于 PowerShell 使用$env:PYTHONIOENCODING = "UTF-8"

Also you can try to install win-unicode-consolewith pip:

您也可以尝试使用 pip安装win-unicode-console

pip install win-unicode-console

Then reload your terminal and try to execute pip --version

然后重新加载您的终端并尝试执行 pip --version

However you can follow suggestions from Windows cmd encoding change causes Python crashanswer because you have same problem.

但是,您可以遵循Windows cmd 编码更改导致 Python 崩溃答案的建议,因为您遇到了同样的问题

回答by Himanshu Bhandari

First of all you need to upgrade your pip. You can do this in Windows by:

首先,您需要升级您的 pip。您可以通过以下方式在 Windows 中执行此操作:

python -m pip install -U pip

Then go manuallyto your script folder, enter command line from that folder (you can do this by clicking shift + right mouse button -> Open console window), and then you should use one of this commands.

然后手动转到您的脚本文件夹,从该文件夹输入命令行(您可以通过单击 shift + 鼠标右键 -> 打开控制台窗口来执行此操作),然后您应该使用此命令之一。

pip -V
pip --version

This will result in

这将导致

pip 7.1.2 from c:\python27\lib\site-packages (python 2.7)

If you still have trouble, you can try to remove your current Python PATH, and add a new one to Python 2.7.

如果您仍然遇到问题,您可以尝试删除您当前的 Python PATH,并在 Python 2.7 中添加一个新的。