Mathieu Dhondt
Mathieu Dhondt

Reputation: 8914

Cannot import lxml in python:3.4 docker container

Most probably due to resource constraints, I am unable to pip install (and hence build) lxml within a python:3.4 based docker container.

I'm therefore installing the package instead. 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 am unable to import lxml, even when simply running Python in my container, like so:

***@*****:/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 resolved?

Upvotes: 3

Views: 2620

Answers (1)

larsks
larsks

Reputation: 311238

Your problem is that you are using the python image, which installs Python from source into /usr/local. You are then trying to install the lxml module using the system package manager, apt-get. This installs lxml where it will be visible to the system Python in /usr/bin/, but not to the source-installed Python in /usr/local. You have a few choices:

  • Don't try to mix the Python package manager (pip) with the system package manager. Just pip install everything. I've tried, and much to my surprise I can successfully pip install lxml (I wasn't expecting all the build dependencies to be in place but apparently they are).

  • Don't bother using the python image. Just start with your favorite distribution (Fedora, Ubuntu, whatever) and use the system package manager:

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

    With recent Ubuntu images this will get you Python 3.4.2 (NB: called python3), which is only a minor revision away from the 3.4.3 installed on the python image.

Upvotes: 2

Related Questions