ImportError: cannot import name HTTPSHandler using

2019-01-06 12:39发布

Facing an HTTPSHandler error while installing python packages using pip, following is the stack trace,

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

I used to edit Modules/setup.dist file and uncomment SSL code lines and rebuilt it, with reference to following thread : http://forums.opensuse.org/english/get-technical-help-here/applications/488962-opensuse-python-openssl-2.html

11条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-06 13:17

Homebrew

This was probably caused by an upgrade to Mavericks. Here's how I fixed it.

Update 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

Reinstall Python

Python 3

brew uninstall python3

brew install python3 --with-brewed-openssl

Python 2

brew uninstall python

brew install python --with-brewed-openssl

This answer consolidates all the Stack Exchange answers and comments I found, and is based mostly on this Apple Stack Exchange answer.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-06 13:17

In many cases this is caused by an out of date virtualenv, here's a script to regenerate your virtualenv(s): https://gist.github.com/WoLpH/fb98f7dc6ba6f05da2b8

Simply copy it to a file (downloadable link above) and execute it like this: 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
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-06 13:20

OSX + homebrew users:

You can get the latest updates to the recipe:

brew reinstall python

But if you still get the issue, e.g. maybe you have upgraded your OS, then you may need to get the latest openssl first. You can check which version and where it is used from:

openssl version -a
which openssl

To get the latest openssl:

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

This may issue a warning:

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

Side note: this warning means that for other apps, you may want to use

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

Then recompile python:

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

or for python 3

brew uninstall python3
brew install python3 --with-brewed-openssl
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-06 13:22

You need to install the OpenSSL header files before building Python if you need SSL support. On Debian and Ubuntu, they are in a package called libssl-dev. You might need some more dependencies, as noted here.

查看更多
Animai°情兽
6楼-- · 2019-01-06 13:28

I'm using Redhat and have met the same problem.

My solution is :

  1. install openssl and openssl-devel ---- yum install openssl openssl-devel -y
  2. install krb5-devel ---- yum install krb5-devel
  3. change to your python's directory and recompile it ---- make

If I didn't do the 2nd step, when I recompiled my python2.7 , the log would say " fail to build module _ssl".

查看更多
登录 后发表回答