python - os.getenv 和 os.environ 看不到我的 bash shell 的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19070615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python - os.getenv and os.environ don't see environment variables of my bash shell
提问by xealits
I am on ubuntu 13.04, bash, python2.7.4
我在 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:
但是对于PATH
变量一切正常:
$ 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
:
$ 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
:
PS使用时出现问题$PYTHONPATH
:
$ python -c 'import os; print os.getenv("PYTHONPATH")'
None
采纳答案by xealits
Aha! the solution is simple!
啊哈!解决办法很简单!
I was setting variables with plain $ A=5
command; when you use $ export B="kkk"
everything is fine.
我用普通$ A=5
命令设置变量;当你使用$ export B="kkk"
一切都很好。
That is becauseexport
makes the variable available to sub-processes:
- it creates a variable in the shell
- and exportsit into the
environment
of the shell - the list
environment
is passed to sub-processes of the shell.
- 它在shell中创建一个变量
- 并将其导出到
environment
外壳的 - 该列表
environment
被传递给 shell 的子进程。
Plain $ A="kkk"
just creates variables in the shell and doesn't do anything with the environment
.
Plain$ A="kkk"
只是在 shell 中创建变量,对environment
.
The interpreter called from the shell obtains it's environment
from the parent -- the shell. So really the variable should be exported into the environment
before.
从 shell 调用的解释器从environment
父级——shell 获取它。所以真的应该将变量导出到environment
之前。
回答by Yann Vernier
Those variables (parameters in bash terminology) are not environmentvariables. You want to export them into the environment, using export
or declare -x
. See the bash documentation on environment.
这些变量(bash 术语中的参数)不是环境变量。您想使用export
或将它们导出到环境中declare -x
。请参阅有关 environment的bash 文档。