LoginSignup
33
32

More than 5 years have passed since last update.

Pythonで環境変数を取得する、なければデフォルト値をセットする

Posted at

概要

pythonを使う時に、環境変数があれば、その値、なければ、デフォルト値をセットしたい

結論

Access environment variables from Python

import os

url = os.getenv("YOUR_URL", "http://qiita.com")
print(url)

os.environ.get

os.environ は、mapのようなので、

docker で試す場合

$ docker run -v $PWD:/work --rm python:2.7.12 python /work/sample.py
http://qiita.com
$ docker run -e YOUR_URL="http://127.0.0.1" -v $PWD:/work --rm python:2.7.12 python /work/sample.py
http://127.0.0.1
33
32
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
33
32