0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python FastAPIの環境構築

Last updated at Posted at 2024-08-13

Python FastAPIの環境構築

なんで記載するのか?

仕事でpythonを利用する可能性が出てきたので、備忘録を兼ねて記載。

0. Project Settingup

0-1. Install Dependency

pyenvを利用するための環境構築

% brew install pyenv
% make ${PROJECT_PATH}
% cd ${PRJECT_PATH}
% pyenv local 3.12.5
% pyenv version
pyenv version
3.12.5 (set by ${PROJECT_PATH}/.python-version)

pipxの設定

macの際にx-code-toolsが必要になる。

% pwd
${PROJECT_PATH}
% brew install pipx
% pipx ensurepath

poetryのinstall

% pipx install poetry

1. Setting Up

% pwd
${PROJECT_PATH}
% poetry init -n
% poetry config virtualenvs.prefer-active-python true
% tree -a .
.
├── .python-version
└── pyproject.toml

% poetry add fastapi uvicorn
% poetry install
..
├── .python-version
├── poetry.lock
└── pyproject.toml
pyproject.toml
[tool.poetry]
name = "app"
version = "0.1.0"
description = ""
authors = [""]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
fastapi = "^0.112.0"
uvicorn = "^0.30.5"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

2. write sample app

% pwd
${PROJECT_PATH}
% mkdir app
% touch main.py
app/main.py
from fastapi import FastAPI


@app.get("/")
def index():
    return {"Hello": "World"}

2-1. 起動コマンドのためにMakefileをつくる

% pwd
${PROJECT_PATH}
% touch Makefile
POETRY=poetry
PACKAGE=app

dev:
	${UVICORN} $(PACKAGE).main:app --reload
 

2-2. 起動する

% make dev
oetry run uvicorn app.main:app --reload
INFO:     Will watch for changes in these directories: [${PROJECT_PATH}]
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [25050] using StatReload
INFO:     Started server process [25054]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?