python - os。采用和操作系统。环境看不到bash shell的环境变量

时间:2021-11-18 22:44:20

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:

但是对于路径变量,一切都没问题

$ 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:

PS问题出现在使用$PYTHONPATH:

$ python -c 'import os; print os.getenv("PYTHONPATH")'
None

2 个解决方案

#1


59  

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=" kkkk "时,一切正常。

That is because export makes the variable available to sub-processes:

这是因为export使子流程可以使用变量:

  • it creates a variable in the shell
  • 它在shell中创建一个变量
  • and exports it into the environment of the shell
  • 并将其出口到壳牌的环境中。
  • the list environment is passed to sub-processes of the shell.
  • 列表环境被传递给shell的子进程。

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

普通的$ A="kkk"只在shell中创建变量,不处理环境。

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调用的解释器从父shell获取环境。所以变量应该被导出到环境中。

#2


4  

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.

这些变量(bash术语中的参数)不是环境变量。您希望使用export或declare -x将它们导出到环境中。参见关于环境的bash文档。

#1


59  

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=" kkkk "时,一切正常。

That is because export makes the variable available to sub-processes:

这是因为export使子流程可以使用变量:

  • it creates a variable in the shell
  • 它在shell中创建一个变量
  • and exports it into the environment of the shell
  • 并将其出口到壳牌的环境中。
  • the list environment is passed to sub-processes of the shell.
  • 列表环境被传递给shell的子进程。

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

普通的$ A="kkk"只在shell中创建变量,不处理环境。

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调用的解释器从父shell获取环境。所以变量应该被导出到环境中。

#2


4  

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.

这些变量(bash术语中的参数)不是环境变量。您希望使用export或declare -x将它们导出到环境中。参见关于环境的bash文档。