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

在使用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 

我曾经编辑过Modules / setup.dist文件,取消注释SSL代码行并重build它,参考以下线程: http : //forums.opensuse.org/english/get-technical-help-here/applications/488962-opensuse -python-的OpenSSL 2.HTML

OSX +自制软件用户:

你可以得到配方的最新更新:

 brew reinstall python 

但是,如果你仍然遇到这个问题,比如你升级了你的操作系统,那么你可能需要先获得最新的openssl。 您可以从以下位置查看使用哪个版本以及在哪里使用:

 openssl version -a which openssl 

要获得最新的openssl:

 brew update brew install openssl brew link --overwrite --dry-run openssl # safety first. brew link openssl --overwrite 

这可能会发出警告:

 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 eg: -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib 

附注:此警告意味着对于其他应用程序,您可能需要使用

 export LDFLAGS=-L/usr/local/opt/openssl/lib export CPPFLAGS=-I/usr/local/opt/openssl/include 

然后重新编译python:

 brew uninstall python brew install python --with-brewed-openssl 

或者为python 3

 brew uninstall python3 brew install python3 --with-brewed-openssl 

如果您需要SSL支持,您需要在构buildPython之前安装OpenSSL头文件。 在Debian和Ubuntu上,它们位于名为libssl-dev的包中。 正如这里所指出的那样 ,您可能需要更多的依赖关系。

家酿

这可能是由于升级小牛造成的。 这是我如何修复它。

更新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 

重新安装Python

Python 3

 brew uninstall python3 brew install python3 --with-brewed-openssl 

Python 2

 brew uninstall python brew install python --with-brewed-openssl 

这个答案巩固了我发现的所有Stack Exchange的答案和评论,主要基于这个Apple Stack Exchange的答案 。

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

做了一个; 点卸载virtualenv然后安装virtualenv,它似乎现在起作用。

当时我强迫brew连接openssl,unlinked它和virtualenv似乎仍然工作,但也许这是因为它是最初连接时,我重新安装它。

在制作和安装Python之前,您需要安装OpenSSl来解决问题。

在Centos上:

 yum install openssl openssl-devel -y 

资源

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

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

看来你的pip需要HTTPSHandlerSSL库的一部分。

OSX

在OS X上,您应该在Python安装期间链接OpenSSL(请参阅: #14497 )。

对于Python 2:

 brew reinstall python --with-brewed-openssl pip install --upgrade pip 

对于Python 3:

 brew reinstall python3 --with-brewed-openssl pip3 install --upgrade pip 

您可以将多个Python实例组合在一起,以列出它们的运行:

 brew list | grep ^python 

或者通过ls -al /usr/local/lib/python*列出你的版本。

我正在使用Redhat,并遇到同样的问题。

我的解决scheme是:

  1. 安装openssl和openssl-devel —- yum install openssl openssl-devel -y
  2. 安装krb5-devel —- yum安装krb5-devel
  3. 改到你的python目录并重新编译—- make

如果我没有做第二步,当我重新编译我的python2.7,日志会说“无法build立模块_ssl”。

在许多情况下,这是由过时的virtualenv引起的,下面是一个脚本来重新生成你的virtualenv(s): https : //gist.github.com/WoLpH/fb98f7dc6ba6f05da2b8

只需将其复制到一个文件(上面的可下载链接),并像这样执行它: zsh -e recreate_virtualenvs.sh <project_name>

 #!/bin/zsh -e if [ ! -d "$PROJECT_HOME" ]; then echo 'Your $PROJECT_HOME needs to be defined' echo 'http://virtualenvwrapper.readthedocs.org/en/latest/install.html#location-of-project-directories' exit 1 fi if [ "" = "$1" ]; then echo "Usage: $0 <project_name>" exit 1 fi env="$1" project_dir="$PROJECT_HOME/$1" env_dir="$HOME/envs/$1" function command_exists(){ type $1 2>/dev/null | grep -vq ' not found' } if command_exists workon; then echo 'Getting virtualenvwrapper from environment' # Workon exists, nothing to do :) elif [ -x ~/bin/mount_workon ]; then echo 'Using mount workon' # Optional support for packaged project directories and virtualenvs using # https://github.com/WoLpH/dotfiles/blob/master/bin/mount_workon . ~/bin/mount_workon mount_file "$project_dir" mount_file "$env_dir" elif command_exists virtualenvwrapper.sh; then echo 'Using virtualenvwrapper' . $(which virtualenvwrapper.sh) fi if ! command_exists workon; then echo 'Virtualenvwrapper not found, please install it' exit 1 fi rmvirtualenv $env || true echo "Recreating $env" mkvirtualenv $env || true workon "$env" || true pip install virtualenv{,wrapper} cd $project_dir setvirtualenvproject if [ -f setup.py ]; then echo "Installing local package" pip install -e . fi function install_requirements(){ # Installing requirements from given file, if it exists if [ -f "$1" ]; then echo "Installing requirements from $1" pip install -r "$1" fi } install_requirements requirements_test.txt install_requirements requirements-test.txt install_requirements requirements.txt install_requirements test_requirements.txt install_requirements test-requirements.txt if [ -d docs ]; then echo "Found docs, installing sphinx" pip install sphinx{,-pypi-upload} py fi echo "Installing ipython" pip install ipython if [ -f tox.ini ]; then deps=$(python -c " parser=__import__('ConfigParser').ConfigParser(); parser.read('tox.ini'); print parser.get('testenv', 'deps').strip().replace('{toxinidir}/', '')") echo "Found deps from tox.ini: $deps" echo $deps | parallel -v --no-notice pip install {} fi if [ -f .travis.yml ]; then echo "Found deps from travis:" installs=$(grep 'pip install' .travis.yml | grep -v '\$' | sed -e 's/.*pip install/pip install/' | grep -v 'pip install . --use-mirrors' | sed -e 's/$/;/') echo $installs eval $installs fi deactivate 

在OSX上,brew不断地拒绝与它的openssl链接,并且出现这个错误:

 15:27 $ brew link --force 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 eg: -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib 

我终于能够得到它的工作:

  brew remove openssl brew uninstall --force openssl brew install openssl export LDFLAGS=-L/usr/local/opt/openssl/lib export CPPFLAGS=-I/usr/local/opt/openssl/include brew remove python brew update brew install python 

对于Ubuntu

首先检查是否安装openssl-develop

 sudo apt-get install libssl-dev 

尝试另一种方式重新安装pip

 sudo apt-get install python-setuptools sudo easy_install pip 

使用setuptools安装pip而不是使用源代码安装可能可以解决依赖问题。