In order to use the Docker SDK for Python, I\'m trying to import docker
in a Python script, but it\'s resulting in an ImportError
with the following tr
The issue could be there because of different versions of Docker-compose and Docker. First compare the version and upgrade it to make it compatible.
Secondly,you can uninstall the backport package (via pip).
sudo pip uninstall backports.ssl-match-hostname
and then install it again as a ubuntu package.
sudo apt-get install python-backports.ssl-match-hostname
On Ubuntu/Debian, there is a bug related to package upgrade (bug #893520) at the moment, where the __init__.py
file is not generated.
Check if the following file exists: /usr/lib/python2.7/dist-packages/backports/__init__.py
If it does not exist and the package python-backports.ssl-match-hostname
is installed, then run the following command to generate the missing file:
sudo pycompile -p python-backports.ssl-match-hostname
Don't install it with other backports packages until fixed (if they fix it)
Seen in https://bitbucket.org/ambv/configparser/issues/17/importerror-when-used-with-other-backports#comment-36669436 :
Michał Górny
Well, I just wanted to report the core issue here but it seems that other already have learned it the hard way.
Long story short, you can't ever mix pkg_resources namespaces and pkgutil namespaces. It's explained in the Python packaging guide. Attempting to import both is probably the best way to cause random breakage for everyone. You have to decide on one of them, and use it consistently across all packages using the backports namespace.
Now, the backports package seems to serve the sole purpose of claiming the namespace, and it uses pkgutil. So I'd suggest following that one.
Upgrading docker didn't solve the problem for me. Instead, I had to install the backports as Ubuntu package (and uninstall from pip):
sudo pip uninstall backports.ssl-match-hostname
sudo apt-get install python-backports.ssl-match-hostname
If you're seeing this from docker-compose:
For me the answer by @CGFoX didn't work, even though this was in the correct direction. When analyzing the issue, we figured that there were two python paths for dist-packages, /usr/local/lib/python2.7/dist-packages/backports
and /usr/lib/python2.7/dist-packages/backports
.
Python was trying to load the module 'ssl_match_hostname' module from /usr/local/lib/python2.7/dist-packages/backports. But it was not present there:
drwxr-sr-x 5 root staff 4096 Aug 12 07:46 ./
drwxrwsr-x 130 root staff 12288 Aug 12 07:37 ../
-rw-r--r-- 1 root staff 75 Nov 14 2018 __init__.py
-rw-r--r-- 1 root staff 269 Nov 14 2018 __init__.pyc
drwxr-sr-x 2 root staff 4096 Nov 14 2018 configparser/
drwxr-sr-x 2 root staff 4096 Nov 14 2018 shutil_get_terminal_size/
But the module is present in the other location.
x@soumubuntu:~$ ll /usr/lib/python2.7/dist-packages/backports
total 16
drwxr-xr-x 3 root root 4096 Aug 12 07:19 ./
drwxr-xr-x 78 root root 4096 Aug 12 06:51 ../
-rw-r--r-- 1 root root 0 Aug 9 03:05 __init__.py
-rw-r--r-- 1 root root 141 Aug 12 07:19 __init__.pyc
drwxr-xr-x 2 root root 4096 Aug 12 07:19 ssl_match_hostname/
So, I copied it from B->A:
sudo cp -r /usr/lib/python2.7/dist-packages/backports/ssl_match_hostname/ /usr/local/lib/python2.7/dist-packages/backports/
and voila, docker compose is working now.
I faced the similar issue however was able to resolve by first uninstalling with following commands -
pip uninstall backports.ssl_match_hostname
Once it gets uninstalled successfully then use following commands to install docker client with -
pip install docker-py
I hope this would help to resolve this issue