Kinnturo
Kinnturo

Reputation: 141

Python Docker container, Module not found

I have the following Project structure

twoFlasks/
├── app.py
├── common/
│   ├── file1.py        
│   ├── file2.py       
│   └── file3.py    
├── Dockerfile
├── requirements.txt       
├── bot1/   
│   │        
│   ├── routes/
│   │   └── bot1_routes.py
└── bot2/
    └── routes/
        └── bot2_routes.py

And the following Dockerfile:

FROM python:3.8

WORKDIR /project

COPY requirements.txt .
COPY app.py .

RUN pip3 install -r requirements.txt

COPY bot1/ .
COPY bot2/ .
COPY common/ .

EXPOSE 5000 3000

CMD ["python", "./app.py"]

The image is built successfully, but when I got to run a container I get the following error message in the logs from my app.py file

Traceback (most recent call last):
  File "./app.py", line 9, in <module>
    from bot2.routes import bot2_routes
ModuleNotFoundError: No module named 'bot2'

It works locally, but not in the Docker container, and I'm kind of at a loss here. If anyone knows what might be the problem, I'd appreciate the help!

Upvotes: 1

Views: 616

Answers (2)

Kinnturo
Kinnturo

Reputation: 141

So, what fixed my issue was a change in the Dockerfile.

COPY bot1/ ./bot1
COPY bot2/ .bot2
COPY common/ ./common

Instead of just copying the folders to .

Upvotes: 1

Aziz Sonawalla
Aziz Sonawalla

Reputation: 2502

If you're using Python <3.3 then you need an __init__.py file in your package root directories to help Python build the namespace: Python - Module Not Found

Upvotes: 0

Related Questions