I’m working on a FastAPI project in VSCode and encountering a persistent issue where my imports for FastAPI are not being recognized (Import "fastapi" could not be resolved
).
A problem occurs and after deleting all containers and images, fastapi cannot be imported when entering main.py in the host environment regardless of docker. (Fast API operates in the host environment before docker-compose up --build
)
Additionally, when I try to use pip in the terminal within VSCode, I get a zsh: command not found: pip
error.
When i enter docker compose run web
I got an error
The virtual environment found in /app/.venv seems to be broken.
Recreating virtualenv untitled folder 2 in /app/.venv.
Under are my codes.
Dockerfile
FROM python:3.11-slim
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1
PYTHONUNBUFFERED=1
RUN pip install poetry
RUN poetry config virtualenvs.create false
COPY pyproject.toml poetry.lock* /app/
RUN poetry config --list
RUN poetry install --no-dev --no-interaction --no-ansi
COPY . /app
CMD ["poetry", "run", "uvicorn", "main:app", "--host=0.0.0.0", "--port=8000"]
docker-compose.yml
version: '3.8'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
environment:
- PYTHONUNBUFFERED=1
main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "123"}
-
I have tried resetting the virtual environment and manually setting the Python interpreter in VSCode, but the issue persists.
-
I tried various modifications to dockerfile and dockercompose. but no effect.
-
Changing environment variables in .zshrc and python version of brew, no effect.