3

The following line of code is throwing an environment variable was not found error in a RHEL8 virtual machine running in Azure:

sourceKeys = os.environ.get("MY_ENV_VAR")

However, when we putty into the VM, we are able to access the variable as follows:

$ echo $MY_ENV_VAR
/path/to/some.yaml

The variable is set by packer in a startup script that has the following code:

export MY_ENV_VAR='/path/to/some.yaml'
echo "export MY_ENV_VAR='/path/to/some.yaml'" >> /etc/environment
echo "export MY_ENV_VAR='/path/to/some.yaml'" >> /etc/bashrc
echo "export MY_ENV_VAR='/path/to/some.yaml'" >> /etc/profile

Why is the linux terminal able to read the environment variable while python is not?

What needs to change in order for the python program to be able to read the environment variable's value?

1
  • 2
    How is the Python program executed? Is it a systemd service, by any chance?
    – Haxiel
    Commented Apr 23, 2022 at 8:55

1 Answer 1

2

os.environ.get()will only retrieve the value of environment variables (env-var), accessible to the process where retrieval is taking place. If your process is a daemon (as in a process launched by systemd) or otherwise NOT related to the execution space in which you issue $ echo $MY_ENV_VAR then the result may differ from what you expect.

So in a nutshell, if you are going to export a variable to make it globally available, you need to export it from the shell that also spawns the process that is used to query/retrieve the value of said env-var.

A few answers are available on stackoverflow.com and elsewhere, e.g. here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .