2

I've set a new environment variable using export new_v = "mynewvar" in my .bash_profile file. I've run source .bash_profile and when I run $ env from terminal I can see the new variable. However, when I run os.environ in python, it's not there. I'm running python 3.6.3 in IDLE on OSX 10.13.1. Is there a way to export new environment variables such that python will be able to access them?

Cole Robertson
  • 599
  • 1
  • 7
  • 18
  • 1
    In Unix, an environment variable that is changed in a script or compiled program will only affect that process and possibly child processes. The parent process and any unrelated processes will not be affected. – OMar Mohamed Feb 15 '18 at 19:22
  • I thought exporting variables made them available to other processes? Is there a way to save an environmental variable such that it can be accessed by other processes? – Cole Robertson Feb 15 '18 at 19:24
  • 1
    Exporting variables makes them available to **child processes**. The parent process and any unrelated processes will not be affected. – OMar Mohamed Feb 15 '18 at 19:27

1 Answers1

6

If you are running Python in IDLE, I suspect you don't start IDLE from the shell for which you've defined your new_v. Hence, IDLE does not know about the variable, and neither can Python.

If you execute python in your shell and type

os.getenv("new_v")

you will see your variable. I'm not a Mac user, so I don't know myself how to set env vars in macOS, but this post Setting environment variables in OS X? seems to have an answer :)

dnswlt
  • 2,925
  • 19
  • 15