JohnK
JohnK

Reputation: 55

Cannot install modules inside Docker container with Jupiter notebook image

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?

Upvotes: 2

Views: 828

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

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.

Upvotes: 1

Related Questions