user994165
user994165

Reputation: 9502

Docker Flask ModuleNotFoundError: No module named 'flask'

I'm getting the following error when executing

docker-compose up --build

web_1  | Traceback (most recent call last):
web_1  |   File "glm-plotter.py", line 4, in <module>
web_1  |     from flask import Flask, render_template, request, session
web_1  | ModuleNotFoundError: No module named 'flask'
glm-plotter_web_1 exited with code 1

I tried changing "Flask" to "flask" in the requirements.txt

Dockerfile

FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential

COPY requirements.txt /
RUN pip3 install --trusted-host pypi.python.org -r /requirements.txt

ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD ["python3", "glm-plotter.py"]

docker-compose.yml

version: "3"
services:
  web:
    volumes:
      - ~/.aws:/root/.aws
    build: .
    ports:
      - "5000:5000"

requirements.txt

click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
numpy==1.11.1
pandas==0.18.1
python-dateutil==2.5.3
pytz==2016.4
six==1.10.0
Werkzeug==0.11.10

glm-plotter.py

from flask import Flask, render_template, request, session
import os, json
import GLMparser
...

Upvotes: 4

Views: 13098

Answers (2)

Jordi
Jordi

Reputation: 551

If you use miniconda image you have to create a new environment and activate it prior to installing the packages and run the program in your docker file. Something like:

FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential

COPY requirements.txt /
RUN ["conda", "create", "-n", "myenv", "python=3.4"]

RUN /bin/bash -c "source activate myenv  && pip install --trusted-host pypi.python.org -r /requirements.txt"

ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD /bin/bash -c "source activate myenv && python glm-plotter.py"

Upvotes: 2

Klaus D.
Klaus D.

Reputation: 14369

I created a Docker and it compiles fine. You might want to adapt it to your personal needs and add the last few lines from your Dockerfile above:

FROM library/python:3.6-stretch

COPY requirements.txt /
RUN pip install -r /requirements.txt

Please note that in the library/python images no explicit version number for python or pip is required since there is only one installed.

Upvotes: 3

Related Questions