ModuleNotFoundError for local python file in Docker

Using Docker I get error for importing local file:

ModuleNotFoundError: No module named 'logger'

Running locally its working using:

uvicorn main:app --reload

Files: enter image description here

Docker file:

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.8
#tiangolo/uvicorn-gunicorn-fastapi:python3.8
#python:3.8-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
# Keeps the same hash for the same query, python use random see to create the hash and we dont want it
ENV PYTHONHASHSEED 0
# Copy local code to the container image.
COPY ./app /app

# Install production dependencies.
RUN pip install fastapi
RUN pip install uvicorn
RUN pip install gunicorn
RUN pip install google-cloud-bigquery
RUN pip install google-cloud-storage
RUN pip install pandas
RUN pip install pyarrow
RUN pip install google-cloud-bigquery-storage
RUN pip install requests
RUN pip install fast-autocomplete
RUN pip install gensim
RUN pip install scipy

EXPOSE 80

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
#CMD ["gunicorn", "-b", "0.0.0.0:80", "app.main:app"]

main.py

 from fastapi import FastAPI
    from typing import Optional
    from pydantic import BaseModel
    import os
    import json
    from logger import Logger
    import uuid
    import logging
    import gzip
    from fast_autocomplete import autocomplete_factory
    from fast_autocomplete import AutoComplete
    import uuid
    from autocomplete import AutoCompleted
    from google_manager import GoogleManager
    from gensim.models.callbacks import CallbackAny2Vec
    from categories_embded import CategoriesEmbded
    from categories_embded import callback
    from google.cloud import bigquery
    import uvicorn
    
    # Keeps the same hash for the same query, python use random see to create the hash and we dont want it
    #os.environ["PYTHONHASHSEED"] = "0"
    #export PYTHONHASHSEED=0
    
    logging.root.setLevel(logging.INFO)
    
    app = FastAPI()

    @app.get("/suggest/{word}")
    def autocomplete(word : str):
      return word

Docker image error:

Traceback (most recent call last):
  File "/usr/local/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 355, in main
    run(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 379, in run
    server.run()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 407, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/main.py", line 414, in serve
    config.load()
  File "/usr/local/lib/python3.8/site-packages/uvicorn/config.py", line 300, in load
    self.loaded_app = import_from_string(self.app)
  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 "./app/main.py", line 6, in <module>
    from logger import Logger
ModuleNotFoundError: No module named 'logger'


Read more here: https://stackoverflow.com/questions/64190272/modulenotfounderror-for-local-python-file-in-docker

Content Attribution

This content was originally published by dasdasd at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: