uwsgi-nginx-flask-docker: Python packages still not available to app after pip install

I have additional requirements I need to install for my app. Here’s my Dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.6

ENV STATIC_INDEX 1

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

COPY . /app

Even after rebuilding, the app still can’t import my packages. When I bring up the docker container, I get this in the output:

Traceback (most recent call last):
  File "./main.py", line 4, in <module>
    from flask_cors import CORS

The build step does show it installing the package.

Building web
Step 1/5 : FROM tiangolo/uwsgi-nginx-flask:python3.6
 ---> 590e17342131
Step 2/5 : ENV STATIC_INDEX 1
 ---> Using cache
 ---> be704d970645
Step 3/5 : COPY requirements.txt /app/
 ---> Using cache
 ---> 9a2d06e8c82e
Step 4/5 : RUN pip install -r /app/requirements.txt
 ---> Running in 85413afc84ee
Collecting certifi==2017.7.27.1 (from -r /app/requirements.txt (line 1))
  Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB)
Collecting chardet==3.0.4 (from -r /app/requirements.txt (line 2))
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
Requirement already satisfied: click==6.7 in /usr/local/lib/python3.6/site-packages (from -r /app/requirements.txt (line 3))
Requirement already satisfied: Flask==0.12.2 in /usr/local/lib/python3.6/site-packages (from -r /app/requirements.txt (line 4))
Collecting Flask-Cors==3.0.3 (from -r /app/requirements.txt (line 5))
  Downloading Flask_Cors-3.0.3-py2.py3-none-any.whl
Collecting idna==2.6 (from -r /app/requirements.txt (line 6))
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
Requirement already satisfied: itsdangerous==0.24 in /usr/local/lib/python3.6/site-packages (from -r /app/requirements.txt (line 7))
Requirement already satisfied: Jinja2==2.9.6 in /usr/local/lib/python3.6/site-packages (from -r /app/requirements.txt (line 8))
Requirement already satisfied: MarkupSafe==1.0 in /usr/local/lib/python3.6/site-packages (from -r /app/requirements.txt (line 9))
Collecting requests==2.18.4 (from -r /app/requirements.txt (line 10))
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting six==1.11.0 (from -r /app/requirements.txt (line 11))
  Downloading six-1.11.0-py2.py3-none-any.whl
Collecting urllib3==1.22 (from -r /app/requirements.txt (line 12))
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
Requirement already satisfied: Werkzeug==0.12.2 in /usr/local/lib/python3.6/site-packages (from -r /app/requirements.txt (line 13))
Installing collected packages: certifi, chardet, six, Flask-Cors, idna, urllib3, requests
Successfully installed Flask-Cors-3.0.3 certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 six-1.11.0 urllib3-1.22
 ---> 0437821ea780
Removing intermediate container 85413afc84ee
Step 5/5 : COPY . /app
 ---> aed7dfd1c4a8
Removing intermediate container debc2010ff97
Successfully built aed7dfd1c4a8
Successfully tagged app_web:latest

Can anyone see what I’m doing wrong here? Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Hi! I’ve encountered the same issue.

The problem is with the PYTHONPATH env variable which is being set here - it’s being overridden so the python interpreter does look for site packages just in /app.

While it isn’t resolved, use one of the following workarounds:

  1. Unset the PYTHONPATH env variable
FROM tiangolo/uwsgi-nginx-flask:python3.6
RUN  unset -v PYTHONPATH
  1. Install pip requirements into the /app directory
RUN  pip install -r /app/requirements.txt -t /app

Can you paste a bigger chunk of your Traceback? It is not very clear what is the error it is complaining about. It’s probably in that line, but we don’t know the specific error in that line.


Also, check this section:

Step 3/5 : COPY requirements.txt /app/
 ---> Using cache

It is not copying the updated requirements.txt because it thinks it already has it from a previous build, so it’s using the Docker cache. It’s possible that it doesn’t have the latest version of requirements.txt (after you added some more dependencies). You might want to build it with --no-cache: docker build --no-cache . or docker-compose build --no-cache.

That will execute all the steps, without using any cached version of your app. It will take longer but will ensure that you have the very latest version of everything.


There’s also an alternative that allows you to be able to use the Docker cache and have faster build times. Which in turn, allows you to iterate fast while developing (this is what I normally do).

The trick is to declare your Python packages in your Dockerfile, instead of the separated requirements.txt.

You can add a line like:

RUN pip install Flask-Cors

And then, when you see that you will also need requests, you can add another line:

RUN pip install Flask-Cors
RUN pip install requests

That will make your Docker build faster because it will use the previous layer that installed Flask-Cors, and install requests on top of it, instead of having to install all the packages one by one every time.

When you are done, you should compact them, to make your Docker image thinner, like:

RUN pip install Flask-Cors requests

But while developing it helps to be able to build the whole thing faster.

This method is less “Pythonic” (not using the requirements.txt) but is more “Dockeronic” (or however they say it), by taking full advantage of Docker, the cache, etc.