Multiple python files docker no module error

I have a python application where the code I split into multiple files just for ease of use, from the main script I just just normal import statements to the other files e.g. from file2 import X, and running locally it works just fine, but when trying to dockerize the app I get no module error, so the above example will render a

File “file1.py”, line 7, in
from file2 import X
ModuleNotFoundError: No module named file2

Have been googling like crazy for two days but will not get I to work, any suggestion will be greatly appreciated!

Dockerfile:
FROM python:3

RUN apt-get update && apt-get install -qq -y
build-essential libpq-dev --no-install-recommends

ENV INSTALL_PATH /gtg_analytics
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY /web_app $INSTALL_PATH/web_app

CMD gunicorn -b 0.0.0.0:8000 --access-logfile - “web_app.app:app”

docker-compose.yml:
version: “3”

services:
web_app:
build: .
command: >
gunicorn -b 0.0.0.0:8000
–access-logfile -
–reload
“web_app.app:app”
environment:
PYTHONUNBUFFERED: ‘true’
volumes:
- ‘.:/web_app’
ports:
- ‘8000:8000’

Sounds like file2 does not exist or is not in the Python Path.

https://docs.python.org/3/tutorial/modules.html#the-module-search-path

You should run a container from your docker image and bring up a command prompt.
Then go inside and look for file2.

docker container run -ityour-docker-containerbash

1 Like