dominique
dominique

Reputation: 91

Python: Not all environment variables present in os.environ

In the python shell, using

import os 
print os.environ 

prints the complete list of environment variables with nothing missing. However when I call the interpreter with a filename:

sudo python file.py 

and use

import os 
print os.environ 

I see that some of the environment variables are missing in the dict.

Why do they behave differently?

Operating system: Ubuntu 14.04

Upvotes: 8

Views: 6281

Answers (3)

Charlie Parker
Charlie Parker

Reputation: 5201

did you try using the export keyword e.g.

export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

Upvotes: 1

storm_m2138
storm_m2138

Reputation: 2511

I saw this happen when ENV Vars were not set using export in bash, so they were not propagating to the python script.

Upvotes: 4

dominique
dominique

Reputation: 91

I realised that the difference was because I was using sudo during the execution for the second where the environment was not preserved. I need to add those environment variables to my sudoers file or preserve them during execution.

Upvotes: 1

Related Questions