不能在Python中导入lxml:3.4 docker容器

最有可能是由于资源限制,我无法在基于python:3.4的docker容器中安装(并因此构build)lxml。

因此,我正在安装包。 我的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 

不幸的是,我无法import lxml ,即使只是在我的容器中运行Python,就像这样:

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

这怎么解决?

你的问题是,你正在使用python图像, 从源代码安装Python到/usr/local 。 然后,您正在使用系统软件包pipe理器apt-get来安装lxml模块。 这会安装lxml ,它将在/usr/bin/系统 Python中可见,但不会安装到/usr/local的源代码安装的Python中。 你有几个select:

  • 不要试图将Python包pipe理器( pip )与系统包pipe理器混合在一起。 只需点击pip install一切。 我已经尝试了,而且我很惊讶,我可以成功地pip install lxml (我不期望所有的构build依赖关系到位,但显然他们是)。

  • 不要打扰使用python图像。 从你最喜欢的发行版(Fedora,Ubuntu,任何)开始,并使用系统包pipe理器:

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

    使用最近的Ubuntu映像,这将会得到Python 3.4.2(NB:叫做python3 ),它只是在python映像上安装的3.4.3的一个小版本。