{"id":310,"date":"2018-09-20T00:04:38","date_gmt":"2018-09-19T16:04:38","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=310"},"modified":"2018-10-28T13:53:03","modified_gmt":"2018-10-28T05:53:03","slug":"how-to-set-environment-variables-for-your-python-application-from-pycharm","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-set-environment-variables-for-your-python-application-from-pycharm\/","title":{"rendered":"How to set environment variables for your Python application from PyCharm"},"content":{"rendered":"<p>The <a href=\"https:\/\/12factor.net\/\" rel=\"noopener\" target=\"_blank\">Twelve-Factor App methodology<\/a> recommends <a href=\"https:\/\/12factor.net\/config\" rel=\"noopener\" target=\"_blank\">storing configurations in the environment<\/a>. <\/p>\n<p>When you follow this methodology and want to run your Python application from PyCharm, you need to create a customised run configuration.<\/p>\n<p>In case you need it, this post shows how to set environment variables for your Python application from PyCharm.<\/p>\n<h2>Creating a run configuration with environment variables<\/h2>\n<p>To create a run configuration with environment variables, you can follow through these screens:<\/p>\n<p>While your project is selected, click on <strong>Run<\/strong> -> <strong>Edit Configurations<\/strong>...<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-Selecting-Edit-Configurations...-from-Run-Menu.jpg\" alt=\"PyCharm CE Version 2018.2.3 Selecting Edit Configurations... from Run Menu\" \/><\/p>\n<p>Give your run configuration a name and click on the folder icon in the <strong>Environment Variables<\/strong> text input:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-Run-Debug-Configurations-screen.jpg\" alt=\"PyCharm CE Version 2018.2.3 Run Debug Configurations screen\" \/><\/p>\n<p>Click <strong>+<\/strong> to add a new environment variable. When you had create all the environment variables that you wish to supply to your Python application, click <strong>OK<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-Environment-Variables-Dialog-with-2-environment-variables.jpg\" alt=\"PyCharm CE Version 2018.2.3 Environment Variables Dialog with 2 environment variables\" \/><\/p>\n<p>Click <strong>Apply<\/strong> then click <strong>OK<\/strong> to finalize your run configurations<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-Run-Debug-Configurations-screen-with-2-environment-variables-set.jpg\" alt=\"PyCharm CE Version 2018.2.3 Run Debug Configurations screen with 2 environment variables set\"\/><\/p>\n<h2>Running or debugging your Python application with the new run configuration containing your environment variables<\/h2>\n<p>Once you had created your run configurations with the environment variables that you want to feed your application, you can right click on a Python script to run or debug it:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-editor-with-new-run-configurations-for-running-and-debugging-a-script.jpg\" alt=\"PyCharm CE Version 2018.2.3 editor with new run configurations for running and debugging a script\"\/><\/p>\n<p>When you run or debug with the new run configurations, your Python application will be able to access the environment variables that you had created:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-editor-after-running-a-run-configuration-with-an-environment-variable-MESSAGEHello-World.jpg\" alt=\"PyCharm CE Version 2018.2.3 editor after running a run configuration with an environment variable MESSAGE=Hello World\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-50\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-50&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-50&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F310&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>The <a href=\"https:\/\/12factor.net\/\" rel=\"noopener\" target=\"_blank\">Twelve-Factor App methodology<\/a> recommends <a href=\"https:\/\/12factor.net\/config\" rel=\"noopener\" target=\"_blank\">storing configurations in the environment<\/a>. <\/p>\n<p>When you follow this methodology and want to run your Python application from PyCharm, you need to create a customised run configuration.<\/p>\n<p>In case you need it, this post shows how to set environment variables for your Python application from PyCharm.<\/p>\n","protected":false},"author":1,"featured_media":1305,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[389,546,226,233,255],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PyCharm-CE-Version-2018.2.3-Run-Debug-Configurations-screen-with-2-environment-variables-set.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-50","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/310"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1305"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}