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.
FROM python:3.8

# Allow statements and log messages to immediately appear in the Knative logs
# Keeps the same hash for the same query, python use random see to create the hash and we dont want it
# 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


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

 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 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
    app = FastAPI()

    def autocomplete(word : str):
      return word

Docker image error:

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

Read more here:

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: