I have a complex piece of software I am not able to post, nor do I have a concrete working example. I will try to explain the problem, maybe someone encountered this before.
On the Linux shell I have defined an environment variable:
> export MY_TEST_ENV=4711
> echo $MY_TEST_ENV
> 4711
Within the complex code I want to obtain this variable with
print os.getenv('MY_TEST_ENV')
which always returns None
. If I create a test-script to test this behavior, even with classes in different files, I always get the desired behavior, e.g., os.getenv('MY_TEST_ENV')
returns the correct value 4711
.
The code is started with sudo
.
Any ideas what could be the reason?
execve
system call and do not fill in the environment variable, the child's environment is zeroed. – Daiseydaisisudo
starts a new shell with superuser privileges, and scrubs the environment according to the security policy. Using the-E
(or--preserve-env
) switch tosudo
may causeMY_TEST_ENV
to be retained, or maybe not again depending on the specifics of your system's security policy. Another route is to request someone with appropriate access to the system to inject the variable you want into root's environment by editing the appropriate initialization script. – Stillwell