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?
Asked
Active
Viewed 3,861 times
2

Cole Robertson
- 599
- 1
- 7
- 18
-
1In 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
-
1Exporting 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 Answers
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