docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

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

Open khpeek opened this issue 8 years ago • 41 comments

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.

khpeek avatar Mar 09 '17 12:03 khpeek

whereas the version in Python is

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

This is the version of library, not docker engine.

When you import docker in IPython, can you print the path of imported package?

In [3]: docker.__file__
Out[3]: '/usr/lib/python3.6/site-packages/docker/__init__.py'

Can you also import ssl_match_hostname in IPython?

In [4]: from backports.ssl_match_hostname import match_hostname

In [5]:

TomasTomecek avatar Mar 09 '17 13:03 TomasTomecek

I'm seeing this error in Python 2.7 on Ubuntu 17.04 when docker is installed with pip install --user docker. There the "backports" module reports as being:

backports <module 'backports' (built-in)>

with the path of that pointing to /usr/lib/python2.7/dist-packages/backports while the backports that includes the code you are expecting is in ~/.local/lib/python2.7/site-packages/backports

This seems like a fundamental problem with the backports module/package idea when used with --user mode installation of docker and its dependencies. Possibly backports could be fixed to allow for this type of usage, but I can confirm that docker does import if you install it into a virtualenv instead of doing --user installation.

mcfletch avatar Jul 06 '17 18:07 mcfletch

I have the same problem just now. After I just updatting docker ,everything becomes OK.

yexxyy avatar Aug 04 '17 02:08 yexxyy

I've got the same problem and ran sudo pip install --upgrade docker and it worked :)

fchevitarese avatar Sep 25 '17 13:09 fchevitarese

I've got the same problem now. OS: macOS 10.12.3 Python Version:Python 2.7.13 (default, Jun 26 2017, 20:06:48) Docker Version: Docker version 17.09.0-ce, build afdb6d4

$ pip show docker Name: docker Version: 2.5.0 Summary: A Python library for the Docker Engine API. Home-page: https://github.com/docker/docker-py Author: Joffrey F Author-email: [email protected] License: Apache License 2.0 Location: /usr/local/lib/python2.7/site-packages Requires: docker-pycreds, requests, backports.ssl-match-hostname, ipaddress, six, websocket-client

$ python Python 2.7.13 (default, Jun 26 2017, 20:06:48) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import docker Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/site-packages/docker/init.py", line 2, in from .api import APIClient File "/usr/local/lib/python2.7/site-packages/docker/api/init.py", line 2, in from .client import APIClient File "/usr/local/lib/python2.7/site-packages/docker/api/client.py", line 11, in from .build import BuildApiMixin File "/usr/local/lib/python2.7/site-packages/docker/api/build.py", line 9, in from .. import utils File "/usr/local/lib/python2.7/site-packages/docker/utils/init.py", line 2, in from .build import tar, exclude_paths File "/usr/local/lib/python2.7/site-packages/docker/utils/build.py", line 5, in from .utils import create_archive File "/usr/local/lib/python2.7/site-packages/docker/utils/utils.py", line 18, in from .. import tls File "/usr/local/lib/python2.7/site-packages/docker/tls.py", line 5, in from .transport import SSLAdapter File "/usr/local/lib/python2.7/site-packages/docker/transport/init.py", line 3, in from .ssladapter import SSLAdapter File "/usr/local/lib/python2.7/site-packages/docker/transport/ssladapter.py", line 21, in from backports.ssl_match_hostname import match_hostname ImportError: No module named ssl_match_hostname

xiamencloud avatar Oct 19 '17 11:10 xiamencloud

Hi @fjhaveri. Have you tried pip install --upgrade docker? How is your pip list?

feliperuhland avatar Jan 24 '18 00:01 feliperuhland

I'm facing a similar error with Docker python image - I'm using Windows machine with latest version of Docker Edge - i get this error: ImportError: No module named XXXXXX

KedemBarak avatar Jan 25 '18 14:01 KedemBarak

I'm getting this when I try to do a pip install --user docker-compose:

