簡體   English   中英

導入錯誤:無法使用 PIP 導入名稱 HTTPSHandler

[英]ImportError: cannot import name HTTPSHandler using 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

我曾經編輯 Modules/setup.dist 文件並取消注釋 SSL 代碼行並重建它,參考以下線程: 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 e.g.: 
-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 支持,則需要在構建 Python 之前安裝 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

蟒蛇 3

brew uninstall python3

brew install python3 --with-brewed-openssl

蟒蛇 2

brew uninstall python

brew install python --with-brewed-openssl

這個答案整合了我發現的所有 Stack Exchange 答案和評論,並且主要基於這個Apple Stack Exchange answer

需要在make之前安裝OpenSSl並安裝Python來解決這個問題。

在 Centos 上:

yum install openssl openssl-devel -y

來源

我在 Mac OSX 上遇到了這個問題,即使在確認我的 PATH 等之后也是如此。

做了一個; pip卸載virtualenv然后安裝virtualenv,它現在似乎可以工作了。

當時我強迫 brew 鏈接 openssl,取消鏈接它,virtualenv 似乎仍然有效,但也許這是因為它最初是在我重新安裝時鏈接的。

對我來說這個問題的另一個症狀是,如果我進入我的 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需要HTTPSHandler ,它是SSL庫的一部分。

操作系統

在 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 並遇到了同樣的問題。

我的解決方案是:

  1. 安裝 openssl 和 openssl-devel ---- yum install openssl openssl-devel -y
  2. 安裝 krb5-devel ---- yum 安裝 krb5-devel
  3. 切換到你的python目錄並重新編譯它----make

如果我沒有做第二步,當我重新編譯我的 python2.7 時,日志會說“無法構建模塊 _ssl”。

在許多情況下,這是由過時的 virtualenv 引起的,這里有一個重新生成 virtualenv 的腳本: 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 e.g.:
  -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而不是用源碼安裝可能可以解決依賴問題。

剛剛安裝了ssl pyopenssl

pip install pyopenssl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM