<p>I have a Python application and it utilizes environment variables in Kubernetes configuration such as:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  namespace: default
data:
  var1: foo
  var2: bar

---

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: default
spec:
  containers:
    - envFrom:
      - configMapRef:
          name: my-config
</code></pre>

<p>So, it is fine when the app is Dockerized and running on a Kubernetes farm.</p>

<p>However, when running the application on a <strong>local machine</strong> without Docker and Kubernetes but a humble command:</p>

<pre class="lang-py prettyprint-override"><code>python app.py
</code></pre>

<p>I have to make the Python modules find the environment variables with <code>os.getenv('var1')</code> even if there is no ConfigMap or Pod on.</p>

<p>Is it possible to without a need to add extra code in Python modules or adding environment variables to the local machine system?</p>

vahdet
<p>In a shell, you could also simply temporarily assign the value(s) for the environment variable(s) right before calling the script. No need to change your script at all.</p>
<p>Consider the following <code>app.py</code> which just prints the environment variables <code>ENV_PARAM</code> and <code>ENV_PARAM2</code>:</p>
<pre class="lang-python prettyprint-override"><code>#!/usr/bin/env python3
import os

print(os.environ['ENV_PARAM'])
print(os.environ['ENV_PARAM2'])
</code></pre>
<p>When the vars are not set and you call it like this</p>
<pre class="lang-bash prettyprint-override"><code>python app.py
</code></pre>
<p>you will get a <code>KeyError</code>.</p>
<blockquote>
<p>KeyError: 'ENV_PARAM'</p>
</blockquote>
<p>When you instead specify the values in the same line and call it like this</p>
<pre class="lang-bash prettyprint-override"><code>ENV_PARAM='foo' ENV_PARAM2='bar' python app.py
</code></pre>
<p>it works fine. Output:</p>
<pre><code>foo
bar
</code></pre>
<p>This will not set the environment variable beyond that, so if you do</p>
<pre class="lang-bash prettyprint-override"><code>echo &quot;$ENV_PARAM&quot;  
</code></pre>
<p>afterwards, it will return nothing. The environment variable was only set temporary, like you required.</p>
