월 16,060원
5개월 할부 시다른 수강생들이 자주 물어보는 질문이 궁금하신가요?
- 해결됨실전! FastAPI 입문
No module named 'sqlalchemy'
sqlalchemy module이 설치되어있음에도 불구하고 위와 같이 서버를 실행시키면 no module named 'sqlalchemy' 라는 오류가 뜨는데 어떻게 해야하나요..+추가로 위의 문제를 해결하기 위해서 시도했던 방법으로,아래와 같이 flask_sqlalchemy를 통해서 sqlalchemy를 불러왔지만from flask_sqlalchemy import SQLAlchemy마찬가지로 flask_sqlalchemy에 대해서도 찾을 수 없다는 오류가 뜹니다..
- 해결됨실전! FastAPI 입문
섹션 테스트 코드 오류..
버전 문제가 아닌 거 같아서 문의 드립니다.. 구글링 해도 안 나와서 질문 드립니다..ㅠㅠ
- 해결됨실전! FastAPI 입문
PyCharm 내 Python Console 사용 질문
WSL2를 사용해 강의를 따라가고 있습니다.파이참에서 파이썬 콘솔을 실행하면 위와 같이 재시작, 정지, 한 줄씩 실행 버튼이 잘 보이는 경우가 있고, 아래처럼 보이지 않는 경우가 있습니다. 이런 경우는 뭐가 문제일까요?또한, 파이썬 콘솔에서 코드를 작성한 이후 엔터를 입력해도 다음 줄로 넘어가기만 하고 실행이 되지 않습니다.한 줄씩 실행하려면 alt + shift + e 이렇게 입력해야 하는데, 이렇게 코드를 실행하게 되면 여러 줄은 또 실행되지 않습니다... 뭐가 문제인지 알려주시면 감사하겠습니다.
- 해결됨실전! FastAPI 입문
Redis 연결중 import
Redis import 가 안되네요 이런경우 어떻게 처리 하는지요? Docker 에 redis container 는 설치되었어요
- 해결됨실전! FastAPI 입문
섹션3 테스트코드 오류발생..
테스트코드 작성시 경고가 뜨는데, 어떻게 해결해야 할까요?
- 미해결실전! FastAPI 입문
실수도 DB todo테이블을 삭제했습니다
contents내용을 수정하려다가 꼬여서 drop명령어로 새로 테이블을 작성하고 할려했는데 테이블을 그대로 만들었는데 uvicorn main:app 이 명령어를 수행하면 500internal error가 뜹니다. ㅠㅠ 어디서부터 뭘 수정해야 할지 모르겠는데 다시 파이썬프로젝트와 DB를 연결할수 있게 할수 있는방법 있을까요??
- 해결됨실전! FastAPI 입문
섹션3 testcode get_단일조회 api
여기까지 코드 잘 따라서 작성하였고 #200부분을 잘 작동합니다. #404 부분코드를 삭제하고 실행하면 잘 돌아가는데404부분 코드를 작성하니까 48번째 줄에서 자꾸 오류가 뜨는데 해결방법이 있을까요??
- 해결됨실전! FastAPI 입문
BackgroundTask 인자값 전달
안녕하세요 강사님 강의 재밌게 잘보고 있습니다BackGround task 강의에서 궁금한게 생겨서 질문 남깁니다. 아래는 verify_otp_handler 의 선언부인데def verify_otp_handler( request : VerifyOTPRequest, background_tasks : BackGroundTasks ... ) backgound_tasks 라는 객체는 DI로 받지도 않고 있는데 어떻게 함수 내부에서 사용될 수 있는지가 궁금합니다. request 라는 객체는 request 헤더부에서 넘어온 값이 프레임워크에 의해서 VerifyOTPRequest로 전처리되거라 어디서부터 왔는지 그 출처를 알겠지만, backgound_tasks는 DI도 되지 않았고 헤더부에서 넘어온 것도 아닌데 어떻게 생성된건지 궁금합니다.
- 미해결실전! FastAPI 입문
docker mysql 연동
docker에서는 mysql에 정상적으로 접속이 되는데python console을 통해서 연결을 시도하면 sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2059, "Authentication plugin 'b'auth_gssapi_client'' not configured")다음 에러가 나옵니다.select host, user from mysql.user where user= 'root';쿼리를 통해서 user 정보를 확인해보면 정상적으로 user가 셋팅 되어있습니다.
- 해결됨실전! FastAPI 입문
pytest는 어떻게 모킹함수를 인지하는지
안녕하세요 강사님강의 정말 재밌게 보고 있습니다.수강 도중 궁금한 내용이 생겨 질문 남깁니다.mocker.patch("main.get_todo_by_todo_id", return_value=Todo(id=1,contents="todo",is_done=True))여기서 인자값으로 문자열 main.get_todo_by_todo_id 를 전달했는데, pytest는 어떻게 저 함수를 모킹대상으로 인지하나요? 개념적으로 설명해주시면 감사합니다~!! response = client.patch("/todos/1", json={"is_done":False})해당 api 를 호출하고 실행하는 과정에서main_get_todo_by_todo_id 함수를 호출할 때 어떻게 모킹처리가 되는지 궁금합니다.
- 해결됨실전! FastAPI 입문
타입힌트 질문
안녕하세요 강사님강의 듣는 도중 조금 궁금한 내용이 생겨서 질문드립니다. 코드에서 아래와 같이 타입힌트를 사용하는 경우가 있는데def done(self) -> "Todo": self>is done = True ~ 왜 Todo에 "" 를 감싸주시는걸까요??그냥 Todo 라고 타입힌트를 줘도될거 같은데 이유가 궁금해서요
- 해결됨실전! FastAPI 입문
코드 질문
안녕하세요.파이썬 2.7만 주로 쓰다 오랜만에 쓰니 좀 헷갈리는게 있어 질문드립니다.encoding: str = "UTF-8"service 작성하시면서 설명해주신 코드인데,encoding = "UTF - 8" 이렇게 써도 encoding 변수에 뭐가 들어가는지 바로 확인이 되는데 굳이 : str 과 타입을 명시할 이유가 있나요 ? 이렇게 작성하였을 때 왜 이렇게 작성하고 어떠한 장점이 있는지 궁금합니다.비슷한 질문으로 def save_user(self, user: User) -> User:다음과 같이 반환 타입을 적어줘야 하는 것도 설명 부탁드립니다.작성하면서도 이걸 굳이 적어줘야 하는지가 궁금해서 질문드립니다.
- 해결됨실전! FastAPI 입문
router testcode 문의
router test code 에서 test_main.py 파일에 api.todo라고 main을 바꾸어 줬는데 import를 따로 안 시켜줘도 되나요 ?main.py는 from api import todo로 import 시키는데 test_main.py는 안 그래도 되는지 궁금합니다.
- 해결됨실전! FastAPI 입문
docker 설치 에러
안녕하세요.윈도우 os 사용중이고 docker desktop 설치 시 이후데몬이 활성화가 안되어 질문드립니다.데몬 활성화를 위해서는 가상화 지원이 되어야 하는데, bios 세팅 및 os 가상화 설정을 모두 하여서 작업관리자에서 가상화:사용 이라고 까지 표기가 되어도 하이퍼바이저 지원이 안된다는 에러가 나옵니다.wsl 설치 또한 가상화가 지원이 안된다는 에러와 함께 설치가 안되어 구글링 이후 문의 드립니다.
- 해결됨실전! FastAPI 입문
uvicorn 실행 에러
안녕하세요.학습 도중 재부팅하여 다시 uvicorn 실행하려고 하니ERROR: Error loading ASGI app. Could not import module "main". 다음과 같은 메세지가 나오면서 fast api 앱이 실행이 안되는데 이유가 궁금합니다.
- 해결됨실전! FastAPI 입문
src 디렉토리를 기본으로 설정하는 방
from main import app이 아닌from src.main import app을 하게 되면 에러가 발생합니다. src. 을 생략할 수 있도록 하는 방법이 없을까요
- 해결됨실전! FastAPI 입문
session.scalar(select(1)) 에러 발생 질문입니다.
에러명은 다음과 같습니다. 도커 설정은 이상이 없는데 어디서 문제가 생긴걸까요pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([WinError 10061] 대상 컴퓨터에서 연결을 거부했으므로 연결하지 못했습니다)")The above exception was the direct cause of the following exception:
- 해결됨실전! FastAPI 입문
DATABASE_URL 상수변수 질문
도커를 이용해 컨테이너를 실행시킬 때 root 사용자에 대해서 todos라는 비밀번호를 갖도록 설정을 해주었는데, DATABASE_URL = "mysql+pymysql://root:todos@127.0.0.1:3306/todos"상수 변수를 이용해 데이터베이스 연결 시도 시,(1045, "Access denied for user 'root'@'localhost' (using password: YES)") 이런 에러가 발생합니다.비밀번호를 설정하였는데도 왜 이렇게 발생하는 건가요?
- 해결됨실전! FastAPI 입문
(실습) ORM 적용 - HTTP Response 처리
from typing import List from fastapi import FastAPI, Body, HTTPException, Depends from pydantic import BaseModel from sqlalchemy.orm import Session from database.orm import ToDo from database.connection import get_db from database.repository import get_todos from schema.response import ListToDoResponse, ToDoSchema app = FastAPI() @app.get("/") def heath_check_handler(): return {"ping": "pong"} todo_data = { 1: { "id": 1, "contents": "실전! FastAPI 섹션 0 수강", "is_done": True, }, 2: { "id": 2, "contents": "실전! FastAPI 섹션 1 수강", "is_done": False, }, 3: { "id": 3, "contents": "실전! FastAPI 섹션 2 수강", "is_done": False, }, } @app.get("/todos", status_code=200) def get_todos_handler( order: str | None = None, session: Session = Depends(get_db), ) -> ListToDoResponse: todos: List[ToDo] = get_todos(session=session) if order and order == "DESC": return ListToDoResponse( todos=[ToDoSchema.from_orm(todo) for todo in todos[::-1]] ) return ListToDoResponse( todos=[ToDoSchema.from_orm(todo) for todo in todos] ) @app.get("/todos/{todo_id}", status_code=200) def get_todo_handler(todo_id: int): todo = todo_data.get(todo_id) if todo: return todo raise HTTPException(status_code=404, detail="ToDo Not Found") class CreateToDoRequest(BaseModel): id: int contents: str is_done: bool @app.post("/todos", status_code=201) def create_todo_handler(request: CreateToDoRequest): todo_data[request.id] = request.dict() return todo_data[request.id] @app.patch("/todos/{todo_id}", status_code=200) def update_todo_handler( todo_id: int, is_done: bool = Body(..., embed=True) ): todo = todo_data.get(todo_id) if todo: todo["is_done"] = is_done return todo raise HTTPException(status_code=404, detail="ToDo Not Found") @app.delete("/todos/{todo_id}") def delete_todo_handler(todo_id: int): todo = todo_data.pop(todo_id, None) if todo: return raise HTTPException(status_code=404, detail="ToDo Not Found") from typing import List from pydantic import BaseModel class ToDoSchema(BaseModel): id: int contents: str is_done: bool class Config: orm_mode = True class ListToDoResponse(BaseModel): todos: List[ToDoSchema] from typing import List from sqlalchemy import select from sqlalchemy.orm import Session from database.orm import ToDo def get_todos(session: Session) -> List[ToDo]: return list(session.scalars(select(ToDo))) from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker DATABASE_URL = "mysql+pymysql://root:todos@127.0.0.1:3306/todos" engine = create_engine(DATABASE_URL, echo=True) SessionFactory = sessionmaker(autocommit=False, autoflush=False, bind=engine) def get_db(): session = SessionFactory() try: yield session finally: session.close() from sqlalchemy import Boolean, Column, Integer, String from sqlalchemy.orm import declarative_base Base = declarative_base() class ToDo(Base): __tablename__ = 'todo' id = Column(Integer, primary_key=True, index=True) contents = Column(String(256), nullable=False) is_done = Column(Boolean, nullable=False) def __repr__(self): return f"ToDo(id={self.id}, contents={self.contents}, is_done={self.is_done})" 에러가 납니다. 파이썬 콘솔 <input>:1: PydanticDeprecatedSince20: The from_orm method is deprecated; from schema.response import ToDoSchemafrom datagbase.orm import ToDoTraceback (most recent call last):File "C:\Program Files\JetBrains\PyCharm 2023.3.2\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcodecoro = func()^^^^^^File "<input>", line 1, in <module>File "C:\Program Files\JetBrains\PyCharm 2023.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_importmodule = self._system_import(name, args, *kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ModuleNotFoundError: No module named 'datagbase'from database.orm import ToDotodo = ToDo(id=100, contents="test", is_done=True)ToDoSchema.from_orm(todo)<input>:1: PydanticDeprecatedSince20: The from_orm method is deprecated; set model_config['from_attributes']=True and use model_validate instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/Traceback (most recent call last):File "C:\Program Files\JetBrains\PyCharm 2023.3.2\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcodecoro = func()^^^^^^File "<input>", line 1, in <module>File "C:\Users\manag\pyProject\todos\Lib\site-packages\typing_extensions.py", line 2499, in wrapperreturn arg(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\pydantic\main.py", line 1126, in from_ormraise PydanticUserError(pydantic.errors.PydanticUserError: You must set the config attribute from_attributes=True to use from_orm http://localhost:8000/docs#/default/get_todos_handler_todos_get접속시 터미널INFO: 127.0.0.1:56312 - "GET /docs HTTP/1.1" 200 OKINFO: 127.0.0.1:56312 - "GET /openapi.json HTTP/1.1" 200 OK2024-01-23 16:06:15,108 INFO sqlalchemy.engine.Engine BEGIN (implicit)2024-01-23 16:06:15,109 INFO sqlalchemy.engine.Engine SELECT todo.id, todo.contents, todo.is_doneFROM todo2024-01-23 16:06:15,109 INFO sqlalchemy.engine.Engine [cached since 826.5s ago] {}2024-01-23 16:06:15,113 INFO sqlalchemy.engine.Engine ROLLBACKINFO: 127.0.0.1:56312 - "GET /todos HTTP/1.1" 500 Internal Server ErrorERROR: Exception in ASGI applicationTraceback (most recent call last):File "C:\Users\manag\pyProject\todos\Lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 404, in run_asgiresult = await app( # type: ignore[func-returns-value]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in callreturn await self.app(scope, receive, send)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\fastapi\applications.py", line 1054, in callawait super().__call__(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\applications.py", line 123, in callawait self.middleware_stack(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\middleware\errors.py", line 186, in callraise excFile "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\middleware\errors.py", line 164, in callawait self.app(scope, receive, _send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in callawait wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_appraise excFile "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_appawait app(scope, receive, sender)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\routing.py", line 762, in callawait self.middleware_stack(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\routing.py", line 782, in appawait route.handle(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\routing.py", line 297, in handleawait self.app(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\routing.py", line 77, in appawait wrap_app_handling_exceptions(app, request)(scope, receive, send)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_appraise excFile "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_appawait app(scope, receive, sender)File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\routing.py", line 72, in appresponse = await func(request)^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\fastapi\routing.py", line 299, in appraise eFile "C:\Users\manag\pyProject\todos\Lib\site-packages\fastapi\routing.py", line 294, in appraw_response = await run_endpoint_function(^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\fastapi\routing.py", line 193, in run_endpoint_functionreturn await run_in_threadpool(dependant.call, **values)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\starlette\concurrency.py", line 40, in run_in_threadpoolreturn await anyio.to_thread.run_sync(func, *args)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\anyio\to_thread.py", line 56, in run_syncreturn await get_async_backend().run_sync_in_worker_thread(^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\anyio\_backends\_asyncio.py", line 2134, in run_sync_in_worker_threadreturn await future^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\anyio\_backends\_asyncio.py", line 851, in runresult = context.run(func, *args)^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\src\main.py", line 48, in get_todos_handlertodos=[ToDoSchema.from_orm(todo) for todo in todos]^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\typing_extensions.py", line 2499, in wrapperreturn arg(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^File "C:\Users\manag\pyProject\todos\Lib\site-packages\pydantic\main.py", line 1126, in from_ormraise PydanticUserError(pydantic.errors.PydanticUserError: You must set the config attribute from_attributes=True to use from_orm
- 해결됨실전! FastAPI 입문
test 코드 질문
강의 너무 잘보고 있습니다test 코드 실습 중에 생긴 문제인데요def test_update_todo(client, mocker): mocker.patch.object(ToDoRepository, "get_todo_by_todo_id", return_value=ToDo(id=1, contents="todo", is_done=True)) mocker.patch.object(ToDoRepository, "update_todo", return_value=ToDo(id=1, contents="todo", is_done=True)) body = { "is_done": False } response = client.patch("/todos/1", json=body) assert response.status_code == 200 assert response.json() == {"id": 1, "contents": "todo", "is_done": True} # 404 mocker.patch.object(ToDoRepository, "get_todo_by_todo_id", return_value=None) response = client.patch("/todos/1", json=body) assert response.status_code == 404 assert response.json() == {"detail": "ToDo Not Found"}이런식으로 test 코드를 작성한 다음에 리팩토링 과정에서 잘못선택되서 todo_id가 빠진 상태로 코드를 올렸습니다@router.patch("/{todo_id}", status_code=200) def update_todo_handler( todo_id: int, is_done: bool = Body(..., embed=True), todo_repo: ToDoRepository = Depends()): todo: ToDo | None = todo_repo.get_todo_by_todo_id() # 이 부분 todo_id를 빼버림 if todo: todo.done() if is_done else todo.undone() todo: ToDo = todo_repo.update_todo(todo) return ToDoSchema.from_orm(todo) else: raise HTTPException(status_code=404, detail="ToDo Not Found")이게 제가 잘못 작성한 코드고 todo.get_todo_by_todo_id 함수에 parameter를 안넣고 pytest를 실행해도 mocker.patch로 지정된 값이 넘어오게 되니까 에러가 나지 않더라구요 실행하면 당연히 500 internal error가 발생하는데 혹시 이런 경우까지 방지하도록 test 코드를 바꾼다면 어떻게 바꿔야할까요?