5

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

questionto42
  • 7,175
  • 4
  • 57
  • 90
Alexander Bruun
  • 247
  • 2
  • 10
  • You don't need to explicitly install pip or python. They're included in the image already. I'm also confused as to why you're downgrading PIP. I'd remove the first two RUN commands. Also, what's the output of your Docker build? Could it be related you run a pip version 10 versions old? – Mies van der Lippe Apr 30 '20 at 08:20
  • I remove the first 2 runs, when trying to import the previously mentioned modules, python exits with code 1 due to it not being able to find the modules mentioned in the requirements file. (So same result.) – Alexander Bruun Apr 30 '20 at 08:22
  • I've just run it as is, and even with redundant python installed and downgraded pip, as mentioned by @MiesvanderLippe, it still works fine and imports modules. Probably it's path related problem. – Alexey Kuzminich Apr 30 '20 at 08:37
  • Shouw us either structure of your app (why are you coping requirements.txt when you copied entire directory to /app) or output of docker build. Also, no images please – Lesiak Apr 30 '20 at 08:42
  • I added the folder structure to the original message. – Alexander Bruun Apr 30 '20 at 08:43
  • Does the problem surface only when running via docker compose or also if you launch via docker run (Strange thing is that you have `File "/app/app.py"` in the error message while you clearly specified workdir to `/app`) How do you run this? – Lesiak Apr 30 '20 at 08:55
  • docker-compose up – Alexander Bruun Apr 30 '20 at 09:01
  • @AlexanderBruun, it's not a docker or pip problem - it just can't import `from textblob import Textblob`, as displayed on the picture above. But all other modules get imported fine. Check your code. – Alexey Kuzminich Apr 30 '20 at 09:14
  • It is a docker problem, i used the exact same python code and it worked fien when creating a brand new docker project. – Alexander Bruun Apr 30 '20 at 09:38

2 Answers2

1

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
Lambo
  • 1,094
  • 11
  • 18
-1

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.

questionto42
  • 7,175
  • 4
  • 57
  • 90