Reputation: 91
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
Reputation: 5201
did you try using the export
keyword e.g.
export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
Upvotes: 1
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
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