''' $ pip install --user docker-compose Collecting docker-compose Using cached docker_compose-1.19.0-py2.py3-none-any.whl Collecting websocket-client<1.0,>=0.32.0 (from docker-compose) Using cached websocket_client-0.46.0-py2.py3-none-any.whl Collecting backports.ssl-match-hostname>=3.5; python_version < "3.5" (from docker-compose) Using cached backports.ssl_match_hostname-3.5.0.1.tar.gz Collecting PyYAML<4,>=3.10 (from docker-compose) Using cached PyYAML-3.12.tar.gz Collecting dockerpty<0.5,>=0.4.1 (from docker-compose) Using cached dockerpty-0.4.1.tar.gz Collecting ipaddress>=1.0.16; python_version < "3.3" (from docker-compose) Using cached ipaddress-1.0.19.tar.gz Collecting docopt<0.7,>=0.6.1 (from docker-compose) Using cached docopt-0.6.2.tar.gz Collecting enum34<2,>=1.0.4; python_version < "3.4" (from docker-compose) Using cached enum34-1.1.6-py2-none-any.whl Collecting docker<3.0,>=2.7.0 (from docker-compose) Using cached docker-2.7.0-py2.py3-none-any.whl Collecting requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1 (from docker-compose) Using cached requests-2.18.4-py2.py3-none-any.whl Collecting texttable<0.10,>=0.9.0 (from docker-compose) Using cached texttable-0.9.1.tar.gz Collecting cached-property<2,>=1.2.0 (from docker-compose) Using cached cached_property-1.3.1-py2.py3-none-any.whl Collecting jsonschema<3,>=2.5.1 (from docker-compose) Using cached jsonschema-2.6.0-py2.py3-none-any.whl Collecting six<2,>=1.3.0 (from docker-compose) Using cached six-1.11.0-py2.py3-none-any.whl Collecting docker-pycreds>=0.2.1 (from docker<3.0,>=2.7.0->docker-compose) Using cached docker_pycreds-0.2.1-py2.py3-none-any.whl Collecting certifi>=2017.4.17 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose) Using cached certifi-2018.1.18-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose) Using cached chardet-3.0.4-py2.py3-none-any.whl Collecting idna<2.7,>=2.5 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose) Using cached idna-2.6-py2.py3-none-any.whl Collecting urllib3<1.23,>=1.21.1 (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose) Using cached urllib3-1.22-py2.py3-none-any.whl Collecting functools32; python_version == "2.7" (from jsonschema<3,>=2.5.1->docker-compose) Using cached functools32-3.2.3-2.zip Installing collected packages: six, websocket-client, backports.ssl-match-hostname, PyYAML, dockerpty, ipaddress, docopt, enum34, docker-pycreds, certifi, chardet, idna, urllib3, requests, docker, texttable, cached-property, functools32, jsonschema, docker-compose Running setup.py install for backports.ssl-match-hostname ... error Complete output from command /usr/bin/python2 -u -c "import setuptools, tokenize;file='/tmp/pip-build-WGvE7j/backports.ssl-match-hostname/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-QUZVUK-record/install-record.txt --single-version-externally-managed --compile --user --prefix=: running install running build running build_py creating build creating build/lib creating build/lib/backports copying backports/init.py -> build/lib/backports creating build/lib/backports/ssl_match_hostname copying backports/ssl_match_hostname/init.py -> build/lib/backports/ssl_match_hostname running install_lib creating /home/myusername/.local/lib/python2.7/site-packages/backports copying build/lib/backports/init.py -> /home/myusername/.local/lib/python2.7/site-packages/backports creating /home/myusername/.local/lib/python2.7/site-packages/backports/ssl_match_hostname copying build/lib/backports/ssl_match_hostname/init.py -> /home/myusername/.local/lib/python2.7/site-packages/backports/ssl_match_hostname byte-compiling /home/myusername/.local/lib/python2.7/site-packages/backports/init.py to init.pyc byte-compiling /home/myusername/.local/lib/python2.7/site-packages/backports/ssl_match_hostname/init.py to init.pyc running install_egg_info running egg_info creating backports.ssl_match_hostname.egg-info writing backports.ssl_match_hostname.egg-info/PKG-INFO writing top-level names to backports.ssl_match_hostname.egg-info/top_level.txt writing dependency_links to backports.ssl_match_hostname.egg-info/dependency_links.txt writing manifest file 'backports.ssl_match_hostname.egg-info/SOURCES.txt' warning: manifest_maker: standard file '-c' not found

reading manifest file 'backports.ssl_match_hostname.egg-info/SOURCES.txt'
writing manifest file 'backports.ssl_match_hostname.egg-info/SOURCES.txt'
Copying backports.ssl_match_hostname.egg-info to /home/myusername/.local/lib/python2.7/site-packages/backports.ssl_match_hostname-3.5.0.1-py2.7.egg-info
running install_scripts
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-WGvE7j/backports.ssl-match-hostname/setup.py", line 33, in <module>
    packages=['backports', 'backports.ssl_match_hostname'],
  File "/usr/lib64/python2.7/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/usr/lib64/python2.7/distutils/dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/lib/python2.7/site-packages/setuptools/command/install.py", line 53, in run
    return _install.run(self)
  File "/usr/lib64/python2.7/distutils/command/install.py", line 575, in run
    self.run_command(cmd_name)
  File "/usr/lib64/python2.7/distutils/cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command
    cmd_obj.run()
  File "/usr/lib/python2.7/site-packages/setuptools/command/install_scripts.py", line 15, in run
    from setuptools.command.easy_install import get_script_args
  File "/usr/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 49, in <module>
    from setuptools.package_index import PackageIndex
  File "/usr/lib/python2.7/site-packages/setuptools/package_index.py", line 14, in <module>
    from setuptools import ssl_support
  File "/usr/lib/python2.7/site-packages/setuptools/ssl_support.py", line 85, in <module>
    from backports.ssl_match_hostname import CertificateError, match_hostname
ImportError: No module named ssl_match_hostname

----------------------------------------

Command "/usr/bin/python2 -u -c "import setuptools, tokenize;file='/tmp/pip-build-WGvE7j/backports.ssl-match-hostname/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-QUZVUK-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-WGvE7j/backports.ssl-match-hostname/ '''

