Docker-py: 'module' object has on attribute 'connection'

Created on 3 May 2016  Â·  29Comments  Â·  Source: docker/docker-py

run on ubuntu 3.19.0-23-generic #24~14.04.1-Ubuntu
docker-py version 1.8.1 and 1.8.0 failed, but version 1.7.0,1.7.2 is OK.

root@ide-staging-01:~# pip freeze | grep docker-py && python --version && docker version
docker-py==1.8.1
Python 2.7.6
Client:
 Version:      1.11.1
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   5604cbe
 Built:        Tue Apr 26 23:30:23 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.11.1
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   5604cbe
 Built:        Tue Apr 26 23:30:23 2016
 OS/Arch:      linux/amd64
root@ide-staging-01:~# uname -a
Linux ide-staging-01 3.19.0-23-generic #24~14.04.1-Ubuntu SMP Wed Jul 8 11:15:13 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@ide-staging-01:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.2 LTS
Release:    14.04
Codename:   trusty
root@ide-staging-01:~# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from docker import Client
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/docker/__init__.py", line 20, in <module>
    from .client import Client, AutoVersionClient, from_env # flake8: noqa
  File "/usr/local/lib/python2.7/dist-packages/docker/client.py", line 25, in <module>
    from . import api
  File "/usr/local/lib/python2.7/dist-packages/docker/api/__init__.py", line 2, in <module>
    from .build import BuildApiMixin
  File "/usr/local/lib/python2.7/dist-packages/docker/api/build.py", line 9, in <module>
    from .. import utils
  File "/usr/local/lib/python2.7/dist-packages/docker/utils/__init__.py", line 1, in <module>
    from .utils import (
  File "/usr/local/lib/python2.7/dist-packages/docker/utils/utils.py", line 33, in <module>
    from .. import tls
  File "/usr/local/lib/python2.7/dist-packages/docker/tls.py", line 5, in <module>
    from .ssladapter import ssladapter
  File "/usr/local/lib/python2.7/dist-packages/docker/ssladapter/__init__.py", line 1, in <module>
    from .ssladapter import SSLAdapter # flake8: noqa
  File "/usr/local/lib/python2.7/dist-packages/docker/ssladapter/ssladapter.py", line 22, in <module>
    urllib3.connection.match_hostname = match_hostname
AttributeError: 'module' object has no attribute 'connection'
>>>
kinbug

Most helpful comment

I solved this issue with the following:
sudo pip install --upgrade pip
Ubuntu/Debian's default version of pip will alter sys.path and thus force requests and urllib3 down to older versions. Docker compose uses pip on startup to detect if old versions are installed.

This was actually on Bash on Ubuntu for Windows.

All 29 comments

I need more information. Please follow the instructions for reporting issues

@shin- I have updated environment information.

This could be a very old version of urllib3. Share the version please:

$ pip freeze | grep -e urllib3 -e requests

It could also be interesting to see if urllib3 (and requests) comes from distribution or from PyPI.

root@ide-staging-01:~# pip freeze | grep -e urllib3 -e requests
requests==2.7.0
urllib3==1.7.1
root@ide-staging-01:~# dpkg -l | grep urllib3
ii  python-urllib3                        1.7.1-1ubuntu3                        all          HTTP library with thread-safe connection pooling for Python
ii  python-urllib3-whl                    1.7.1-1ubuntu3                        all          HTTP library with thread-safe connection pooling
root@ide-staging-01:~# dpkg -l | grep request
ii  iputils-arping                        3:20121221-4ubuntu1.1                 amd64        Tool to send ICMP echo requests to an ARP address
ii  python-requests                       2.2.1-1ubuntu0.3                      all          elegant and simple HTTP library for Python, built for human beings
ii  python-requests-whl                   2.2.1-1ubuntu0.3                      all          elegant and simple HTTP library for Python, built for human beings

Noticed the same issue on my side.
I was setting up some Docker containers through ansible.
System information:
OS/Distrib: Linux / Ubuntu14.04
Python version: 2.7
docker-py version: 1.8.1
docker version: 1.11.1

After careful investigation, I noticed a few things:

  1. requests module was present, and looked like it matched the expectations of docker-py
  2. No module could be imported from within docker-py
  3. docker-py thus fallbacked onto urllib3 rather than requests.packages.urllib3
  4. urllib3 somehow did not provide the same API than my version of requests.packages.urllib3

Digging more into it, I found out the following:

  1. https://github.com/kennethreitz/requests/blob/master/requests/packages/__init__.py#L14 : See the quote: requests.packages.urllib3 is not urllib3.
    I think they attempt to imply that both should not be seen as equivalent. As such, the following lines were surprising to me, but from reading the comments on this PR, this may be intended for a recent urllib3 version: https://github.com/docker/docker-py/blob/master/docker/ssladapter/ssladapter.py#L13. Does it deserve an improvement ?
  2. Changing the ordering of the paths in PYTHONPATH seemed to fix the issue or resolution of requests.packages.urllib3, thus fixing docker-py
    It looked like on the machine having this issue, some manual install/removal somehow changed the order of the paths in PYTHONPATH, so that /usr/local/lib/python2.7/dist-packages was coming later in the list than /usr/lib/python2.7/dist-packages.
    The effect was that importing something within requests suche as requests.packages yielded a no such module error.
    Inverting the order of the two paths previously quoted seemed to solve the issue (we did that globally through the file /etc/environment)

I do not know how our machine came to this situation, and I couldn't find out more than this in the time I spent investigating.

I believe that happens because requests in Ubuntu can't vendor in urllib3 (see here), so we had to construct a fallback that uses the version installed on the system instead.

Because the version of urllib3 that's available on Trusty is so old, docker-py's attempt at monkey-patching it is unfortunately failing. Installing a more recent version of urllib3 using pip may solve the issue. (pip install -U urllib3==1.14)

I'll try and figure out if there's a better solution for us.

@shin- Thanks for being reactive on this subject.
I do not know which urllib3 version I was using, but it is probably older, as you pointed out.
That being said, I'll take anything that does not involve tweaking my environment variables to get requests.packages.urllib3 to be importable when it is present in the system's directories.

I'm getting this error with:
docker 1.11.1
docker-py 1.8.1
urllib3 1.15.1

Works with:
docker 1.11.1
docker-py 1.7.2
urllib3 1.15.1

@mixmatch What about docker-py==1.8.1 + urllib3==1.14 ?

Also, what OS/version are you using?

this fixed it for me
$ cat /etc/environment .bash_profile .profile | grep python2.7 PYTHONPATH="/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages"
with:
$ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 17.3 Rosa
Release: 17.3
Codename: rosa

thx @DavidPineauScality

This is still an issue, and practically breaks all our ansible playbooks on ubuntu 14.04

@oryband Did you try pip-installing urllib3==1.14?

@shin- calling pip install urllib3==1.14 doesn't have any effect because the current OS installation overrides it.

$ pip freeze
...
urllib3==1.7.1

$ pip install urllib3==1.14
Downloading/unpacking urllib3==1.14
  Downloading urllib3-1.14-py2.py3-none-any.whl (89kB): 89kB downloaded
Installing collected packages: urllib3
  Found existing installation: urllib3 1.7.1
    Not uninstalling urllib3 at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed urllib3
Cleaning up...

$ pip freeze
...
urllib3==1.7.1

@shin- ok, i rm /usr/lib/python2.7/dist-packages/urllib3-1.7.1.egg-info and then successfully installed urllib3==1.14 and validated this via pip freeze, but the problem still persists.

I can confirm setting $PYTHONPATH like mentioned above fixes the problem.

Only updating python-urllib3 package to 1.15.1 on Ubuntu 14.04 helped me to overcome this issue.
docker-py==1.9.0
docker version 1.11.2 api version 1.23

Had the same problem with

  • ubuntu 14.04
  • docker 1.12.1
  • docker-py==1.8.1
  • urllib3==1.7.1

Solved it by

pip install urllib3==1.14

and

export PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages

this resolved my similar issue
apt-get install python-openssl

I solved this issue with the following:
sudo pip install --upgrade pip
Ubuntu/Debian's default version of pip will alter sys.path and thus force requests and urllib3 down to older versions. Docker compose uses pip on startup to detect if old versions are installed.

This was actually on Bash on Ubuntu for Windows.

Also solved by :

pip install urllib3==1.14
export PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages

I solved by the code : sudo pip install --upgrade pip and
then use pip install -U urllib3.

Had this problem with docker-compose, but the solution proposed by @a568283992 worked for me:
sudo pip install --upgrade pip && pip install -U urllib3

solved using sudo easy_install -U pip && sudo pip install docker-py

I encountered this issue, too (see https://github.com/docker/docker-py/issues/1526). In my case, I'm using salt to configure the instance and something about how it attempts to install docker-py at https://github.com/saltstack-formulas/docker-formula/blob/0bff590b7bdd9568140c9693ca6e8b6fb4731408/docker/init.sls#L133-L155 results in both requests and urllib3 being out of date, so you must explicitly install the right version of dependencies manually or disable docker-py installation.

Should not there be a requirement for the correct urllib3 version to be installed when pip install docker-compose?

This problem still occurs (using Vagrant imageubuntu/trusty64):

$ docker version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:06:06 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:06:06 2017
 OS/Arch:      linux/amd64
 Experimental: false
$ python --version
Python 2.7.6
$ pip freeze | grep -E 'urllib3|requests|docker'
docker==2.3.0
docker-compose==1.13.0
docker-pycreds==0.2.1
dockerpty==0.4.1
requests==2.2.1
urllib3==1.7.1

@darkn3rd Agreed. Sounds like this issue could be closed with a single line of code in setup.py.

After installation of docker-compose on Ubuntu 16.04, I got this attribute error on any interaction with docker-compose.
Fix for me was to update both urllib3 and the request module:

sudo -H pip install -U urllib3
sudo -H pip install -U requests

Only updating the urllib3 still left the 'unbundling' problem in the request module, which includes an older version of urllib3

I had the same issue.
It was caused by ${site-packages}/requests/packages/urllib3 folder
remained after upgrading from requests<2

# install the most resent requests version
pip install --upgrade requests
# check if we have the problem
python -c "import requests.packages.urllib3 as u; u.connection"
# print rm command
python -c "import sys
from os.path import exists, join, abspath

for p in sys.path:
    module_path = abspath(join(p, 'requests'))
    if exists(join(module_path, 'packages', 'urllib3')):
        print('rm -rI \'%s\'' % module_path)
"
Was this page helpful?
0 / 5 - 0 ratings