2

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?

JohnK
  • 45
  • 7
  • Do you need to "install" `src`? From the notebook, you should already be able to do `from src import plots`. Without seeing your setup file, we don't know what the real module name should be – OneCricketeer Jun 22 '22 at 16:00
  • I have now included the `setup.py` file in the question. – JohnK Jun 22 '22 at 17:22
  • Okay, so looks like you should be trying to import `almond_analysis`, not `src` since that is the name of the module, but like I said, you should also be able to import directly from the folder/package without "installing" anything else. – OneCricketeer Jun 22 '22 at 20:04
  • I cannot import directly from the folder/package right after I have typed `docker compose up` . If I go into `data_exploration.ipynb` and type `import src as test` or `from src import plots`, I get the error `ModuleNotFoundError: No module named 'src'`. However, if I go inside my container (inside the `work/` directory) and type `pip install -e .`, both these errors go away. I want not to have to go in the container and type `pip install -e .`, but instead for this command to be run in the `RUN pip install --upgrade pip && pip install -e .` line in my Dockerfile. – JohnK Jun 23 '22 at 11:16
  • The module you are installing is called `almond_analysis`, not `src`, though. – OneCricketeer Jun 23 '22 at 17:05

1 Answers1

1

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245