Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 -c 'import os; print os.environ[ "A" ]'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'A'

But everything works fine with the PATH variable:

$ echo $PATH 
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ python -c 'import os; print os.getenv("PATH")'
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

And it notices changes in PATH:

$ PATH="/home/alex/tests/:$PATH"
$ echo $PATH 
/home/alex/tests/:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ python -c 'import os; print os.getenv("PATH")'
/home/alex/tests/:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

What could be wrong?

PS the problem comes when using $PYTHONPATH:

$ python -c 'import os; print os.getenv("PYTHONPATH")'
None
like image 410
xealits Avatar asked Sep 28 '13 19:09

xealits


People also ask

How do I set environ variables in Python os?

To set and get environment variables in Python you can just use the os module: import os # Set environment variables os. environ['API_USER'] = 'username' os. environ['API_PASSWORD'] = 'secret' # Get environment variables USER = os.

How do I view environment variables in Python?

The os. getenv() method is used to extract the value of the environment variable key if it exists. Otherwise, the default value will be returned. Note: The os module in Python provides an interface to interact with the operating system.

How do I set environment variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.


2 Answers

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 because export 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.

like image 200
xealits Avatar answered Oct 21 '22 20:10

xealits


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.

like image 9
Yann Vernier Avatar answered Oct 21 '22 20:10

Yann Vernier