Can't import lxml in python: 3.4 docker container

Most likely due to resource constraints, I am unable to install (and therefore build) lxml in a python based docker container: 3.4.

So I install the package. My Dockerfile:

FROM python:3.4
COPY ./ /source
RUN apt-get update
RUN apt-get install -y python3-lxml
RUN pip install -r /source/requirements.txt


Unfortunately I can't import lxml

even when I just run Python in my container, e.g .:

***@*****:/web/source# docker exec -i -t 84c4cbf09321 python
Python 3.4.3 (default, May 26 2015, 19:20:24)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'lxml'


How can this be solved?


source to share

1 answer

Your problem is that you are using an image python

that installs Python from source to /usr/local

. Then you try to install the module lxml

using the system package manager apt-get

. This sets lxml

where it will be visible to the Python system in /usr/bin/

, but not to the Python source in /usr/local

. You have several options:

  • Don't try to mix the Python ( pip

    ) package manager with the system package manager. Just pip install

    everything. I tried, and much to my surprise, I can succeed pip install lxml

    (I didn't expect all the build dependencies to be in place, but apparently they are).

  • Don't use an image python

    . Just start with your favorite distro (Fedora, Ubuntu, whatever) and use the system package manager:

    FROM ubuntu
    RUN apt-get install -y python3-lxml

    With recent Ubuntu images, you get Python 3.4.2 (NB: called python3

    ), which is only a minor version from 3.4.3 installed on the image python




All Articles