Molecule: Failed to import docker-py – cannot import name certs

When trying to do a molecule test of an Ansible role on a friend’s jumpbox, I kept getting this error:

thehearn@thatbox99> molecule converge -s docker
<snip>
    TASK [Discover local Docker images] ********************************************
    failed: [localhost] (item=None) => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}
    failed: [localhost] (item=None) => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": false}

Running in “debug” mode gave me a bit more detail, leading to this message:

thehearn@thatbox99> molecule --debug converge -s docker
<snip>
        "msg": "Failed to import docker-py - cannot import name certs. Try `pip install docker-py`"

docker-py is already installed, though:

thehearn@thatbox99> pip freeze | grep docker-py
docker-py==1.10.6
docker-pycreds==0.4.0

Weird. What the heck is going on? Apparently, the “certs” feature is part of the “requests” package. However, that’s installed as well:

thehearn@thatbox99> pip freeze | grep requests
requests==2.22.0
requests-ntlm==1.1.0

Huh. I guess I’ll just remove it, and reinstall it, and see if that does the job.

thehearn@thatbox99> sudo pip uninstall requests
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Pytho
n 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Uninstalling requests-2.18.4:
  Would remove:
    /usr/lib/python2.7/site-packages/requests-2.18.4.dist-info/*
    /usr/lib/python2.7/site-packages/requests/*
  Would not remove (might be manually added):
    /usr/lib/python2.7/site-packages/requests/packages/__init__.py
    /usr/lib/python2.7/site-packages/requests/packages/__init__.pyo
Proceed (y/n)? y
  Successfully uninstalled requests-2.18.4
thehearn@thatbox99> sudo pip install requests
Installing collected packages: requests
  Found existing installation: requests 2.6.0
ERROR: Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

Aha. Looks like maybe we had conflicting versions of “requests” installed at the same time, 2.18.4, and 2.6.0 (from distutils). Okay, let’s see if we can force the reinstall:

thehearn@thatbox99> sudo pip install requests --upgrade --force-reinstall
<snip>
Installing collected packages: urllib3, certifi, chardet, idna, requests
  Found existing installation: urllib3 1.22
    Uninstalling urllib3-1.22:
      Successfully uninstalled urllib3-1.22
  Found existing installation: certifi 2018.4.16
    Uninstalling certifi-2018.4.16:
      Successfully uninstalled certifi-2018.4.16
  Found existing installation: chardet 3.0.4
    Uninstalling chardet-3.0.4:
      Successfully uninstalled chardet-3.0.4
  Found existing installation: idna 2.6
    Uninstalling idna-2.6:
      Successfully uninstalled idna-2.6
  Found existing installation: requests 2.6.0
ERROR: Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

Argh. Okay, maybe we can tell it to ignore the fact that it’s already installed?

thehearn@thatbox99> sudo pip install requests --upgrade --force-reinstall --ignore-installed
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Pytho
n 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting requests
  Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Using cached https://files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl
Collecting certifi>=2017.4.17
  Using cached https://files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2
  Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5
  Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl
Installing collected packages: urllib3, certifi, chardet, idna, requests
Successfully installed certifi-2019.9.11 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.7

That looks better. And now my molecule test is able to spin up the docker instances! Awesome!