question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python packages still not available to app after pip install

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
klausweisscommented, Apr 11, 2018

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
2reactions
tiangolocommented, Sep 26, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pip installs packages successfully, but executables not found ...
I can successfully install packages and import them inside my python environment and python scripts. However any executable associated with a package that...
Read more >
Installing Packages - Python Packaging User Guide - Python.org
Installing Packages ¶. This section covers the basics of how to install Python packages. It's important to note that the term “package” in...
Read more >
Every package installed with pip is not found - Ask Ubuntu
When you logged in, before you installed your first pip packages, this directory did not exist so it is not currently in your...
Read more >
I used pip to install a library, but when I import it it says Module ...
First try using pip3 inatead of pip maybe pip install libraries in different dictionary ... And if it still does not work first...
Read more >
Why Can't Python Find My Modules? - Real Python
This is caused by the fact that the version of Python you're running your script with is not configured to search for modules...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found