question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'ImportError: No module named ssl_match_hostname' upon 'import docker' in a Python script

See original GitHub issue

In a script ~/Documents/Scratch/test_import_docker.py with the single statement import docker, I’m getting the following ImportError:

Traceback (most recent call last):
  File "test_import_docker.py", line 1, in <module>
    import docker
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/__init__.py", line 6, in <module>
    from .client import Client, AutoVersionClient, from_env # flake8: noqa
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/client.py", line 11, in <module>
    from . import api
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/api/__init__.py", line 2, in <module>
    from .build import BuildApiMixin
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/api/build.py", line 9, in <module>
    from .. import utils
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/utils/__init__.py", line 2, in <module>
    from .utils import (
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/utils/utils.py", line 19, in <module>
    from .. import tls
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/tls.py", line 5, in <module>
    from .ssladapter import ssladapter
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/ssladapter/__init__.py", line 1, in <module>
    from .ssladapter import SSLAdapter # flake8: noqa
  File "/home/kurt/.local/lib/python2.7/site-packages/docker/ssladapter/ssladapter.py", line 21, in <module>
    from backports.ssl_match_hostname import match_hostname
ImportError: No module named ssl_match_hostname

Strangely, I am able to import docker from iPython:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import docker

In [2]: 

Why is the import not working in the first case?

My Docker version from the command line is:

kurt@kurt-ThinkPad:~$ docker --version
Docker version 1.12.3, build 6b644ec

whereas the version in Python is

In [3]: docker.version
Out[3]: '1.10.3'

Incidentally, I’ve also created a StackOverflow question about this.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:17
  • Comments:41 (2 by maintainers)

github_iconTop GitHub Comments

82reactions
msholty-fdcommented, Sep 4, 2018

I just had this fun issue while setting up my Ubuntu 18.x machine. docker-compose was working for me, then this morning I tried to do some additional installation of some libraries, specifically using python3.6 and suddenly my docker-compose and docker wasn’t working.

I eventually figured out that the version of backports that python2 was trying to import was here: /usr/lib/python2.7/dist-packages/backports

Even though I had another version of it here: /usr/local/lib/python2.7/dist-packages/backports

So I ran: cp -r /usr/local/lib/python2.7/dist-packages/backports/ssl_match_hostname/ /usr/lib/python2.7/dist-packages/backports

And that fixed it and docker-compose works again.

72reactions
lgilzcommented, Feb 26, 2018

I had the same problem on my ubuntu 17.04. I solved it by installing via apt get

sudo pip uninstall docker docker-compose
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install docker docker-compose
Read more comments on GitHub >

github_iconTop Results From Across the Web

ImportError: No module named ssl_match_hostname when ...
On Ubuntu/Debian, there is a bug related to package upgrade (bug #893520) at the moment, where the __init__.py file is not generated.
Read more >
ImportError: No module named backports.ssl_match_hostname
When using upstream docker (i.e. docker-ce from download.docker.com, not docker 1.5 from Ubuntu), docker-compose dies with this ImportError.
Read more >
ImportError: No module named backports.ssl_match_hostname
Package: python-docker Version: 2.5.1-1 Severity: grave Hi, Seems that python-docker completely fails: >>> import docker Traceback (most ...
Read more >
Debugging ImportError and ModuleNotFoundErrors in your ...
Your code runs fine on your computer, but when you try to package it with Docker you keep getting ImportError s or ModuleNotFoundError...
Read more >
ImportError: No module named ssl_match_hostname when ...
Coding example for the question ImportError: No module named ssl_match_hostname when importing the docker SDK for Python-docker.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found