pip raises ImportError: cannot import name HTTPSHandler

21,328

After some struggling I figured it out. In short (not really sure if the first command is necessary):

sudo apt-get install libssl-dev
sudo apt-get --reinstall install libpython2.7-stdlib

The long explanation is that, based on the places Python is looking for libraries:

python -c "import sys; print sys.path"

and seeing that there was an _ssl.so file in /usr/lib/python2.7/lib-dynload on a "healthy" other 14.04.1 server, I figured out that the package responsible for this is libpython2.7-stdlib:

$ dpkg -S /usr/lib/python2.7/lib-dynload/_ssl.x86_64-linux-gnu.so
libpython2.7-stdlib:amd64: /usr/lib/python2.7/lib-dynload/_ssl.x86_64-linux-gnu.so

After reinstalling libpython2.7-stdlib on the original machine, the file was recreated and the problem went away.

sudo apt-get --reinstall install libpython2.7-stdlib

(I really don't know how or why was this _ssl file missing from the system - I'm not the only person using the server...)

Share:
21,328

Related videos on Youtube

metakermit
Author by

metakermit

Updated on September 18, 2022

Comments

  • metakermit
    metakermit over 1 year

    After installing the python-pip package in Ubuntu 14.04.1 I get:

    $ pip
    Traceback (most recent call last):
      File "/usr/bin/pip", line 9, in <module>
        load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 351, in load_entry_point
        return get_distribution(dist).load_entry_point(group, name)
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2363, in load_entry_point
        return ep.load()
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2088, in load
        entry = __import__(self.module_name, globals(),globals(), ['__name__'])
      File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 10, in <module>
        from pip.util import get_installed_distributions, get_prog
      File "/usr/lib/python2.7/dist-packages/pip/util.py", line 18, in <module>
        from distlib import version
      File "/usr/lib/python2.7/dist-packages/distlib/version.py", line 14, in <module>
        from .compat import string_types
      File "/usr/lib/python2.7/dist-packages/distlib/compat.py", line 31, in <module>
        from urllib2 import (Request, urlopen, URLError, HTTPError,
    ImportError: cannot import name HTTPSHandler
    

    I checked, I do have libssl-dev and python-openssl installed and the problem seems to be that the ssl module can't be imported:

    >>> import ssl
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/ssl.py", line 61, in <module>
        import _ssl             # if we can't import it, let the error propagate
    ImportError: No module named _ssl
    

    Everything is installed from the Ubuntu repositories, though, so I don't know what's causing the problem.

  • Edward Moffett
    Edward Moffett about 5 years
    python 3 equivalent is python3 -c "import sys; print(sys.path)"