Do You Really Need Environment Variables in Python?
Tags: python, programming, github
Published: 2021-09-27 07:56:19

<p>While developing projects across&nbsp;different environments,&nbsp;you may want to specify different configuration values for each environment.&nbsp;The local system on which you developed an application will not be the same as the one on which you will be deploying it.&nbsp;That&rsquo;s where environment variables come into play!&nbsp;<br />
In this blog, we&rsquo;re going to talk about what environment variables are, why they are important, and how they can be used. We&rsquo;ll walk through various examples of Python environment variables to help you get started.<br />
&nbsp;</p>

<h1><strong>What is an Environment Variable?</strong></h1>

<p>An environment variable is a&nbsp;variable&nbsp;whose value is set outside of a program. They&nbsp;exist outside of your code as part of your server environment.&nbsp;These variables allow you to set different values for a variable depending on the environment in which you are building an application.<br />
&nbsp;</p>

<h1><strong>Why Do We Need&nbsp;Environment Variables?</strong></h1>

<p>Let us see why&nbsp;we need environment variables, not only in Python, in any project we work on.</p>

<h3><strong>Automation</strong></h3>

<p>Many standard applications or projects that we work on have multiple different environments. One environment may be used for development, another can be used during testing, and yet another can be used for production. These environments have different configurations set as per the requirement.&nbsp;<br />
We often use APIs in our projects, and there are API Keys associated with them. But the keys we use in production&nbsp;are probably not going to be the same as the ones you use on your local machine.&nbsp;You may use dummy data on your local machine when building an application, but this is not appropriate in a production environment.<br />
Instead of manually changing the variables when you run a script on a different machine, you can use an environment variable that returns the value of that variable. This means&nbsp;you can easily deploy changes to your code without having to substitute configuration values for a new environment.</p>

<h3><strong>Better Security</strong></h3>

<p>There are various aspects of code you don&#39;t wish others to see, such as API Keys and Secrets. Instead of hardcoding these values directly into the code, if we use environment variables to store them,&nbsp;only your program and the developer who set the variable will be able to access it. This way, our private data is safe and more secured.<br />
&nbsp;</p>

<h1><strong>How to Set an Environment Variable in Python?</strong></h1>

<p>Let&rsquo;s begin by creating an environment variable.&nbsp;This is done similarly to Python dictionaries. It&rsquo;s important to note that this changes the environment variable&nbsp;<em>in this session</em>. In other words, changing the environment variable here will not affect the environment variable anywhere else.&nbsp;</p>

<p>Fire up your Python interpreter and the first step is to import the os library: :</p>

<pre>
<code class="language-bash">Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import os</code></pre>

<p>This library contains the code for working with Python environment variables. Next, we are going to set an environment variable:</p>

<pre>
<code class="language-bash">&gt;&gt;&gt; os.environ["STAGE"] = "DEVELOPMENT"</code></pre>

<h1><br />
<strong>How to Get an Environment Variable&nbsp;in Python?</strong></h1>

<p>The next thing we need to do is to retrieve these variables and use them in our Python code.&nbsp;That&rsquo;s where we can use the&nbsp;<code>os.environ.get()&nbsp;</code>method.&nbsp;This retrieves the value of an environment variable currently set on your system.&nbsp;</p>

<pre>
<code class="language-bash">&gt;&gt;&gt; os.environ.get('STAGE')
'DEVELOPMENT'</code></pre>

<p>You can retrieve a list of all the environment variables you have set by printing the contents of os.environ to the console:</p>

<pre>
<code class="language-bash">&gt;&gt;&gt; os.environ</code></pre>

<p>There are a number of default environment variables set inside Python. This means that printing out all the environment variables set inside a Python program may return a long list.<br />
&nbsp;</p>

<h1><strong>How to Delete an Environment Variable in Python?</strong></h1>

<p>If you need to clear a single environment variable in the session you can use&nbsp;<code>os.environ.pop()</code>&nbsp; with the key&nbsp;and if you need to clear all environment variables you can use&nbsp;<code>os.environ.clear()</code></p>

<pre>
<code class="language-bash">&gt;&gt;&gt; os.environ.pop("STAGE")
'DEVELOPMENT'

&gt;&gt;&gt; os.environ.clear()</code></pre>

<p>&nbsp;You should only use the&nbsp;<code>clear()&nbsp;</code>method if you are confident that you do not need any of the environment variables in a program anymore. Using the&nbsp;<code>pop()</code>&nbsp;method is preferred because it is more specific.<br />
&nbsp;</p>

<div style="background:#eeeeee; border:1px solid #cccccc; padding:5px 10px">
<p>&nbsp;It&rsquo;s important to remember that the settings you apply in a Python script don&rsquo;t work outside that specific process;&nbsp;<code>os.environ</code>&nbsp;doesn&rsquo;t overwrite the environment variables system-wide. If you need to permanently delete or set environment variables you will need to do so with a shell environment, such as Bash.</p>
</div>

<p>&nbsp;</p>