michaeldayCA avatar Feb 09 '18 20:02 michaeldayCA

The following workaround worked for me (sudo pip install --upgrade docker didn't):

sudo cp -r ~/.local/lib/python2.7/site-packages/backports/ssl_match_hostname/ /usr/lib/python2.7/dist-packages/backports

mulmschneider avatar Feb 25 '18 17:02 mulmschneider

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

lgilz avatar Feb 26 '18 09:02 lgilz

On Ubuntu 17.10, a workaround is to uninstall docker python module with pip and install python-docker with apt:

# pip uninstall docker
...
# apt install python-docker
...
# python2 -c 'import docker; print(docker.__version__)'
2.5.1

jean-christophe-manciot avatar Mar 17 '18 09:03 jean-christophe-manciot

On Ubuntu 17.10, a much better workaround is to use a virtualenv.

# virtualenv awx-build-virtualenv
Running virtualenv with interpreter /usr/bin/python2
New python executable in awx-build-virtualenv/bin/python2
Also creating executable in awx-build-virtualenv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
# source awx-build-virtualenv/bin/activate
(awx-build-virtualenv) # pip install docker
Collecting docker
  Using cached docker-3.1.1-py2.py3-none-any.whl
Collecting backports.ssl-match-hostname>=3.5; python_version < "3.5" (from docker)
...
(awx-build-virtualenv) # python2 -c 'import docker; print(docker.__version__)'
3.1.1

jean-christophe-manciot avatar Mar 19 '18 10:03 jean-christophe-manciot

On Ubuntu 17.10, outside a virtualenv, it is possible to import docker pip module with downgraded requests pip module (thanks to this thread):

# pip freeze | grep requests
requests==2.18.1
# pip freeze | grep docker
docker==2.7.0
docker-compose==1.19.0
docker-pycreds==0.2.2
dockerpty==0.4.1
# python2 -c 'import docker; print(docker.__version__)'
2.7.0

jean-christophe-manciot avatar Mar 20 '18 11:03 jean-christophe-manciot

We are running into the same issue: https://plexguide.com/threads/cant-install-any-containers.1062/#post-5984

3 people reporting it via ansible and there is no good solution

Admin9705 avatar May 01 '18 09:05 Admin9705

I had the same problem running docker-compose commands (ImportError: No module named ssl_match_hostname). After reinstall docker and docker-compose with apt the docker-compose commands are working, but I still can't import docker with python -c 'import docker'

raiene avatar May 16 '18 18:05 raiene

you have to like reinstall your box for all the problem to go away

Admin9705 avatar May 16 '18 23:05 Admin9705

I fixed the issue by removing all my ~/.local/lib/python{2,3}, but maybe it could be fixed by running touch ~/.local/lib/python2/dist-packages/backports/__init___.py.

rbelem avatar Jun 19 '18 20:06 rbelem

@jean-christophe-manciot - You were a relentless beast on this. I've been there :0 - Thanks for all the effort! This fixed me on Ubuntu 18.04 as well.

jouellnyc avatar Aug 09 '18 03:08 jouellnyc

None of the given workarounds worked for me. How can be fix this?

et304383 avatar Aug 17 '18 00:08 et304383

I discovered I had a bad version of backports.functools-lru-cache sitting in my .local folder conflicting with the globally installed pip packages.

So uninstalling that seemed to help.

et304383 avatar Aug 29 '18 11:08 et304383

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.

msholty-fd avatar Sep 04 '18 12:09 msholty-fd

@msholty-fd I'm having the same issue with a clean install of Ubuntu 18.04.

What could cause this?

danielfaust avatar Nov 05 '18 22:11 danielfaust

Run bash command is a good solution : mv ~/.local ~/.local.bak

Nick1211 avatar Nov 27 '18 06:11 Nick1211

I'm confronting the same issue and tried to fix it during 12 hours but no fixes found in Ubuntu 18.xx ,

Any one have some suggestions ?

@Nick1211 when you are talking abou ~/.local you mean /local/ ?

because in my /root/.local it's empty 😕

@jean-christophe-manciot How you have fixed this issue on Ubuntu 18.10 ? I'm under a vps and i have this issue also, so if you have any suggestion to fix this issue everything is welcome.

I Have tried to reintal with pip and unistall the pip docker and install it again with aptitude but nothing, the issue still here.

As i have reported here also, https://github.com/Admin9705/PlexGuide.com-The-Awesome-Plex-Server/issues/725

Rgds

ruCyberPoison avatar Dec 13 '18 20:12 ruCyberPoison

$ sudo pip uninstall backports.ssl_match_hostname and then install the docker-py with

$ sudo pip install docker-py

nischay30 avatar Dec 22 '18 14:12 nischay30

@nischay30 something is clearly not right about your recommandation because docker-py is the old/outdated name of the pip package. We are supposed to use just simple docker.

*docker-py had last release back in 2016, is not maintained *.

After doing more testing I was able to find a way to avoid this problem by patching requirments with:

backports.ssl-match-hostname>=3.7.1; python_version=='2.7'  # PSF
# ^ workaround for https://github.com/docker/docker-py/issues/1502

If you miss to add this line, you may endup with a broken docker container on those systems that happen to have an older versions of backports.ssl-match-hostname.

Note: I am not sure which is the first version that avoids this bug but the latest one that I used worked.

ssbarnea avatar Jan 22 '19 17:01 ssbarnea

Ran into the very same problem with a freshly installed Ubuntu 18.10

Edit: what worked for us is to use python3 and ensure the latest version of python module docker is installed (4.0.1 as of today)

sudo apt-get install python3
pip3 --upgrade docker

# pip3 list | grep docker
docker (4.0.1)
docker-compose (1.24.0)
docker-pycreds (0.4.0)
dockerpty (0.4.1)


# Eventually use this variable with Ansible if you're going to use python module docker with a playbook :
ansible_python_interpreter: /usr/bin/python3

hclaria avatar Jan 24 '19 11:01 hclaria

The same problem on Ubuntu 18.10 appeared some time after I updated Python 3.

It's solved by re-installing Docker and Docker Compose:

sudo -H python -m pip uninstall docker docker-compose
sudo apt update
sudo apt upgrade -y
sudo apt install docker-ce docker-compose

If docker-compose still fails to start, follow the installation instructions directly from the Docker documentation: https://docs.docker.com/compose/install/

verify Docker and Docker Compose:

docker --version
docker-compose --version

naXa777 avatar May 22 '19 23:05 naXa777

I randomly got this problem after upgrading on Ubuntu 18.04. It broke automation on all servers running python commands on the docker engine.

g00nix avatar May 29 '19 15:05 g00nix

I just ran into this after installing docker-compose, and at least on the host I was looking at (a new Debian buster installation), the cause came down to an OS package (python-configparser) shipping files under /usr/lib/python2.7/dist-packages/backports. Because of the ordering of Python's sys.path (and I suppose the semantics of submodules), this prevents Python from looking in /usr/local/lib/python2.7/dist-packages/backports (note local in this path), where pip installed the files for backports.ssl-match-hostname.

To anyone else hitting this bug, check whether the path you see via introspection on the backports module matches the path pip installed files under:

# pip show -f backports.ssl-match-hostname
Name: backports.ssl-match-hostname
[...]
Location: /usr/local/lib/python2.7/dist-packages
Requires:
Required-by: docker, docker-compose
Files:
  backports.ssl_match_hostname-3.7.0.1.dist-info/INSTALLER
  backports.ssl_match_hostname-3.7.0.1.dist-info/LICENSE.txt
  backports.ssl_match_hostname-3.7.0.1.dist-info/METADATA
  backports.ssl_match_hostname-3.7.0.1.dist-info/RECORD
  backports.ssl_match_hostname-3.7.0.1.dist-info/WHEEL
  backports.ssl_match_hostname-3.7.0.1.dist-info/top_level.txt
  backports/__init__.py
  backports/__init__.pyc
  backports/ssl_match_hostname/__init__.py
  backports/ssl_match_hostname/__init__.pyc

# python -c 'import backports ; print(backports.__path__)'
['/usr/lib/python2.7/dist-packages/backports']

The above shows the backports module that Python found on import is not the same as the one the ssl_match_hostname submodule is nested under.

I found two possible fixes for my host's environment, though I'd expect the fix is going to vary depending on the specific thing that is confusing the search path.

  1. Uninstall pip's backports.ssl-match-hostname, and install Debian's python-backports.ssl-match-hostname package (this is the approach I went with)
  2. Remove Debian's python-configparser package (which will probably have the effect of removing some other packages that depend on it)

DrewBloechl avatar Jun 27 '19 23:06 DrewBloechl