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 envから受けとるだけのスクリプト 勉強用メモ

0
Posted at
exm/config/.env
   main.py
   config.py

config.py

import os
from dataclasses import dataclass
from dotenv import load_dotenv

load_dotenv("config/bank.env")

@dataclass(frozen=True)
class Config:)
    api_port: int = int(os.getenv("API_PORT", 8080))
    # 他の設定項目を呼び出せる。
    exm: str = ...
config = Config()

他のファイルは

from config import config

で呼出し。

main.pyで

# print(f"ポート{config.api_port}で起動")
if __name__ == "__main__":
    uvicorn.run(
        "api:app",
        host="127.0.0.1",
        port=config.api_port,#設定を組み込み
        reload=True
    )

スクリプト内のハードコードを排除し、管理が楽になる。
使い方もわかりやすく直感的。
積極的にコードに使って生きたい

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?