コード 既にOS等で定義済みの環境変数を.envの内容で上書き実行する場合は、オプションにoverride=Trueを指定する。 from dotenv import load_dotenv from flask import Flask from pymongo import MongoClient load_dotenv(override=True) app = Flask(__name__) mongo = MongoClient(os.getenv('MONGO_URI')) ... app.run()
![Flaskでの簡単な環境変数の使い方](https://cdn-ak-scissors.b.st-hatena.com/image/square/3a78bd90a476e58bf81cbf7693acbb0257f25140/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Fzenn%2Fimage%2Fupload%2Fs--QIyW4kRr--%2Fc_fit%252Cg_north_west%252Cl_text%3Anotosansjp-medium.otf_55%3AFlask%2525E3%252581%2525A7%2525E3%252581%2525AE%2525E7%2525B0%2525A1%2525E5%25258D%252598%2525E3%252581%2525AA%2525E7%252592%2525B0%2525E5%2525A2%252583%2525E5%2525A4%252589%2525E6%252595%2525B0%2525E3%252581%2525AE%2525E4%2525BD%2525BF%2525E3%252581%252584%2525E6%252596%2525B9%252Cw_1010%252Cx_90%252Cy_100%2Fg_south_west%252Cl_text%3Anotosansjp-medium.otf_37%3Asivaquen%252Cx_203%252Cy_121%2Fg_south_west%252Ch_90%252Cl_fetch%3AaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL3plbm4tdXNlci11cGxvYWQvYXZhdGFyL2NiODAzOTlhZWYuanBlZw%3D%3D%252Cr_max%252Cw_90%252Cx_87%252Cy_95%2Fv1627283836%2Fdefault%2Fog-base-w1200-v2.png)