无法使用木星笔记本图像在Docker容器中安装模块

发布于 2025-02-09 14:54:04 字数 1772 浏览 0 评论 0原文

我有以下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

和以下项目结构:

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

内部data_exploration.ipynb笔记本,我想用命令导入模块src作为测试。但是,尽管我在Dockerfile中输入了运行PIP install -e。但是,当我对容器进行操作时,在Work目录中,并运行pip install -e。错误消失了。我尝试在dockerfile的末尾添加cmd pip install -e。而没有成功。我还阅读了 this 帖子(更具体地说,更具体地说,要添加行运行APT-GET更新运行apt-get install -y-no-no-install-recommends,但没有成功。

我的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.*",
)

有人知道如何使modulenotfounderror走开吗?

I have got the following Docker file

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. However, despite me having typed RUN pip install -e . in the Dockerfile, I get the error ModuleNotFoundError: No module named 'src'. When I do to my container though, inside the work directory, and run pip install -e . the error goes away. I tried adding CMD pip install -e . at the end of the Dockerfile without success. 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.

My setup.py file looks like this:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-02-16 14:54:04

将代码安装在Work文件夹下,然后您不需要安装任何内容,或者确实需要Dockerfile,除非您拥有其他依赖关系

在主机上,为笔记本创建文件夹...

笔记本/src/plots.py

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

在Compose中,将其安装在Working Directory /home/jovyan/jovyan/work

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

上环境,并在工作文件夹中创建笔记本,然后导入模块。

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

如果您不使用Docker或Jupyter,这与您在主机上所做的工作流程相同,因此这两个都不是问题所在。

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.

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文