Docker-py: No module named docker

Created on 9 Nov 2015  路  19Comments  路  Source: docker/docker-py

I've installed docker-py using sudo pip install docker-py. Here are my details:

$ pip freeze | grep docker-py && python --version && docker version
docker-py==1.5.0
Python 2.7.6
Client:
 Version:      1.8.3
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   f4bf5c7
 Built:        Mon Oct 12 05:37:18 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.3
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   f4bf5c7
 Built:        Mon Oct 12 05:37:18 UTC 2015
 OS/Arch:      linux/amd64

When I go into python and try to import it, I get:

>>> from docker import Client
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named docker

However, if instead I clone this repo and run the setup.py install, I can import docker. I guess there's something different between the version in the PyPI and github?

Here's the version information for the docker-py installation that works:

$ pip freeze | grep docker-py && python --version && docker version
docker-py==1.6.0-dev
Python 2.7.6
Client:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.2
 Git commit:   76d6bc9
 Built:        Tue Nov  3 17:43:42 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.2
 Git commit:   76d6bc9
 Built:        Tue Nov  3 17:43:42 UTC 2015
 OS/Arch:      linux/amd64

Here's my python version information:

$ python --version
Python 2.7.6

Most helpful comment

I installed _pip_ from the github repo (this is because I believe the debian package python-pip to be too out of date). Then used pip to install docker-py, i.e.:

git clone https://github.com/pypa/pip
cd pip
sudo python setup.py install
sudo pip install docker-py

All 19 comments

Could you post the output of

python --version

Done, see original question

To me it looks like that you install docker-py for python 2 and you are trying to import it in python 3. Or your $PYTHONPATH is misconfigured.

EDIT: does any of these work?

$ python2 -c "import docker"
$ python3 -c "import docker"

I uninstalled docker-py 1.6.0-dev, then re-installed the version from pip, and now it can import. I'm wondering if there's some setup step in the latest version that wasn't in pip. I'm going to make a copy of my machine and try again.

Alright, reverting back to the original state:

$ python2 -c "import docker"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named docker
$ python3 -c "import docker"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'docker'

The $PYTHONPATH variable is the same both when docker-py installed from pip as installed from this repo - it's empty.

Well, taking a shot in the dark!
Could you post the output of:

python2.7 -c 'import sys; print(sys.path)'

And in any one of those paths, does the directory docker exist?

In my case, it is located at /usr/lib/python2.7/site-packages/docker/

@containscafeine good point

Looks like I have two installations of docker:

for d in sys.path:
    if os.path.exists(os.path.join(d, "docker")):
        print("docker is at %r" % d)
   ....:         
docker is at '/usr/bin'
docker is at '/home/user/.local/lib/python2.7/site-packages'
docker is at '/usr/lib/python2.7/site-packages'

To try to describe exactly my steps:

  • download and install vmware player
  • create a fresh ubuntu14.04 machine
  • install python-setuptools
  • clone pip from the github repo and install
  • sudo pip install docker-py

Then, running the command

python2.7 -c 'import sys; print(sys.path)'

yields:

['', '/usr/local/lib/python2.7/dist-packages/pip-8.0.0.dev0-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-i386-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

No docker.

When you say you installed form the github repo, did you do so using pip (pip install .) or setup.py (python setup.py install)? Or something else?

Thanks for helping us look into this!

I installed _pip_ from the github repo (this is because I believe the debian package python-pip to be too out of date). Then used pip to install docker-py, i.e.:

git clone https://github.com/pypa/pip
cd pip
sudo python setup.py install
sudo pip install docker-py

I tried reproducing with the following Dockerfile but didn't encounter the import error. Could it be that you cloned a bugged version of the pip repo? What commit is the HEAD at?

FROM ubuntu:14.04
WORKDIR /var/opt
RUN apt-get install -y python-setuptools git
RUN git clone https://github.com/pypa/pip
RUN cd pip && python setup.py install
RUN pip install docker-py
ENTRYPOINT python -c 'import docker; print("docker-py operational.")'

Hmm. It is a pip vs. pip-dev issue. Repeating the same steps above with python-pip works.

So, in summary:

  • latest docker-py from github works
  • python-pip + version of docker-py in PyPI works
  • pip from github + version of docker-py in PyPI does not work.

I'm raising this issue with pip.

I'm closing this now, but feel free to reopen if there are any new developments.

What was missing for me is, that it seems to require the module on the target host; this doesn't come clear from the error message. a local require docker thus worked flawless for me, fixing this required to run the pip command on the target host.

@dothebart What do you mean by "target host"?

The host that I try to administrate using ansible. Sorry I missed that this is not primarily about ansible.

Ansible loads this module on the source host, which I run it on with the configs, (which works) but also on the host it wants to provision. Thus - revalidate that before you run the docker commands you have this in your .yml-file:

- pip name=docker-py

as it installs this module on the target host.

hi ,

i have same problem and i have install "pip install docker " and did "python2.7 -c "import docker""
but still i am getting below error when i run module ..

fatal: [192.168.122.102]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on pcs_dockr1's Python /usr/bin/python. Please read module documentation and install 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 or pip install docker-py (Python 2.6). The error was: No module named docker"}

#

ansible 2.9.7
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

#

To me it looks like that you install docker-py for python 2 and you are trying to import it in python 3. Or your $PYTHONPATH is misconfigured.

EDIT: does any of these work?

$ python2 -c "import docker"
$ python3 -c "import docker"

Importing docker with pip3 solve my problem. Thanks for that.

Was this page helpful?
0 / 5 - 0 ratings