Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python project can't find modules inside of Docker

Tags:

python

docker

I'm trying to run a python project inside of docker using the following Dockerfile for machine learning purposes:

FROM python:3

RUN apt-get update \
    && apt-get install -yq --no-install-recommends \
    python3 \
    python3-pip
RUN pip3 install --upgrade pip==9.0.3 \
    && pip3 install setuptools

# for flask web server
EXPOSE 8081

# set working directory
ADD . /app
WORKDIR /app

# install required libraries
COPY requirements.txt ./
RUN pip3 install -r requirements.txt

# This is the runtime command for the container
CMD python3 app.py

And here is my requirements file:

flask
scikit-learn[alldeps]
pandas
textblob
numpy
matplotlib[alldeps]

But when i try to import textblob and pandas, i get a no module named 'X' error in my docker cmd. enter image description here

|    warnings.warn(msg, category=FutureWarning)
| Traceback (most recent call last):
|    File "app/app.py", line 12, in <module>
|      from textblob import Textblob
| ImportError: No module named 'textblob'
exited with code 1

Folder structure

machinelearning:
    backend:
        app.py
        Dockerfile
        requirements.txt
    frontend:
        ... (frontend works fine.)
    docker-compose.yml

Does anyone know the solution to this problem? (I'm fairly new to Docker, so I might just be missing something crucial.)

like image 535
Alexander Bruun Avatar asked Aug 22 '26 21:08

Alexander Bruun


1 Answers

This worked for me

FROM python:3

RUN apt-get update
RUN apt-get install -y --no-install-recommends

# for flask web server
EXPOSE 8081

# set working directory
WORKDIR /app

# install required libraries
COPY requirements.txt .
RUN pip install -r requirements.txt

# copy source code into working directory
COPY . /app

# This is the runtime command for the container
CMD python3 app.py
like image 111
Lambo Avatar answered Aug 24 '26 09:08

Lambo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!