[Solved] CentOS7 python import docker Error: No module named selectors

Environmental information

ansible to call the docker module of remote centos7 , you need to install the package docker = = 4.4.4 .

"msg": "Failed to import the required Python library (Docker SDK for Python: docker above 5.0.0 (Python >= 3.6) or docker before 5.0.0 (Python 2.7) or docker-py (Python 2.6)) on k8s-node-56-166.by.com's Python /usr/bin/python. 
Please read the module documentation and install it in the appropriate location. 
If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` (Python >= 3.6) or `pip install docker==4.4.4` (Python 2.7) or `pip install docker-py` (Python 2.6). The error was: No module named requests.exceptions"

After using pip to install, the following error is still reported:

>>> import docker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/docker/__init__.py", line 2, in <module>
    from .api import APIClient
  File "/usr/lib/python2.7/site-packages/docker/api/__init__.py", line 2, in <module>
    from .client import APIClient
  File "/usr/lib/python2.7/site-packages/docker/api/client.py", line 8, in <module>
    import websocket
  File "/usr/lib/python2.7/site-packages/websocket/__init__.py", line 20, in <module>
    from ._app import WebSocketApp
  File "/usr/lib/python2.7/site-packages/websocket/_app.py", line 23, in <module>
    import selectors
ImportError: No module named selectors
>>> qa
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

This line of error code comes from

File "/usr/lib/python2.7/site-packages/websocket/_app.py", line 23, in <module>
    import selectors

This file is from the websocket client package.

Observe the detailed installation information as follows:

[[email protected] ~]# pip install docker==4.4.4
Collecting docker==4.4.4
  Downloading https://files.pythonhosted.org/packages/c4/22/410313ad554477e87ec406d38d85f810e61ddb0d2fc44e64994857476de9/docker-4.4.4-py2.py3-none-any.whl (147kB)
    100% |████████████████████████████████| 153kB 636kB/s 
Requirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker==4.4.4)
Requirement already satisfied (use --upgrade to upgrade): ipaddress>=1.0.16; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker==4.4.4)
Requirement already satisfied (use --upgrade to upgrade): backports.ssl-match-hostname>=3.5; python_version < "3.5" in /usr/lib/python2.7/site-packages (from docker==4.4.4)
Collecting requests!=2.18.0,>=2.14.2 (from docker==4.4.4)
  Downloading https://files.pythonhosted.org/packages/92/96/144f70b972a9c0eabbd4391ef93ccd49d0f2747f4f6a2a2738e99e5adc65/requests-2.26.0-py2.py3-none-any.whl (62kB)
    100% |████████████████████████████████| 71kB 827kB/s 
Collecting websocket-client>=0.32.0 (from docker==4.4.4)
  Downloading https://files.pythonhosted.org/packages/55/44/030ea47390896c8d6dc9995c8e9a4c5df3a161cd45416d88119036c73eda/websocket_client-1.2.1-py2.py3-none-any.whl (52kB)
    100% |████████████████████████████████| 61kB 815kB/s 
Collecting certifi>=2017.4.17 (from requests!=2.18.0,>=2.14.2->docker==4.4.4)
  Downloading https://files.pythonhosted.org/packages/05/1b/0a0dece0e8aa492a6ec9e4ad2fe366b511558cdc73fd3abc82ba7348e875/certifi-2021.5.30-py2.py3-none-any.whl (145kB)
    100% |████████████████████████████████| 153kB 740kB/s 
Collecting urllib3<1.27,>=1.21.1 (from requests!=2.18.0,>=2.14.2->docker==4.4.4)
  Downloading https://files.pythonhosted.org/packages/5f/64/43575537846896abac0b15c3e5ac678d787a4021e906703f1766bfb8ea11/urllib3-1.26.6-py2.py3-none-any.whl (138kB)
    100% |████████████████████████████████| 143kB 4.1MB/s 
Collecting idna<3,>=2.5; python_version < "3" (from requests!=2.18.0,>=2.14.2->docker==4.4.4)
  Downloading https://files.pythonhosted.org/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 732kB/s 
Collecting chardet<5,>=3.0.2; python_version < "3" (from requests!=2.18.0,>=2.14.2->docker==4.4.4)
  Downloading https://files.pythonhosted.org/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl (178kB)
    100% |████████████████████████████████| 184kB 610kB/s 
Installing collected packages: certifi, urllib3, idna, chardet, requests, websocket-client, docker
  Found existing installation: chardet 2.2.1
    Uninstalling chardet-2.2.1:
      Successfully uninstalled chardet-2.2.1
Successfully installed certifi-2021.5.30 chardet-4.0.0 docker-4.4.4 idna-2.10 requests-2.26.0 urllib3-1.26.6 websocket-client-1.2.1

docker==4.4.4withwebsocket-client>=0.32.0It is OK
websocket-clientversion is1.2.1

[[email protected] ~]# pip show websocket-client
---
Metadata-Version: 2.1
Name: websocket-client
Version: 1.2.1
Summary: WebSocket client for Python with low level API options
Home-page: https://github.com/websocket-client/websocket-client.git
Author: liris
Author-email: [email protected]
Installer: pip
License: Apache-2.0
Location: /usr/lib/python2.7/site-packages
...

Solution:

Reduce the version of websocket client to 0.32.0 .

[[email protected] ~]# pip install websocket-client==0.32.0

The verification is successful, and the ansible call is also successful.

[[email protected] ~]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
>>> exit


Read More: