本文介绍了'pip3 --version' 因语法错误而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一切都始于我开始为 Python 3 安装 Requests 库(我在 OSX Mavericks 上运行 Python 2.7.5(由 brew install python 安装)和 3.4.2(由 brew install python3 安装.当我运行 pip3 --version(或与 pip3 命令相关的任何内容)时,我看到:

This all began when I set out to install the Requests library for Python 3 (I'm running on OSX Mavericks with Python 2.7.5 (installed by brew install python) and 3.4.2 (installed by brew install python3). When I run pip3 --version (or anything related to the pip3 command) I see this:

$ pip3 --version
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 7, in <module>
    from pip import main
  File "/usr/local/lib/python3.4/site-packages/pip/__init__.py", line 11, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/local/lib/python3.4/site-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/usr/local/lib/python3.4/site-packages/pip/download.py", line 22, in <module>
    from pip._vendor import requests, six
  File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/__init__.py", line 53, in <module>
    from .packages.urllib3.contrib import pyopenssl
  File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.py", line 49, in <module>
    from ndg.httpsclient.ssl_peer_verification import SUBJ_ALT_NAME_SUPPORT
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ndg/httpsclient/ssl_peer_verification.py", line 17
    except ImportError, e:
                      ^
SyntaxError: invalid syntax

当我运行 Python 2.7.5 版本时,我看到:

When I run the Python 2.7.5 version I see this:

$ pip --version
pip 1.5.6 from /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg (python 2.7)

只是出于理智的目的当我进入交互式解释器时,这是我看到的:

Just for sanity purposes Here is what when I see when I enter the interactive interpreters:

$ python3
Python 3.4.2 (default, Oct 19 2014, 17:52:17)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

许多与更新 pip3 相关的其他答案都建议我使用此命令更新 pip3 pip3 install --upgrade pip 会出现相同的错误,或者我使用 easy_install -U pip 但由于 brew 设置 Python 的方式,它只更新了 Python 2.7.5 版本(没有 easy_install3).有什么想法吗?

A lot of the other answers related to updating pip3 suggest that I update pip3 with this commend pip3 install --upgrade pip which gives the same error, or I use easy_install -U pip but because of how brew sets up the Pythons, it only updates the Python 2.7.5 version (there is no easy_install3). Any ideas?

推荐答案

根本问题是您以某种方式将仅 Python 2.x 的包安装到了 3.x 站点包中.

The root problem is that you somehow got a Python 2.x-only package installed into your 3.x site-packages.

在此基础上,您实际上有两个不同的 Python 2.7 安装(Apple 的和 Homebrew 的)相互交叉,这可能与您如何将 2.7 包也安装到 3.x 中有关......

Underlying that, you've actually got two different Python 2.7 installations (Apple's and Homebrew's) crossed with each other, which may have something to do with how you got a 2.7 package into 3.x as well…

无论如何,这是破坏 pip 的原因是 pip 有各种可选的依赖项,如果存在,它会尝试导入,其中一些也这样做,所以启动 pip 并最终导入 ndg-httpsclient 包.

Anyway, the reason this is breaking pip is that pip has various optional dependencies that it tries to import if present, and some of them do the same, and so on, and ultimately, starting up pip is importing the ndg-httpsclient package.

我不确定你是如何得到那个包裹的.标准的 Homebrew 3.x 在两个额外的站点包目录中查找(启动 python3 然后 import sys; print(sys.path) 以查看它查找的所有位置,stdlib 和站点)超出 pip3 安装到的那个.

I'm not sure how you got that package. A standard Homebrew 3.x looks in two extra site-packages directories (fire up python3 then import sys; print(sys.path) to see all of the places it looks, both stdlib and site) beyond the one that pip3 installs into.

在这种情况下,您以某种方式将 ndg-httpsclient 的 2.x 版本安装到 /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/‌ python3.4/site-packages.

In this case, you've somehow installed the 2.x version of ndg-httpsclient into /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/‌​python3.4/site-packages.

因为你没有用 pip 安装它——而且,更重要的是,因为你不能首先运行 pip——你不能只需 pip uninstall 即可.所以:

Since you didn't install it with pip—and, more to the point, since you can't run pip in the first place—you can't just pip uninstall it. So:

rm -rf /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/‌​python3.4/site-packages/ndg*

这可能会破坏其他依赖它的软件包.一旦你开始工作,你可以使用 pip3 list 查看你安装的所有站点包并测试它们.如果您想变得偏执,请执行以下操作:

This could break some other packages that depend on it. Once you get things working, you can use pip3 list to see all the site packages you've installed and test them out. If you want to be paranoid, do something like this:

$ pip3 list > mypackages
$ rm -rf <each site-package directory>
$ brew uninstall python3
$ brew install python3
$ pip3 install -r mypackages

您可能想要类似地清理您的 Homebrew 2.7(或者只是废弃它并只使用 Apple 的——尤其是因为我很确定您无论如何都在运行 Apple 的),以及 Apple 2.7 的站点包(但不是Apple 的 Python 本身,当然,因为你不能卸载它).

You might want to similarly clean up your Homebrew 2.7 (or just scrap it and only use Apple's—especially since I'm pretty sure you're running Apple's anyway), and the site-packages for the Apple 2.7 (but not Apple's Python itself, of course, because you can't uninstall that).

这篇关于'pip3 --version' 因语法错误而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:22
查看更多