Skip to main content

os.getenv("xxx") shows different result while using sudo or not to run python3

It happened to me when I try to use os.getenv.

I have edited the "\etc\profile" file to add an env variable and ran the source command.

And I wrote some codes to test it.It worked well and I can get the env variable I set when I run python just using python3 command in the command line.

But I find it doesn't work when I using sudo python3.The os.getenv("xxx") returns None. That's the question.Why it doesn't work just because of using sudo.

Solved

sudo does not keep the user's environment variables. Maybe this can help:

How to keep Environment Variables when Using SUDO


Comments