Docker容器中缺少python软件包

时间:2019-06-13 14:18:28

标签: python docker pip docker-compose pipenv

我正在Docker容器中的tensorflow笔记本上工作。我通过在容器的构建阶段安装的pipenv管理其他依赖项。奇怪的是,我明确安装的某些软件包(例如“ certifi”)似乎没有在python环境中显示。

我已经尝试在RUN命令中使用pip安装它,切换了Pipfile的Python版本,并将几乎所有内容添加到PATH和PYTHONPATH env变量中。奇怪的是,如果我登录到该容器并手动执行操作,它就像是一种魅力。

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-06bc95e4104d> in <module>
      6 # data wrangling
      7 import json
----> 8 import spacy
      9 from langdetect import detect
     10 import psycopg2

~usr/local/lib/python3.5/dist-packages/spacy/__init__.py in <module>
     10 from thinc.neural.util import prefer_gpu, require_gpu
     11 
---> 12 from .cli.info import info as cli_info
     13 from .glossary import explain
     14 from .about import __version__

~usr/local/lib/python3.5/dist-packages/spacy/cli/__init__.py in <module>
----> 1 from .download import download  # noqa: F401
      2 from .info import info  # noqa: F401
      3 from .link import link  # noqa: F401
      4 from .package import package  # noqa: F401
      5 from .profile import profile  # noqa: F401

~usr/local/lib/python3.5/dist-packages/spacy/cli/download.py in <module>
      3 
      4 import plac
----> 5 import requests
      6 import os
      7 import subprocess

~usr/local/lib/python3.5/dist-packages/requests/__init__.py in <module>
    110 from .__version__ import __copyright__, __cake__
    111 
--> 112 from . import utils
    113 from . import packages
    114 from .models import Request, Response, PreparedRequest

~usr/local/lib/python3.5/dist-packages/requests/utils.py in <module>
     22 
     23 from .__version__ import __version__
---> 24 from . import certs
     25 # to_native_string is unused here, but imported here for backwards compatibility
     26 from ._internal_utils import to_native_string

~usr/local/lib/python3.5/dist-packages/requests/certs.py in <module>
     13 packaged CA bundle.
     14 """
---> 15 from certifi import where
     16 
     17 if __name__ == '__main__':

ImportError: No module named 'certifi'


有人已经遇到过类似的问题吗?

用于复制的最小设置如下:

docker-compose.yaml:

version: '3.1'
services:

  notebook:
    container_name: Notebook
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5430:8888"
    user: ${CURRENT_UID}

Dockerfile:

FROM tensorflow/tensorflow:latest-py3-jupyter

RUN pip install --user pipenv
ENV PATH="/root/.local/bin:/.local/bin:${PATH}"
ENV PYTHONPATH="/usr/lib/python35.zip:/usr/lib/python3.5:/usr/lib/python3.5/plat-x86_64-linux-gnu:/usr/lib/python3.5/lib-dynload:/.local/lib/python3.5/site-packages:/usr/local/lib/python3.5/dist-packages:/usr/lib/python3/dist-packages:${PYTHONPATH}"

RUN apt-get update
RUN apt-get install -y libsm6 libxext6 libxrender-dev libglib2.0-0 postgresql libpq-dev

# We copy just the requirements.txt first to leverage Docker cache
COPY ./Pipfile ./Pipfile
COPY ./Pipfile.lock ./Pipfile.lock

RUN pipenv install --deploy --system --ignore-pipfile
RUN python -m spacy download xx

RUN pip install --user certifi

Pipfile:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
numpy = "*"
scikit-learn = {extras = ["alldeps"]}
spacy = "*"
langdetect = "*"
ipykernel = "*"
bokeh = "*"
ipywidgets = "*"
plotly = "*"
lapjv = "*"
tensorflow = "*"
"psycopg2" = "*"
gensim = "*"
cmake = "*"
certifi = "*"

[dev-packages]

[requires]
python_version = "3.5"

使用pipenv lock锁定Pipfile,使用docker-compose up --build构建并启动容器,然后登录到该容器,这表明未安装certifi软件包。 帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

从您的dockerfile中,运行spacy后似乎正在安装certifi,而从堆栈跟踪中看似依赖certifi

RUN python -m spacy download xx

RUN pip install --user certifi

在运行依赖脚本的脚本之前,切换这些行的顺序以进行安装。

还要确保所有内容都配置为使用python3,或显式使用python3pip3

答案 1 :(得分:0)

重温死的问题:显然存在一个点冲突无法解决的版本冲突。我转而使用Poetry作为模块管理解决方案,并以此方式规避了issua。