ImportError:无法使用PIP导入名称HTTPSHandler

use*_*020 61 python ssl openssl pip

使用pip安装python包时遇到HTTPSHandler错误,以下是堆栈跟踪,

--------desktop:~$ pip install Django==1.3
Traceback (most recent call last):
  File "/home/env/.genv/bin/pip", line 9, in <module>
    load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
  File "/home/env/.genv/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/env/.genv/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "/home/env/.genv/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/home/env/.genv/lib/python2.7/site-packages/pip/__init__.py", line 10, in <module>
    from pip.util import get_installed_distributions, get_prog
  File "/home/env/.genv/lib/python2.7/site-packages/pip/util.py", line 17, in <module>
    from pip.vendor.distlib import version
  File "/home/env/.genv/lib/python2.7/site-packages/pip/vendor/distlib/version.py", line 13, in <module>
    from .compat import string_types
  File "/home/env/.genv/lib/python2.7/site-packages/pip/vendor/distlib/compat.py", line 31, in <module>
    from urllib2 import (Request, urlopen, URLError, HTTPError,
ImportError: cannot import name HTTPSHandler
Run Code Online (Sandbox Code Playgroud)

我以前编辑Modules/setup.dist文件并取消注释SSL代码行并重新构建它,参考以下主题:http://forums.opensuse.org/english/get-technical-help-here/applications/488962-opensuse -python-的OpenSSL 2.HTML

dno*_*zay 47

OSX +自制软件用户:

您可以获得配方的最新更新:

brew reinstall python
Run Code Online (Sandbox Code Playgroud)

但是如果你仍然遇到问题,例如你可能已经升级了操作系统,那么你可能需要先获得最新的openssl.您可以从以下位置查看使用的版本和位置:

openssl version -a
which openssl
Run Code Online (Sandbox Code Playgroud)

要获得最新的openssl:

brew update
brew install openssl
brew link --overwrite --dry-run openssl  # safety first.
brew link openssl --overwrite
Run Code Online (Sandbox Code Playgroud)

这可能会发出警告:

bash-4.3$ brew link --overwrite --dry-run openssl
Warning: Refusing to link: openssl Linking keg-only openssl means you may end up linking against the insecure, deprecated system OpenSSL while using the headers from Homebrew's openssl. 
Instead, pass the full include/library paths to your compiler e.g.: 
-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib
Run Code Online (Sandbox Code Playgroud)

附注:此警告表示对于其他应用,您可能需要使用

export LDFLAGS=-L/usr/local/opt/openssl/lib
export CPPFLAGS=-I/usr/local/opt/openssl/include
Run Code Online (Sandbox Code Playgroud)

然后重新编译python:

brew uninstall python
brew install python --with-brewed-openssl
Run Code Online (Sandbox Code Playgroud)

或者对于python 3

brew uninstall python3
brew install python3 --with-brewed-openssl
Run Code Online (Sandbox Code Playgroud)

  • 如果您安装了多个python版本,请不要认为这将有效 (2认同)
  • `bash-4.3 $ brew链接--overwrite --dry-run openssl警告:拒绝链接:openssl链接keg-only openssl意味着你可能在使用Homebrew的openssl的头文件时最终链接到不安全,不赞成的系统OpenSSL.相反,将完整的include/library路径传递给编译器,例如:-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib` (2认同)

sk1*_*k1p 22

如果需要SSL支持,则需要在构建Python之前安装OpenSSL头文件.在Debian和Ubuntu上,它们位于一个名为的包中libssl-dev.您可能需要更多依赖项,如此处所述.


Way*_*inn 20

家酿

这可能是由于升级到小牛队造成的.这就是我修复它的方法.

更新OpenSSL

#make a copy of the existing library, just in case
sudo cp /usr/bin/openssl /usr/bin/openssl.apple

# update openssl 
brew update
brew install openssl
brew link --force openssl

# reload terminal paths
hash -r
Run Code Online (Sandbox Code Playgroud)

重新安装Python

Python 3

brew uninstall python3

brew install python3 --with-brewed-openssl
Run Code Online (Sandbox Code Playgroud)

Python 2

brew uninstall python

brew install python --with-brewed-openssl
Run Code Online (Sandbox Code Playgroud)

这个答案整合了我发现的所有Stack Exchange答案和评论,主要基于Apple Stack Exchange的答案.


leo*_*eon 9

您需要在make之前安装OpenSSl并安装Python来解决问题.

在Centos上:

yum install openssl openssl-devel -y
Run Code Online (Sandbox Code Playgroud)

资源


小智 9

即使在确认我的PATH等之后,我在Mac OSX上遇到了这个问题.

做了一个; 点击卸载virtualenv然后安装virtualenv它似乎现在有效.

当时我强迫brew连接openssl,取消关联它和virtualenv似乎仍然有用但也许那是因为它最初是在我重新安装时链接的.


Dav*_*ght 6

对我来说这个问题的另一个症状是,如果我进入我的virtualenv的python控制台并且import ssl它会错误.原来我的virtualenv没有使用brewpython版本,只是我机器上的默认安装.不知道为什么默认安装突然停止工作,但这是我如何修复它的问题:

  • rmvirtualenv myvirtualenv
  • brew update
  • brew reinstall python
  • mkvirtualenv -p /usr/local/Cellar/python/whatever_version_number/bin/python myvirtualenv