Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'app' #225

Open
jmandt opened this issue Dec 17, 2022 · 2 comments
Open

ModuleNotFoundError: No module named 'app' #225

jmandt opened this issue Dec 17, 2022 · 2 comments

Comments

@jmandt
Copy link

jmandt commented Dec 17, 2022

Hi, the error as mentioned in the title is thrown when running the container. Below I tried to describe my setup in detail.

I have setup everything exactly as described in the readme. Therefore, this is how my Dockerfile looks like.

FROM python:3.10 as requirements-stage

WORKDIR /tmp

RUN pip install poetry

COPY ./pyproject.toml ./poetry.lock* /tmp/

RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.10

COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY ./app /app

my directory structure is as follows.

app
├── main.py
├── api
│  └── __init__.py
|  └── user.py
└── tests
     └── __init__.py
pyproject.toml
Dockerfile

then, in the app/main.py I am trying to import user from the api-directory by doing:
from app.api import user

When running the container, the following error is thrown.
ModuleNotFoundError: No module named 'app'

This is probably because app is not in the PYTHONPATH. I have tried to add it to the Dockerfile, but this did not help.
I want to keep the structure and the way of importing it as is as it is also needed by my test setup.

How can I fix this? The problem has probably a very trivial solution, but I am currently stuck.

Thanks in advance

@JaeHyunL
Copy link

I've just had a similar error.
I solved it this way.

My source code directory is app.
DockerFile "COPY"
"./app" "/app" 이라
formed by a structure called /app/app
You change Dockerfile.

COPY . /app

OR

COPY ./app /app_code
WORKDIR /app_code

@tedsecretsource
Copy link

@JaeHyunL nailed it! Thank you, whoever you are!

I am using the Heroku container stack. My Dockerfile looked like this:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11-slim

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY ./app /app

I changed it to this (appended /app on the COPY line):

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11-slim

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY ./app /app/app

Also, FWIW, my heroku.yml file looks like this:

build:
  docker:
    web: Dockerfile
run:
  web: sh /start.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants