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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:43:21  来源:igfitidea点击:

python - os.getenv and os.environ don't see environment variables of my bash shell

pythonbashenvironment-variablespythonpath

提问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 PATHvariable:

但是对于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=5command; when you use $ export B="kkk"everything is fine.

我用普通$ A=5命令设置变量;当你使用$ export B="kkk"一切都很好。

That is becauseexportmakes the variable available to sub-processes:

这是贝科使用export,使变量提供给子流程:

  • it creates a variable in the shell
  • and exportsit into the environmentof the shell
  • the list environmentis 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 environmentfrom the parent -- the shell. So really the variable should be exported into the environmentbefore.

从 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 exportor declare -x. See the bash documentation on environment.

这些变量(bash 术语中的参数)不是环境变量。您想使用export或将它们导出到环境中declare -x。请参阅有关 environmentbash 文档