Python 导入错误:无法使用 PIP 导入名称 HTTPSHandler

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20688034/
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-18 21:01:53  来源:igfitidea点击:

ImportError: cannot import name HTTPSHandler using PIP

pythonsslopensslpip

提问by user3016020

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

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

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

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

回答by sk1p

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.

如果需要 SSL 支持,则需要在构建 Python 之前安装 OpenSSL 头文件。在 Debian 和 Ubuntu 上,它们位于一个名为libssl-dev. 您可能需要一些更多的依赖关系,因为这里要注意

回答by leon

You need to install OpenSSl before make and install Python to solve the problem.

需要在make之前安装OpenSSl并安装Python来解决这个问题。

On Centos:

在 Centos 上:

yum install openssl openssl-devel -y

source

来源

回答by jay

I was having this problem on Mac OSX, even after confirming my PATH, etc.

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

Did a; pip uninstall virtualenv then install virtualenv and it seemed to works now.

做了一个;pip卸载virtualenv然后安装virtualenv,它现在似乎可以工作了。

At the time I had forced brew to link openssl, unlinked it and virtualenv still seems to work but maybe that's because it was originally linked when I reinstalled it.

当时我强迫 brew 链接 openssl,取消链接它,virtualenv 似乎仍然有效,但也许这是因为它最初是在我重新安装时链接的。

回答by David Wright

Another symptom of this problem for me was if I went into the python console of my virtualenv and did import sslit would error out. Turns out my virtualenv wasn't using the brewversion of python, just the default install on my machine. No clue why the default install suddenly stopped working, but here's how I fixed it the problem:

对我来说,这个问题的另一个症状是,如果我进入我的 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
  • rmvirtualenv myvirtualenv
  • brew update
  • brew reinstall python
  • mkvirtualenv -p /usr/local/Cellar/python/whatever_version_number/bin/python myvirtualenv

回答by Wolph

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

在许多情况下,这是由过时的 virtualenv 引起的,这里有一个重新生成 virtualenv 的脚本: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>

只需将它复制到一个文件(上面的可下载链接)并像这样执行它: 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 [ "" = "" ]; then
    echo "Usage: 
brew reinstall python
<project_name>" exit 1 fi env="" project_dir="$PROJECT_HOME/" env_dir="$HOME/envs/" function command_exists(){ type 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 "" ]; then echo "Installing requirements from " pip install -r "" 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

回答by dnozay

OSX + homebrew users:

OSX + 自制软件用户:

You can get the latest updates to the recipe:

您可以获得食谱的最新更新:

openssl version -a
which openssl

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。您可以从以下位置检查使用哪个版本和位置:

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

To get the latest openssl:

要获取最新的 openssl:

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

This may issue a warning:

这可能会发出警告:

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

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

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

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

Then recompile python:

然后重新编译python:

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

or for python 3

或者对于python 3

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

回答by kenorb

It seems your piprequires HTTPSHandlerwhich is part of SSLlibrary.

看来您的pip需求HTTPSHandlerSSL库的一部分。

OSX

操作系统

On OS X you should link OpenSSL during Python installation (See: #14497).

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

For Python 2:

对于 Python 2:

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

For Python 3:

对于 Python 3:

brew list | grep ^python

You could have multiple Python instances together, to list them run:

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

#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

Or list your version via ls -al /usr/local/lib/python*.

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

回答by Waylon Flinn

Homebrew

家酿

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

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

Update OpenSSL

更新 OpenSSL

brew uninstall python3

brew install python3 --with-brewed-openssl

Reinstall Python

重新安装 Python

Python 3

蟒蛇 3

brew uninstall python

brew install python --with-brewed-openssl

Python 2

蟒蛇 2

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

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

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

回答by April

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

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

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
  1. 安装 openssl 和 openssl-devel ---- yum install openssl openssl-devel -y
  2. 安装 krb5-devel ---- yum 安装 krb5-devel
  3. 切换到你的python目录并重新编译它----make

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

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

回答by Brandon

On OSX, brew kept refusing to link against its openssl with this error:

在 OSX 上,brew 一直拒绝链接到其 openssl 并出现以下错误:

  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

I finally was able to get it working with:

我终于能够让它与:

##代码##