{"id":2978,"date":"2017-02-28T15:14:56","date_gmt":"2017-02-28T20:14:56","guid":{"rendered":"http:\/\/blogs.terrorware.com\/geoff\/?p=2978"},"modified":"2017-02-28T15:14:56","modified_gmt":"2017-02-28T20:14:56","slug":"setting-environment-variables-in-a-python-virtualenv-from-a-env-file","status":"publish","type":"post","link":"https:\/\/blogs.terrorware.com\/geoff\/2017\/02\/28\/setting-environment-variables-in-a-python-virtualenv-from-a-env-file\/","title":{"rendered":"Setting environment variables in a Python virtualenv from a .env file"},"content":{"rendered":"<p>Lately, I&#8217;ve gotten into the practice of storing configuration in environment variables and keeping those in a <code>.env<\/code> file in the root of my project directory.<\/p>\n<p>This file contains simple environment variable assignment and is used by Heroku and Docker.<\/p>\n<p>However making sure these environment variables get set for any command being run in the development environment.<\/p>\n<p>I often work in Python virtual environments, so putting the shell commands in the <code>postactivate<\/code> and <code>postdeactivate<\/code> scripts makes sense to me.<\/p>\n<p>To set environment variables in <code>postactivate<\/code>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n# This hook is sourced after this virtualenv is activated.\ncd ~\/workspace\/scrape-represent-statements\nset -a\nsource .env\nset +a\n<\/pre>\n<p>To unset environment variables in <code>postdeactivate<\/code>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n# This hook is sourced after this virtualenv is deactivated.\nwhile read var; do unset $var; done &amp;lt; &amp;lt;(cat .env | grep -v &amp;#039;^\\s*#&amp;#039; | sed  &amp;#039;s\/\\s*\\(&#x5B;^=]*\\)=.*\/\\1\/&amp;#039;)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Lately, I&#8217;ve gotten into the practice of storing configuration in environment variables and keeping those in a .env file in the root of my project directory. This file contains simple environment variable assignment and is used by Heroku and Docker. However making sure these environment variables get set for any command being run in the&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.terrorware.com\/geoff\/2017\/02\/28\/setting-environment-variables-in-a-python-virtualenv-from-a-env-file\/\">Continue reading <span class=\"screen-reader-text\">Setting environment variables in a Python virtualenv from a .env file<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2978","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4wnIz-M2","_links":{"self":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/2978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/comments?post=2978"}],"version-history":[{"count":1,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/2978\/revisions"}],"predecessor-version":[{"id":2979,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/2978\/revisions\/2979"}],"wp:attachment":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/media?parent=2978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/categories?post=2978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/tags?post=2978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}