Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Python script with temporary environment variables

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

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

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

However, when running the application on a local machine without Docker and Kubernetes but a humble command:

python app.py

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

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

like image 852
vahdet Avatar asked Jul 12 '19 14:07

vahdet


People also ask

How do I run a Python script in VENV?

To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .

How do you use an environment variable in a Python script?

Read Environment Variables in Python: The os module will require to import to read the environment variables. The os. environ object is used in Python to access the environment variable. The coder can set and get the value of any environment variable by using this object.

Are environment variables temporary?

Environment variables can be defined temporarily by booting to MS-DOS mode, defining variables with the SET command and then starting Windows. Variables defined in this way persist until the system is rebooted.

Can you set environment variables in Python?

With python code, environment variables can be set and manipulated. Setting the environment variable with code makes it more secure and it does not affect the running python script.


2 Answers

you can set the environment variable intended by add the key-value pair in os module's environ dictionary.

import os 

os.environ['intended var'] = 'intended value'

Sample run

>>> os.getenv('intended var')
'intended value'
like image 114
Swadhikar Avatar answered Oct 04 '22 01:10

Swadhikar


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.

Consider the following app.py which just prints the environment variables ENV_PARAM and ENV_PARAM2:

#!/usr/bin/env python3
import os

print(os.environ['ENV_PARAM'])
print(os.environ['ENV_PARAM2'])

When the vars are not set and you call it like this

python app.py

you will get a KeyError.

KeyError: 'ENV_PARAM'

When you instead specify the values in the same line and call it like this

ENV_PARAM='foo' ENV_PARAM2='bar' python app.py

it works fine. Output:

foo
bar

This will not set the environment variable beyond that, so if you do

echo "$ENV_PARAM"  

afterwards, it will return nothing. The environment variable was only set temporary, like you required.

like image 45
buddemat Avatar answered Oct 04 '22 01:10

buddemat