Skip to content Skip to sidebar Skip to footer

Python - Os.getenv And Os.environ Don't See Environment Variables Of My Bash Shell

I am on ubuntu 13.04, bash, python2.7.4 The interpreter doesn't see variables I set. Here is an example: $ echo $A 5 $ python -c 'import os; print os.getenv( 'A' )' None $ python -

Solution 1:

Aha! the solution is simple!

I was setting variables with plain $ A=5 command; when you use $ export B="foo" everything is fine.

That is becauseexport makes the variable available to sub-processes:

  • it creates a variable in the shell
  • and exports it into the environment of the shell
  • the environment is passed to sub-processes of the shell.

Plain $ A="foo" just creates variables in the shell and doesn't do anything with the environment.

The interpreter called from the shell obtains its environment from the parent -- the shell. So really the variable should be exported into the environment before.

Solution 2:

Those variables (parameters in bash terminology) are not environment variables. You want to export them into the environment, using export or declare -x. See the bash documentation on environment.

Post a Comment for "Python - Os.getenv And Os.environ Don't See Environment Variables Of My Bash Shell"