pythondockerdocker-composedockerfilerequirements.txt

Docker: ModuleNotFoundError: No module named 'tabulate'


I am creating a python program that will allow me to have a dashboard to check on stocks and different financial indicators pulled from yahoo finance. "

I tried to import a module named "tabulate" to allow me to format the information nicely into a table. But when I added it to the requirements file it doesn't work, I even tried running it straight in the docker-compose file but it still says "ModuleNotFoundError: No module named 'tabulate'

!!Python File (this is what makes the error)!!

import yfinance as yf
from tabulate import tabulate
import pandas as pd

!!Docker File!!

FROM python:3.11-slim

WORKDIR /app

COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY ./src ./src


CMD ["python3", "src/main.py" ]

!!docker-compose.yaml!!

services:
  app:
    build: .
    command: python3 src/main.py
    volumes:
      - .:/app
    tty: true
    stdin_open: true

!!requirements.txt!!

yfinance
tabulate
pandas

I tried to import a module named "tabulate" to allow me to format the information nicely into a table. But when I added it to the requirements file it doesn't work, I even tried running it straight in the docker-compose file but it still says "ModuleNotFoundError: No module named 'tabulate'

!!"docker build ." output!!

[+] Building 1.2s (10/10) FINISHED                         docker:desktop-linux
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 242B                                       0.0s
 => [internal] load metadata for docker.io/library/python:3.11-slim        1.1s
 => [1/5] FROM docker.io/library/python:3.11-slim@sha256:8f64a67710f3d981  0.0s
 => [internal] load build context                                          0.0s
 => => transferring context: 3.94kB                                        0.0s
 => CACHED [2/5] WORKDIR /app                                              0.0s
 => CACHED [3/5] COPY ./requirements.txt ./                                0.0s
 => CACHED [4/5] RUN pip install --no-cache-dir -r requirements.txt        0.0s
 => CACHED [5/5] COPY ./src ./src                                          0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:db3805b9c6284cc3fa65db601b3a5c51553f2523f0fa9  0.0s

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/qubwln0smtsm1nqr2nchwlwnk

What's Next?
  1. Sign in to your Docker account → docker login
  2. View a summary of image vulnerabilities and recommendations → docker scout quickview

!!docker build . -t test1 output!!

[+] Building 1.1s (10/10) FINISHED                                             docker:desktop-linux
 => [internal] load build definition from Dockerfile                                           0.0s
 => => transferring dockerfile: 242B                                                           0.0s
 => [internal] load .dockerignore                                                              0.0s
 => => transferring context: 2B                                                                0.0s
 => [internal] load metadata for docker.io/library/python:3.11-slim                            1.0s
 => [internal] load build context                                                              0.0s
 => => transferring context: 293B                                                              0.0s
 => [1/5] FROM docker.io/library/python:3.11-slim@sha256:8f64a67710f3d981cf3008d6f9f1dbe61acc  0.0s
 => CACHED [2/5] WORKDIR /app                                                                  0.0s
 => CACHED [3/5] COPY ./requirements.txt ./                                                    0.0s
 => CACHED [4/5] RUN pip install --no-cache-dir -r requirements.txt                            0.0s
 => CACHED [5/5] COPY ./src ./src                                                              0.0s
 => exporting to image                                                                         0.0s
 => => exporting layers                                                                        0.0s
 => => writing image sha256:db3805b9c6284cc3fa65db601b3a5c51553f2523f0fa90bf4fd488d3af06cd86   0.0s
 => => naming to docker.io/library/test1                                                       0.0s

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/xsr0m6u083ltp0yd3lry79j9y

What's Next?
  1. Sign in to your Docker account → docker login
  2. View a summary of image vulnerabilities and recommendations → docker scout quickview

!!Output for docker build --no-cache .!!

[+] Building 15.6s (10/10) FINISHED                                            docker:desktop-linux
 => [internal] load build definition from Dockerfile                                           0.0s
 => => transferring dockerfile: 242B                                                           0.0s
 => [internal] load .dockerignore                                                              0.0s
 => => transferring context: 2B                                                                0.0s
 => [internal] load metadata for docker.io/library/python:3.11-slim                            0.3s
 => [1/5] FROM docker.io/library/python:3.11-slim@sha256:8f64a67710f3d981cf3008d6f9f1dbe61acc  0.0s
 => [internal] load build context                                                              0.0s
 => => transferring context: 293B                                                              0.0s
 => CACHED [2/5] WORKDIR /app                                                                  0.0s
 => [3/5] COPY ./requirements.txt ./                                                           0.0s
 => [4/5] RUN pip install --no-cache-dir -r requirements.txt                                  14.7s
 => [5/5] COPY ./src ./src                                                                     0.0s 
 => exporting to image                                                                         0.5s 
 => => exporting layers                                                                        0.5s 
 => => writing image sha256:9e1b379dba0593ebe97344053b3314557a7f22037f3f3981d4a652f17ce3eb79   0.0s 
                                                                                                    
View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/x2bwqvnfu70pfrq8kpgrbch01

Solution

  • From your output it appears you are using a cached version of the image and it does not include your modifications to requirements.txt

    => CACHED [2/5] WORKDIR /app                                              0.0s
     => CACHED [3/5] COPY ./requirements.txt ./                                0.0s
     => CACHED [4/5] RUN pip install --no-cache-dir -r requirements.txt        0.0s
     => CACHED [5/5] COPY ./src ./src         
    

    You can either remove the docker image and then rebuild or use docker build with the --no-cache option or have docker-compose directly build without cache for you.

    To find a list of your image names execute:

    docker image ls
    

    And then remove the image with:

    docker image rm image-name
    # Rebuild the image 
    

    To use the --no-cache option use the the following:

    docker build --no-cache . -t image-name
    

    Have docker-compose directly build image for you without cache:

    docker-compose down
    docker-compose build --no-cache
    docker-compose up