8

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

6
  • 4
    Can you please paste the output from both ways? Commented Jul 26, 2016 at 6:40
  • 8
    There is no such thing as a "complete" environment. Different variables will be present in different situations. What's missing, and why is it a problem? Commented Jul 26, 2016 at 6:46
  • They're identical for me! Commented Jul 26, 2016 at 6:52
  • 1
    which operating system are you running this on? Commented Jul 26, 2016 at 6:52
  • 2
    sudo -E Commented Jul 27, 2016 at 5:18

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

did you try using the export keyword e.g.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.