简体   繁体   English

无法使用 Jupiter 笔记本映像在 Docker 容器内安装模块

[英]Cannot install modules inside Docker container with Jupiter notebook image

I have got the following Docker file我有以下 Docker 文件

FROM jupyter/scipy-notebook

COPY . ./work

RUN pip install -r ./work/requirements.txt

COPY setup.py /work

RUN pip install --upgrade pip && pip install -e .



COPY src /work/src

WORKDIR /work

and the following project structure:以及以下项目结构:

almond_analysis:
    notebooks:
        data_exploration.ipynb
    src:
       __init__.py
       plots.py
    .gitignore
    docker-compose.yml
    Dockerfile
    README.md
    requirements.txt
    setup.py

Inside the data_exploration.ipynb notebook, I would like to import the module src with the command import src as test .data_exploration.ipynb笔记本中,我想使用命令import src as test导入模块src However, despite me having typed RUN pip install -e .但是,尽管我输入了RUN pip install -e . in the Dockerfile, I get the error ModuleNotFoundError: No module named 'src' .在 Dockerfile 中,我收到错误ModuleNotFoundError: No module named 'src' When I do to my container though, inside the work directory, and run pip install -e .但是,当我对我的容器执行操作时,在work目录中运行pip install -e . the error goes away.错误消失了。 I tried adding CMD pip install -e .我尝试添加CMD pip install -e . at the end of the Dockerfile without success.在 Dockerfile 结束时没有成功。 I read also what was suggested in this post (more specifically to add the lines RUN apt-get update and RUN apt-get install -y --no-install-recommends but without success.我还阅读了这篇文章中的建议(更具体地说,添加了RUN apt-get updateRUN apt-get install -y --no-install-recommends ,但没有成功。

My setup.py file looks like this:我的setup.py文件如下所示:

from setuptools import find_packages, setup
import sys
import os

sys.path.insert(0, os.path.abspath( os.path.dirname(__file__)))


requirements_path="requirements.txt"
with open(requirements_path) as requirements_file:
    requirements = requirements_file.read().splitlines()

setup(
    name="almond_analysis",
    version="0.0.1",
    description = "Almond analysis",
    long_description="Analysing yield with Python."
    author= "George N",
    packages=find_packages(),
    install_requires=requirements,
    classifiers=[
        "Programming Language :: Python :: 3"
    ],
    python_requires =">= 3.0.*",
)

Does someone have an idea on how to make the ModuleNotFoundError go away?有人知道如何使ModuleNotFoundError消失吗?

Mount your code under the work folder, then you don't need to install anything, or really need a Dockerfile unless you have other dependencies .将你的代码挂载到work文件夹下,然后你不需要安装任何东西,或者真的需要一个 Dockerfile,除非你有其他依赖项。

On host, create folder for notebooks...在主机上,为笔记本创建文件夹...

notebooks/src/plots.py

def foobar():
    print('Hello from plots')

In compose, mount it under the working directory /home/jovyan/work在compose中,挂载到工作目录/home/jovyan/work

notebook:
    image: jupyter/scipy-notebook
    ...
    volumes:
      - ./notebooks:/home/jovyan/work

Load up the Jupyter environment, and create a notebook in the work folder, and then import the module.加载 Jupyter 环境,并在 work 文件夹中创建一个 notebook,然后导入模块。

from src import plots
plots.foobar()  # Hello from plots

This is the same workflow you'd do on your host if you weren't using Docker or Jupyter, so neither of those are really the problem.如果您不使用 Docker 或 Jupyter,这与您在主机上执行的工作流程相同,因此这些都不是真正的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM