{"id":314,"date":"2019-10-16T13:50:49","date_gmt":"2019-10-16T04:50:49","guid":{"rendered":"http:\/\/localhost:8000\/?p=314"},"modified":"2021-01-23T12:21:02","modified_gmt":"2021-01-23T03:21:02","slug":"python-environment-variables","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/10\/python-environment-variables.html","title":{"rendered":"python\u306b\u304a\u3051\u308b\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u306e\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a"},"content":{"rendered":"
Twelve-Factor App<\/a>\u304c\u30c7\u30d5\u30a1\u30af\u30c8\u306b\u306a\u308a\u3064\u3064\u3042\u308b\u4e2d\u3001python\u306e\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u969b\u3082\u3001\u8a2d\u5b9a\u306f\u30b3\u30fc\u30c9\u304b\u3089\u5207\u308a\u96e2\u3057\u74b0\u5883\u5909\u6570\u306b\u683c\u7d0d\u3059\u308b\u3053\u3068\u3082\u591a\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u57fa\u672c\u7684\u306b\u306f\u4e0a\u306b\u3042\u308b\u3082\u306e\u307b\u3069\u3001\u512a\u5148\u5ea6\u304c\u9ad8\u3044\u3068\u8003\u3048\u307e\u3057\u305f\u3002<\/p>\n direnv<\/a> \u306f\u3001\u30b7\u30f3\u30d7\u30eb\u3067\u5f37\u529b\u306a\u30c4\u30fc\u30eb\u3067\u3059\u3002<\/p>\n \u305f\u3060\u3057\u3001Windows\u3067\u306f\u7d20\u76f4\u306b\u306f\u4f7f\u3048\u306a\u3044\u307f\u305f\u3044\u3067\u3001Git Bash\u3084Cygwin\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308b\u3063\u307d\u3044\u3067\u3059\uff08issue<\/a>\u53c2\u7167\u306e\u3053\u3068\uff09\u3002 python-dotenv\u306f\u3001node.js\u306edotenv\u306epython\u7248\u3067\u3001 \u307e\u305a\u3001\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n \u3042\u3068\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4e2d\u3067\u74b0\u5883\u5909\u6570\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/p>\n \u3053\u308c\u3060\u3051\u3067\u3059\u3002<\/p>\n \u5b9f\u969b\u306b\u306f\u3001 \u3068\u3057\u3066\u304a\u304d\u3001\u5b9f\u884c\u3057\u305f\u3044python\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4e2d\u3067<\/p>\n \u3068\u3059\u308c\u3070\u3001\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n flake8\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001 \u3068\u3057\u3066\u3001\u30c1\u30a7\u30c3\u30af\u5bfe\u8c61\u5916\u306b\u3059\u308b\u3068\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n Foreman\u306fRubyGems\u3067\u516c\u958b\u3055\u308c\u3066\u3044\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002Procfile\u306b python\u306e\u516c\u5f0f\u30a4\u30e1\u30fc\u30b8\u3092\u30d9\u30fc\u30b9\u306b\u30e9\u30a4\u30d6\u30e9\u30ea\u4f9d\u5b58\u95a2\u4fc2\u3092\u89e3\u6c7a\u3057\u74b0\u5883\u5909\u6570\u3082\u8a2d\u5b9a\u3057\u305f\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u3063\u3066\u3001\u305d\u306e\u30a4\u30e1\u30fc\u30b8\u3092\u4eee\u60f3VM\u4e0a\u3067\u52d5\u304b\u3057\u3066ssh\u3067\u5165\u3063\u3066python\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002 Windows\u306e\u30e1\u30f3\u30d0\u30fc\u304c\u3044\u306a\u3044\u5834\u5408\u306f\u3001direnv<\/b>\u4e00\u629e\u3067\u3059\u3002<\/p>\n Windows\u306e\u30e1\u30f3\u30d0\u30fc\u304c\u3044\u308b\u5834\u5408\u306f\u3068\u3066\u3082\u60a9\u307e\u3057\u3044\u3067\u3059\u306d\u3002Cygwin\u3084Git Bash\u3092\u4f7f\u3046\u3053\u3068\u306b\u30e1\u30f3\u30d0\u30fc\u304c\u4e86\u627f\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3042\u308c\u3070\u3001direnv<\/b>\u3067\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002 Docker\u306a\u3069\u306e\u4eee\u60f3VM\u3067\u52d5\u304b\u3055\u306a\u3044\u5834\u5408\u3001<\/p>\n \u3068\u3044\u3046\u5f62\u3067\u3001python\u306e\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u74b0\u5883\u306e\u72ec\u7acb\u6027\u306f\u307b\u307c\u5b88\u3089\u308c\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u306a\u3068\u611f\u3058\u3066\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" Twelve-Factor App\u304c\u30c7\u30d5\u30a1\u30af\u30c8\u306b\u306a\u308a\u3064\u3064\u3042\u308b\u4e2d\u3001python\u306e\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u969b\u3082\u3001\u8a2d\u5b9a\u306f\u30b3\u30fc\u30c9\u304b\u3089\u5207\u308a\u96e2\u3057\u74b0\u5883\u5909\u6570\u306b\u683c\u7d0d\u3059\u308b\u3053\u3068\u3082\u591a\u3044\u3068\u601d\u3044\u307e\u3059\u3002 staging\u74b0\u5883\u3084production\u74b0\u5883\u306f\u3001service\u5316\u3059\u308b\u306a\u308adocker\u5316\u3059\u308b\u306a\u308a\u3001\u4f55\u3089\u304b\u306e\u65b9\u6cd5\u3067\u74b0\u5883\u5909\u6570\u3092\u5916\u90e8\u304b\u3089\u4e0e\u3048\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u74b0\u5883\u306b\u304a\u3044\u3066\u3069\u3046\u3084\u3063\u3066\u74b0\u5883\u5909\u6570\u3092\u4e0e\u3048\u308b\u304b\u306f\u7d50\u69cb\u60a9\u307f\u3069\u3053\u308d\u3067\u3059\u3002 \u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u305d\u306e\u8fba\u3092\u8003\u3048\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u8003\u616e\u30dd\u30a4\u30f3\u30c8 \u57fa\u672c\u7684\u306b\u306f\u4e0a\u306b\u3042\u308b\u3082\u306e\u307b\u3069\u3001\u512a\u5148\u5ea6\u304c\u9ad8\u3044\u3068\u8003\u3048\u307e\u3057\u305f\u3002 \u74b0\u5883\u3092\u6c5a\u3055\u306a\u3044\uff08\u74b0\u5883\u5909\u6570\u304c\u6b8b\u3063\u305f\u307e\u307e\u306b\u3057\u306a\u3044\uff09 \u74b0\u5883\u5909\u6570\u304c\u6b8b\u3063\u305f\u307e\u307e\u306b\u306a\u308b\u3068\u3001\u5225\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u540c\u3058terminal\u3067\u5b9f\u884c\u3057\u3066\u3001\u4e0d\u6b63\u306a\u52d5\u4f5c\u3092\u3057\u305f\u308a\u4e8b\u6545\u306b\u3064\u306a\u304c\u308b \u958b\u767a\u306e\u30b9\u30d4\u30fc\u30c9\u3092\u843d\u3068\u3055\u306a\u3044 \u5b9f\u88c5\u304b\u3089\u52d5\u4f5c\u78ba\u8a8d\u307e\u3067\u306e\u30a4\u30c6\u30ec\u30fc\u30c8\u304c\u77ed\u3051\u308c\u3070\u77ed\u3044\u307b\u3069\u3088\u3044\uff08\u74b0\u5883\u7acb\u3061\u4e0a\u3052\u306b\u6642\u9593\u304c\u304b\u304b\u308b\u306e\u306f\u5acc\uff09 \u904b\u7528\u306e\u624b\u9593\u3092\u5897\u3084 <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/314"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":315,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/314\/revisions\/315"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\nstaging\u74b0\u5883\u3084production\u74b0\u5883\u306f\u3001service\u5316\u3059\u308b\u306a\u308adocker\u5316\u3059\u308b\u306a\u308a\u3001\u4f55\u3089\u304b\u306e\u65b9\u6cd5\u3067\u74b0\u5883\u5909\u6570\u3092\u5916\u90e8\u304b\u3089\u4e0e\u3048\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u30ed\u30fc\u30ab\u30eb\u958b\u767a\u74b0\u5883\u306b\u304a\u3044\u3066\u3069\u3046\u3084\u3063\u3066\u74b0\u5883\u5909\u6570\u3092\u4e0e\u3048\u308b\u304b\u306f\u7d50\u69cb\u60a9\u307f\u3069\u3053\u308d\u3067\u3059\u3002
\n\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u305d\u306e\u8fba\u3092\u8003\u3048\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\u8003\u616e\u30dd\u30a4\u30f3\u30c8<\/h2>\n
\n
\n
\n
\n
\n
\u5bfe\u5fdc\u6848<\/h2>\n
direnv<\/h3>\n
\n
.envrc<\/code>)\u3092\u7f6e\u3044\u3066\u3042\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u914d\u4e0b\u306bcd\u3057\u305f\u6642\u3060\u3051\u3001\u5b9a\u7fa9\u3092\u8aad\u307f\u8fbc\u307f\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u3066\u304f\u308c\u308b<\/li>\n
\n\u8003\u616e\u30dd\u30a4\u30f3\u30c8\u300c1, 2, 3, 4\u300d\u306fOK\u3067\u3001\u300c5\u300d\u3060\u3051\u5fae\u5999\u3067\u3059\u3002<\/p>\npython-dotenv<\/h3>\n
.env<\/code>\u306b\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3001
load_dotenv()<\/code>\u3059\u308b\u3060\u3051\u3067\u74b0\u5883\u5909\u6570\u306b\u8a2d\u5b9a\u3057\u3066\u304f\u308c\u308b\u3068\u3066\u3082\u30b7\u30f3\u30d7\u30eb\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002
\n\u8003\u616e\u30dd\u30a4\u30f3\u30c8\u300c1, 2, 3, 5\u300d\u306fOK\u3067\u3059\u304c\u3001\u300c4\u300d\u304cNG\u3067\u3059\u3002
\n\u300c4\u300d\u306e\u9055\u548c\u611f\u306b\u76ee\u3092\u3064\u3076\u308c\u3070\u3001\u30b7\u30f3\u30d7\u30eb\u3067\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n[\u53c2\u8003] python-dotenv\u306e\u4f7f\u3044\u65b9<\/h4>\n
pip install python-dotenv<\/code><\/pre>\n
from dotenv import load_dotenv\nload_dotenv()<\/code><\/pre>\n
setenv.py<\/code>\u3092\u4f5c\u3063\u3066<\/p>\n
from dotenv import load_dotenv\nload_dotenv(override=True) # override\u3067\u65e2\u5b58\u306e\u74b0\u5883\u5909\u6570\u304c\u3042\u3063\u3066\u3082\u4e0a\u66f8\u304d<\/code><\/pre>\n
import setenv<\/code><\/pre>\n
'setenv' imported but unused<\/code>\u3068\u6012\u3089\u308c\u308b\u306e\u3067\u3001<\/p>\n
import setenv # NOQA<\/code><\/pre>\n
.env<\/h3>\n
.env<\/code>\u306b
export HOGE=fuga<\/code>\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u304a\u304d\u3001\u305d\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092
source .env<\/code>\u3067\u8aad\u307f\u8fbc\u307f\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u3066\u304b\u3089\u3001python\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
\n\u3060\u3060\u3057\u3001Windows\u3067\u306f\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a\u65b9\u6cd5\u304cset HOGE=fuga<\/code>\u306b\u306a\u308b\u3057\u3001
source .env<\/code>\u307f\u305f\u3044\u306a\u3053\u3068\u304c\u3067\u304d\u308b\u304b\u3069\u3046\u304b\u304c\u3088\u304f\u5206\u304b\u308a\u307e\u305b\u3093\uff08\u9577\u3044\u3053\u3068windows\u89e6\u3063\u3066\u306a\u3044\u306e\u3067...\uff09\u3002
\n\u8003\u616e\u30dd\u30a4\u30f3\u30c8\u300c2, 3, 4\u300d\u306fOK\u3067\u3059\u304c\u3001\u6700\u512a\u5148\u4e8b\u9805\u3067\u3042\u308b\u300c1\uff1a\u74b0\u5883\u3092\u6c5a\u3055\u306a\u3044\u300d\u304c\u5b9f\u73fe\u3067\u304d\u306a\u3044\u306e\u3067\u3001\u4eca\u56de\u306f\u9664\u5916\u3057\u307e\u3057\u305f\u3002<\/p>\nForman<\/h3>\n
\u30b3\u30de\u30f3\u30c9\u540d: \u30b9\u30af\u30ea\u30d7\u30c8<\/code>\u306e\u5f62\u5f0f\u3067\u30b3\u30de\u30f3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u304a\u304d\u3001
foreman run \u30b3\u30de\u30f3\u30c9\u540d<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3067\u304d\u3001\u305d\u306e\u969b\u306b
.env<\/code>\u306b\u8a18\u8f09\u3055\u308c\u3066\u3044\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u74b0\u5883\u5909\u6570\u306b\u8a2d\u5b9a\u3057\u3066\u304f\u308c\u308b\u30c4\u30fc\u30eb\u3067\u3059\u3002
\nRubyGems\u306a\u306e\u3067\u304d\u3063\u3068Windows\u3067\u3082\u4f7f\u3048\u308b\u3067\u3057\u3087\u3046\u3002
\n\u8003\u616e\u30dd\u30a4\u30f3\u30c8\u300c1, 2, 4, 5\u300d\u306fOK\u306a\u306e\u3067\u3059\u304c\u3001\u300c3:\u904b\u7528\u306e\u624b\u9593\u300d\u3064\u307e\u308a\u3001Procfile\u306e\u7ba1\u7406\u3068\u3044\u3046\u624b\u9593\u304c\u5897\u3048\u308b\u70b9\u304c\u5fae\u5999\u3067\u3059\u3002<\/p>\nDocker \/ Vagrant<\/h3>\n
\n\u8003\u616e\u30dd\u30a4\u30f3\u30c8\u300c1, 4, 5\u300d\u306fOK\u306a\u306e\u3067\u3059\u304c\u3001DockerFile\u306a\u308aVagrantFile\u306a\u308a\u3092\u7ba1\u7406\u3059\u308b\u300c3:\u904b\u7528\u306e\u624b\u9593\u300d\u304c\u5897\u3048\u308b\u306e\u3068\u3001\u30a4\u30e1\u30fc\u30b8\u3092\u30ed\u30fc\u30c9\u3059\u308b\u6642\u9593\u306a\u3069\u3092\u8003\u3048\u308b\u3068\u300c2:\u958b\u767a\u30b9\u30d4\u30fc\u30c9\u300d\u3082\u843d\u3061\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u4eca\u56de\u306f\u9664\u5916\u3057\u307e\u3057\u305f\u3002<\/p>\n\u7d50\u8ad6<\/h2>\n
\n\u305d\u308c\u304c\u96e3\u3057\u3044\u5834\u5408\u306f\u3001python-dotenv<\/b>\u304bforeman<\/b>\u306e\u3044\u305a\u308c\u304b\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\u6240\u611f<\/h2>\n
\n