7

I've got an issue with my python script

First, I defined an environment variable as

export TEST=test

My Python script is quite easy "test.py"

import os
print os.environ['TEST']

So when I run it with

~ $ python test.py

I've got the expected result test printed out. However, if I run the script with

~ $ sudo python test.py

I've got an KeyError: 'TEST' error.

What have I missed ?

GuillaumeA
  • 3,245
  • 3
  • 28
  • 54

1 Answers1

9

Sudo runs with different environment. To keep current environment use -E flag.

sudo -E python test.py

 -E, --preserve-env
             Indicates to the security policy that the user wishes to preserve their existing environment variables.  The security policy may return an error if the user does not have permission to
             preserve the environment.
xiº
  • 4,067
  • 3
  • 21
  • 36