【问题标题】:ImportError: cannot import name HTTPSHandler using PIPImportError:无法使用 PIP 导入名称 HTTPSHandler
【发布时间】:2014-01-08 09:29:41
【问题描述】:

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

【问题讨论】:

  • 你是如何安装python的?你能在 python shell 中 import sslimport _ssl 吗?
  • 你在什么系统上?
  • 我做了./configure,make,sudo make altinstall,然后添加了带有虚拟环境的python2.7;但是在执行 pip 时,它会返回相同的错误。导入 ssl“ImportError:没有名为 _ssl 的模块”。还使用 --with-ssl 并通过在可下载的 Modules/Setup.dist 中取消注释 ssl 行再次重建。
  • Ubuntu 10.04 和 i5 3330 64 位架构
  • 在 OS X 上遇到同样的错误。有人知道如何在 Mac 上解决这个问题吗?

标签: python ssl openssl pip


【解决方案1】:

刚刚安装了ssl pyopenssl

pip install pyopenssl

【讨论】:

  • 使用 cmets 功能与用户互动,而不是发布您的评论作为答案。
【解决方案2】:

对于 Ubuntu

首先检查是否安装 openssl-develop

sudo apt-get install libssl-dev

尝试其他方式重新安装pip

sudo apt-get install python-setuptools
sudo easy_install pip

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

【讨论】:

    【解决方案3】:

    OSX + homebrew 用户:

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

    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
    

    【讨论】:

    • 如果您安装了多个 python 版本,请不要认为这会起作用
    • 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
    • 当我尝试brew install python3 --with-brewed-openssl 时收到错误Error: invalid option: --with-brewed-openssl。这个答案有更新吗?
    【解决方案4】:

    在 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
    

    【讨论】:

      【解决方案5】:

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

      我的解决办法是:

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

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

      【讨论】:

        【解决方案6】:

        自制

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

        更新 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 答案和 cmets,主要基于此 Apple Stack Exchange answer

        【讨论】:

        • Prefect,我在 El Capitan 10.11 beta 上获得了 ImportError: cannot import name HTTPSHandler,我最终使用了 brew updatebrew install openssl,因为我已经安装了 pip 和 python。谢谢
        • 谢谢!注意:brew upgrade 不仅会重新安装 python,还会同时重新安装其他可能已过时的模块。这可能需要几分钟,具体取决于您安装的其他内容。
        【解决方案7】:

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

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

        【讨论】:

        • Daniel 和我收到权限被拒绝错误。有什么想法吗?
        • 我应该指向/usr/local/Cellar/python/whatever_version_number/bin/python 而不是/usr/local/Cellar/python/whatever_version_number/ 在上面进行了更改。我希望你和丹尼尔能成功。
        【解决方案8】:

        您的pip 似乎需要HTTPSHandler,它是SSL 库的一部分。

        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*列出您的版本。

        【讨论】:

          【解决方案9】:

          make前需要安装OpenSSl,安装Python即可解决问题。

          在 Centos 上:

          yum install openssl openssl-devel -y
          

          source

          【讨论】:

          • 我安装了openssl,但我在构建python 2.7.10时仍然遇到问题
          • 消息:python get-pip.py Traceback(最近一次调用最后):文件“get-pip.py”,第 17759 行,在 main() 文件“get-pip.py ",第 162 行,在主引导中 (tmpdir=tmpdir) 文件“get-pip.py”,第 82 行,在引导中导入 pip
          • 文件“/tmp/tmpaVpnhQ/pip.zip/pip/__init__.py”,第 15 行,在 文件“/tmp/tmpaVpnhQ/pip.zip/pip/vcs/subversion. py”,第 9 行,在 文件中“/tmp/tmpaVpnhQ/pip.zip/pip/index.py”,第 30 行,在 文件中“/tmp/tmpaVpnhQ/pip.zip/pip/wheel. py",第 35 行,在 文件 "/tmp/tmpaVpnhQ/pip.zip/pip/_vendor/distlib/scripts.py",第 14 行,在 文件 "/tmp/tmpaVpnhQ/pip.zip/ pip/_vendor/distlib/compat.py",第 31 行,在 ImportError: cannot import name HTTPSHandler
          【解决方案10】:

          在许多情况下,这是由过时的 virtualenv 引起的,这里有一个脚本可以重新生成您的 virtualenv:https://gist.github.com/WoLpH/fb98f7dc6ba6f05da2b8

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

          #!/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
          

          【讨论】:

            【解决方案11】:

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

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

            当时我强迫 brew 链接 openssl,取消链接,virtualenv 似乎仍然可以工作,但可能是因为我重新安装它时它最初是链接的。

            【讨论】:

              【解决方案12】:

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

              【讨论】:

              • 嘿,谢谢,是的,它需要一些依赖项,现在它正在工作。
              猜你喜欢
              • 2016-03-23
              • 1970-01-01
              • 2020-05-07
              • 2021-07-30
              • 2022-08-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-14
              相关资源
              最近更新 更多