<h1><strong>Using Dotenv to Manage Environment Variables in Python</strong></h1>

<p>&nbsp;As the number of environment variables increase, we may need some other way to manage them.&nbsp;The <strong>python-dotenv</strong> library provides a number of useful functions for managing environment variables.&nbsp;<br />
It allows&nbsp;you to read environment variables from a&nbsp;file. This means that you do not need to declare them inside a Python shell.&nbsp;Reading environment variables from a module file is more convenient if you have a lot of values to read. It also makes it easier to manage variables if you need to change their values.&nbsp;</p>

<p>To work with the dotenv package, you will need to install it:</p>

<pre>
<code class="language-bash">&gt;&gt;&gt; pip install python-dotenv</code></pre>

<p>Let&rsquo;s start by defining an environment variable. To do this, we are going to create a file called .env. We can create this file from the command line using the touch command:</p>

<pre>
<code class="language-bash">&gt;&gt;&gt; touch .env</code></pre>

<p>Open up your .env file and add in the following contents:</p>

<pre>
<code class="language-markdown">STAGE=DEVELOPMENT
USER=ASHUTOSH</code></pre>

<p>Environment variables are assigned like any other variable. On the left side of the equals sign, you have the name of the variable. On the right side, you have the value that variable will store. These two values are separated by an equals sign.</p>

<p>Next, we&rsquo;re going to load our variables into a python file:</p>

<pre>
<code class="language-python">from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env file</code></pre>

<p>This code imports the&nbsp;<code>load_dotenv()</code>&nbsp;method from the dotenv library and executes it. This will read all the variables in our .env file into our environment.</p>

<p>Next, let&rsquo;s try to retrieve our variable using the os library:</p>

<pre>
<code class="language-python">import os

user = os.environ.get('USER')
stage = os.environ.get('STAGE')

print(user, stage)</code></pre>

<p>Our code returns:&nbsp;<code>ASHUTOSH DEVELOPMENT</code>. Our USER and STAGE&nbsp;was set inside our .env file. The&nbsp;<code>load_dotenv()</code>&nbsp;method loaded our environment variables and made them accessible using the os.environ method.</p>

<p>Like many aspects of Python, implementing environment variables isn&rsquo;t cumbersome, provided you have the right tools. By making use of the&nbsp;os&nbsp;package (especially&nbsp;os.environ), along with&nbsp;dotenv&nbsp;when the situation calls for it, you have all the tools you need to begin using environment variables in Python.<br />
&nbsp;</p>

<h1><strong>Using Python-Decouple to Manage Environment Variables in Python</strong></h1>

<p>To manage our environment variables, we have another library called&nbsp;<code>python-decouple</code>. We can set the environment variables in a .env file&nbsp;in the same way we did before.</p>

<p>First install python-decouple into your local Python environment:</p>

<pre>
<code class="language-bash">&gt;&gt;&gt; pip install python-decouple</code></pre>

<p>Now that you have installed the library and&nbsp;your environment variables are stored in a&nbsp;<code>.env</code>&nbsp;file, you can access them in your Python code like this:</p>

<pre>
<code class="language-python">from decouple import config

user = config('USER')
stage = config('STAGE')

print(user, stage)</code></pre>

<p>The code will output :&nbsp;<code>ASHUTOSH DEVELOPMENT</code>. Notice that here we didn&#39;t need the os module to access the environment variables.</p>

<p>The benefit of using something like the above approach(whether python-dotenv or python-decouple) is that when you deploy your application to a cloud service, you can set your environment variables using whatever method or interface the provider has and your Python code should still be able to access them.</p>

<p>&nbsp;</p>

<h1><strong>Points to Ponder</strong></h1>

<p>There are few things that we need to take care of while managing our environment variables:</p>

<ul>
	<li>It is common convention to use capital letters for names of global constants in your code.</li>
	<li>If you&rsquo;re using git, remember to add&nbsp;<code>.env</code>&nbsp;to your&nbsp;<code>.gitignore</code>&nbsp;file so that you don&rsquo;t commit this file to your code repository.</li>
	<li>Most cloud service providers will have a CLI or web interface that lets you configure the environment variables for your staging or production environments. For guidance in these cases you&#39;ll need to refer to their documentation on how to set environment variables when using their service.</li>
</ul>

<h1><br />
<strong>Conclusion</strong></h1>

<p>Environment variables are predefined values which are used to configure a value outside of a program. They are commonly used to set up different environments, hence their name.&nbsp;</p>

<p>Environment variables are a secure way to set secret values. You would not want to directly add any secret&nbsp;in your application code because it would be readable to everyone who sees your application. You can set it in an environment variable so that only you and your program can see the value.</p>

<p>Now you&rsquo;re ready to start working with environment variables in Python code like a professional!</p>

<p>This post was originally published on <a href="https://iread.ga">iRead</a> : <a href="https://iread.ga/posts/49/do-you-really-need-environment-variables-in-python">https://iread.ga/posts/49/do-you-really-need-environment-variables-in-python</a>.</p>