Search code examples
pythonpython-3.xdockerdocker-image

No module named Error when running docker image


I am working on a python project and I need to create a docker image for testing. In my project I have a main.py file that has the statement from <module> import *. here represents a folder in which I put all the other files of my project. The dockerfile is as follows:

FROM continuumio/miniconda3

COPY parcellation_env.yml .
RUN conda env create -f parcellation_env.yml

SHELL ["conda", "run", "-n", "parcellation_env", "/bin/bash", "-c"]

COPY main.py .
ADD module/ .

ENTRYPOINT ["conda", "run", "-n", "parcellation_env", "python", "main.py", "-C", "white_matter_parcellation/config/config_point_autoencoder.yaml", "--use_splits", "-y"]

When creating the image, everything works fine, but when I try to run the image in a container I get a No module named "module" error, signaling that something went wrong.

I'm new to docker and this is my first time using it, does anyone know what went wrong?

Note: the error occurs both if I put ADD and COPY for module/, I already tried changing that


EDIT:

The folder structure of my project is currently the following

|--main.py
|--module
|--parcellation_env.yml
|--__init__.py
|----folder1
|------folder_1_files
|------folder_1_init
|----folder2
|------folder_2_files
|------folder_3...
|------folder_2_init
...

Solution

  • There are some few issues I could see ideally, if you are having module errors , it usually points to the fact that Python can not find specific module in question.

    I think the best is to restructure how you are placing files via your docker file for python to use.

    I have made few modifications and I hope it helps if there is anything I suspect might be not having a WORKDIR.

    Make sure your structure is like this

    ├── Dockerfile
    ├── main.py
    ├── module or src/
    │   ├── __init__.py
    │   ├── files.py
    ├── parcellation_env.yml
    

    here is a snippet of your docker file with some few mods.

    
    FROM continuumio/miniconda3
    
    # Copy the environment file and create the conda environment
    COPY parcellation_env.yml .
    RUN conda env create -f parcellation_env.yml
    
    # Set the shell to use the conda environment
    SHELL ["conda", "run", "-n", "parcellation_env", "/bin/bash", "-c"]
    
    # Your working directory should include src/*/*/ etc. tests and the likes
    WORKDIR /app
    
    # copy the main script and the module directory to /app
    COPY main.py /app/
    ADD module/ /app/module/
    
    ENV PYTHONPATH=/app
    
    
    ENTRYPOINT ["conda", "run", "-n", "parcellation_env", "python", "main.py", "-C", "white_matter_parcellation/config/config_point_autoencoder.yaml", "--use_splits", "-y"]