Python project can't find modules inside of Docker

Question:

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.)

Asked By: Alexander Bruun

||

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
Answered By: Lambo

On Linux, whenever you have the message:

ImportError: No module named 'XYZ'`

check whether you can install it or its dependencies with apt-get, example here that does not work for textblob, though, but may help with other modules:

(This does not work; it is an example what often helps, but not here)

# Python3:
sudo apt-get install python3-textblob
# Python2:
sudo apt-get install python-textblob

See Python error "ImportError: No module named" or How to solve cannot import name ‘abort’ from ‘werkzeug.exceptions’ error while importing Flask.

In the case of "textblob", this does not work for python2.7, and I did not test it on python3 but it will likely not work either, but in such cases, one should give it a try.

And just guessing is not needed, search through the apt cache with a RegEx. Then:

$ apt-cache search "python.*blob"
libapache-directory-jdbm-java - ApacheDS JDBM Implementation
python-git-doc - Python library to interact with Git repositories - docs
python-swagger-spec-validator-doc - Validation of Swagger specifications (Documentation)
python3-azure-storage - Microsoft Azure Storage Library for Python 3.x
python3-bdsf - Python Blob Detection and Source Finder
python3-binwalk - Python3 library for analyzing binary blobs and executable code
python3-discogs-client - Python module to access the Discogs API
python3-git - Python library to interact with Git repositories - Python 3.x
python3-mnemonic - Implementation of Bitcoin BIP-0039 (Python 3)
python3-nosehtmloutput - plugin to produce test results in html - Python 3.x
python3-swagger-spec-validator - Validation of Swagger specifications (Python3 version)
python3-types-toml - Typing stubs for toml
python3-types-typed-ast - Typing stubs for typed-ast

would be needed to check whether there are some python packages for "textblob" out there.

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.