{"id":1049,"date":"2017-05-02T00:34:21","date_gmt":"2017-05-01T15:34:21","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1049"},"modified":"2017-05-02T19:23:49","modified_gmt":"2017-05-02T10:23:49","slug":"%e7%92%b0%e5%a2%83%e5%a4%89%e6%95%b0%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e3%81%a6%e7%92%b0%e5%a2%83%e6%af%8e%e3%81%ab%e8%a8%ad%e5%ae%9a%e3%82%92%e5%88%87%e3%82%8a%e6%9b%bf%e3%81%88%e3%82%8b-os-environ","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1049","title":{"rendered":"\u74b0\u5883\u5909\u6570\u3092\u53d6\u5f97\u3057\u3066\u74b0\u5883\u6bce\u306b\u8a2d\u5b9a\u3092\u5207\u308a\u66ff\u3048\u308b os.environ"},"content":{"rendered":"<p>\u3042\u308b\u7a0b\u5ea6\u306e\u898f\u6a21\u306e\u30b7\u30b9\u30c6\u30e0\u3092\u958b\u767a\u3059\u308b\u5834\u5408\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u30ed\u30fc\u30ab\u30eb\u3001\u958b\u767a\u3001\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u3001\u672c\u756a\u306a\u3069\u8907\u6570\u306e\u74b0\u5883\u3067\u52d5\u4f5c\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u540c\u3058linux\u74b0\u5883\u3067\u3082DB\u306e\u63a5\u7d9a\u5148\u3084\u30e1\u30fc\u30eb\u306e\u5b9b\u5148\u306f\u74b0\u5883\u3054\u3068\u306b\u5909\u3048\u308b\u5fc5\u8981\u304c\u51fa\u3066\u304f\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u305d\u3093\u306a\u3068\u304d\u74b0\u5883\u5909\u6570\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002\u3053\u306e\u30da\u30fc\u30b8\u3067\u306f\u74b0\u5883\u5909\u6570\u3092\u53d6\u5f97\u3057\u3001\u74b0\u5883\u3054\u3068\u306b\u8a2d\u5b9a\u3092\u5207\u308a\u66ff\u3048\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h2>\u74b0\u5883\u5909\u6570\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>python\u3067\u306f\u74b0\u5883\u5909\u6570\u306f os\u30e2\u30b8\u30e5\u30fc\u30eb\u306eos.environ\u306b\u8f9e\u66f8\u5f62\u5f0f\u3067\u683c\u7d0d\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u8f9e\u66f8\u5f62\u5f0f\u306a\u306e\u3067\u3001\u5909\u6570\u540d\u3092\u30ad\u30fc\u3067\u6307\u5b9a\u3059\u308b\u304b\u3001get\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\n\r\n# \u74b0\u5883\u5909\u6570\u53d6\u5f97(\u53d6\u5f97\u3067\u304d\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc)\r\napp_env = os.environ[&quot;APP_ENV&quot;]\r\nprint(app_env)\r\n\r\n# \u74b0\u5883\u5909\u6570\u53d6\u5f97(\u53d6\u5f97\u3067\u304d\u306a\u304f\u3066\u3082\u30a8\u30e9\u30fc\u306b\u306a\u3089\u305aNone\u304c\u8fd4\u3055\u308c\u308b)\r\napp_env = os.environ.get(&quot;APP_ENV&quot;)\r\nprint(app_env)\r\n\r\n<\/pre>\n<p>\u4ee5\u4e0b\u306flinux\u3067\u306e\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u4e0a\u3067\u306e\u5b9f\u884c\u4f8b\u3067\u3059\u304c\u3001mac\u3082\u540c\u69d8\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u52d5\u4f5c\u78ba\u8a8d\u306f\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3059\u304c\u3001windows\u306e\u5834\u5408\u306fset=TEST\u306a\u3069\u3068\u3059\u308b\u3068\u52d5\u304f\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ APP_ENV=DEV\r\n$ export APP_ENV\r\n$ python sample.py \r\n$ DEV\r\n$ DEV\r\n<\/pre>\n<p>export\u3057\u305f\u5909\u6570\u304cpython\u30b3\u30fc\u30c9\u4e0a\u3067\u53d6\u5f97\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u306e\u30b3\u30e1\u30f3\u30c8\u4e0a\u306b\u3082\u8a18\u8f09\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u6307\u5b9a\u3057\u305f\u74b0\u5883\u5909\u6570\u81ea\u4f53\u304c\u306a\u3044\u5834\u5408\u3001\u30a8\u30e9\u30fc\u3092\u767a\u751f\u3055\u305b\u305f\u3044\u5834\u5408\u306f\u30ad\u30fc\u3092\u5927\u30ab\u30c3\u30b3\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002\u307e\u305f\u3001get\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u306a\u3044\u5834\u5408\u306fNone\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<h2>\u74b0\u5883\u3054\u3068\u306e\u5207\u66ff\u4f8b<\/h2>\n<p>\u3053\u308c\u3092\u5229\u7528\u3057\u3066\u74b0\u5883\u5225\u306b\u5207\u308a\u66ff\u3048\u308b\u5b9f\u88c5\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002\u4ed5\u69d8\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3068\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u74b0\u5883\u5909\u6570APP_ENV=DEV\u306e\u5834\u5408\u306f\u958b\u767a\u74b0\u5883<\/li>\n<li>\u74b0\u5883\u5909\u6570APP_ENV=STG\u306e\u5834\u5408\u306f\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0<\/li>\n<li>\u74b0\u5883\u5909\u6570APP_ENV=PRD\u306e\u5834\u5408\u306f\u672c\u756a\u74b0\u5883<\/li>\n<li>\u74b0\u5883\u5909\u6570APP_ENV=\u304c\u306a\u3044\u5834\u5408\u3084\u4e0a\u8a18\u4ee5\u5916\u306e\u5834\u5408\u306f\u30ed\u30fc\u30ab\u30eb\u74b0\u5883<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\n\r\ndef main():\r\n    # \u74b0\u5883\u5909\u6570\u53d6\u5f97\r\n    app_env = os.environ.get(&quot;APP_ENV&quot;)\r\n    \r\n    if app_env == 'DEV':\r\n        # \u958b\u767a\u74b0\u5883\u7528\u306e\u8a2d\u5b9a\u3092\u884c\u3046\r\n        print('\u958b\u767a')\r\n    elif app_env == 'STG':\r\n        # \u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u74b0\u5883\u7528\u306e\u8a2d\u5b9a\u3092\u884c\u3046\r\n        print('\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0')\r\n    elif app_env == 'PRD':\r\n        # \u672c\u756a\u74b0\u5883\u7528\u306e\u8a2d\u5b9a\u3092\u884c\u3046\r\n        print('\u672c\u756a')\r\n    else:\r\n        # \u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u7528\u306e\u8a2d\u5b9a\u3092\u884c\u3046\r\n        print('\u30ed\u30fc\u30ab\u30eb')\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n\r\n<\/pre>\n<p>\u3053\u308c\u3067\u74b0\u5883\u5909\u6570\u3067\u81ea\u52d5\u3067\u5207\u308a\u66ff\u308f\u308b\u305f\u3081\u3001\u624b\u52d5\u306b\u3088\u308b\u8a2d\u5b9a\u306e\u624b\u9593\u3084\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u914d\u5e03\u6642\u306b\u8a2d\u5b9a\u304c\u4e0a\u66f8\u304d\u3055\u308c\u305f\u3001\u3068\u3044\u3063\u305f\u4e8b\u6545\u3082\u6e1b\u3089\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3042\u308b\u7a0b\u5ea6\u306e\u898f\u6a21\u306e\u30b7\u30b9\u30c6\u30e0\u3092\u958b\u767a\u3059\u308b\u5834\u5408\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u30ed\u30fc\u30ab\u30eb\u3001\u958b\u767a\u3001\u30b9\u30c6\u30fc\u30b8\u30f3\u30b0\u3001\u672c\u756a\u306a\u3069\u8907\u6570\u306e\u74b0\u5883\u3067\u52d5\u4f5c\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u540c\u3058linux\u74b0\u5883\u3067\u3082DB\u306e\u63a5\u7d9a\u5148\u3084\u30e1\u30fc\u30eb\u306e\u5b9b\u5148\u306f\u74b0\u5883\u3054\u3068\u306b\u5909\u3048\u308b\u5fc5\u8981\u304c\u51fa\u3066\u304f\u308b\u3068\u601d\u3044\u307e...<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1049"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=1049"}],"version-history":[{"count":3,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1049\/revisions"}],"predecessor-version":[{"id":1054,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1049\/revisions\/1054"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}