python - os.getenv and os.environ don't see environment variables of my bash shell
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 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.
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.
xealits
Updated on July 09, 2022Comments
-
xealits almost 2 years
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
-
xealits over 10 yearshey! terminology is vague: docs.python.org/2/using/cmdline.html#environment-variables
-
Yann Vernier over 10 yearsNot sure which term you're complaining about, but the issue lies with bash. Python has no control over which variables bash puts in the environment.
-
xealits over 10 yearsI mean that if it is a "parameter" or "variable" -- doesn't matter to me. I care about setting
PYTHONPATH
to work. Though, I see your point -- you mean that assignment$ A=5
doesn't make an environment variable at all. There is a list of variables in the shell, called "environment". It is passed to sub-processes. Andexport
declares some variables in that list (exports them into it). Plain assignment doesn't create variables in that list -- it puts them somewhere else. I'll correct my answer according to all of this. -
Charles Duffy over 2 years
environment
is not code, so it should not be code-formatted.