Fastapi: Docker ModuleNotFoundError: No module named 'uvloop'

Created on 15 Oct 2020  路  2Comments  路  Source: tiangolo/fastapi

Hi

I have a custom uvicorn-gunicorn image taken from your slim example

FROM python:3.8.5-slim-buster

RUN apt-get update && apt-get install -y build-essential gcc libc-dev make python3-lxml  \
    && pip install --no-cache-dir uvicorn gunicorn

COPY ./start.sh /start.sh
RUN chmod +x /start.sh

COPY ./gunicorn_conf.py /gunicorn_conf.py

COPY ./start-reload.sh /start-reload.sh
RUN chmod +x /start-reload.sh

COPY ./app /app
WORKDIR /app/

ENV PYTHONPATH=/app

EXPOSE 80

# Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Gunicorn with Uvicorn
CMD ["/start.sh"]

It's identical, all except that I need lxml. This builds fine, but when I use it to run my fastapi project like you do here:

FROM path-to-my/uvicorn-gunicorn:python3.8

LABEL maintainer="Sebastian Ramirez <[email protected]>"

RUN pip install --no-cache-dir fastapi

COPY ./app /app

When I run the container though I am getting this output

Checking for script in /app/prestart.sh
Running script /app/prestart.sh
Running inside /app/prestart.sh, you could add migrations to this file, e.g.:

#! /usr/bin/env bash

# Let the DB start
sleep 10;
# Run migrations
alembic upgrade head

[2020-10-15 13:13:32 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-10-15 13:13:32 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
[2020-10-15 13:13:32 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-10-15 13:13:32 +0000] [7] [INFO] Booting worker with pid: 7
[2020-10-15 13:13:32 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/workers.py", line 60, in init_process
    self.config.setup_event_loop()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/config.py", line 333, in setup_event_loop
    loop_setup = import_from_string(LOOP_SETUPS[self.loop])
  File "/usr/local/lib/python3.8/site-packages/uvicorn/importer.py", line 23, in import_from_string
    raise exc from None
  File "/usr/local/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.8/site-packages/uvicorn/loops/uvloop.py", line 3, in <module>
    import uvloop
ModuleNotFoundError: No module named 'uvloop'
{"loglevel": "info", "workers": 4, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout": 120, "keepalive": 5, "errorlog": "-", "accesslog": "-", "workers_per_core": 1.0, "use_max_workers": null, "host": "0.0.0.0", "port": "80"}
[2020-10-15 13:13:32 +0000] [7] [INFO] Worker exiting (pid: 7)
[2020-10-15 13:13:32 +0000] [1] [INFO] Shutting down: Master
[2020-10-15 13:13:32 +0000] [1] [INFO] Reason: Worker failed to boot.
{"loglevel": "info", "workers": 4, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout": 120, "keepalive": 5, "errorlog": "-", "accesslog": "-", "workers_per_core": 1.0, "use_max_workers": null, "host": "0.0.0.0", "port": "80"}

So I'm not exactly sure what's wrong here because I don't see uvloops explicitly installed in your builds. I just now added uvloops and it went on and errored that

 File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 7, in <module>
    import httptools
ModuleNotFoundError: No module named 'httptools'

So it seems that python isn't installing dependencies

First check

  • [x] I added a very descriptive title to this issue.
  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] I searched the FastAPI documentation, with the integrated search.
  • [x] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [x] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [x] I already checked if it is not related to FastAPI but to Pydantic.
  • [x] I already checked if it is not related to FastAPI but to Swagger UI.
  • [x] I already checked if it is not related to FastAPI but to ReDoc.
  • [x] After submitting this, I commit to one of:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

question

Most helpful comment

From https://github.com/encode/uvicorn/blob/master/CHANGELOG.md (starting from 0.12.0): "Uvicorn no longer ships extra dependencies uvloop, websockets and httptools as default. To install these dependencies use uvicorn[standard]."
In your pyproject.toml , replace uvicorn dependency with uvicorn = {extras = ["standard"], version = "^0.12.0"}

All 2 comments

From https://github.com/encode/uvicorn/blob/master/CHANGELOG.md (starting from 0.12.0): "Uvicorn no longer ships extra dependencies uvloop, websockets and httptools as default. To install these dependencies use uvicorn[standard]."
In your pyproject.toml , replace uvicorn dependency with uvicorn = {extras = ["standard"], version = "^0.12.0"}

Thanks @ffvpor for the help

Was this page helpful?
0 / 5 - 0 ratings