python - os.getenv 和 os.environ 看不到我的 bash shell 的环境变量

2023-07-04Python开发问题
144

本文介绍了python - os.getenv 和 os.environ 看不到我的 bash shell 的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 ubuntu 13.04,bash,python2.7.4

I am on ubuntu 13.04, bash, python2.7.4

解释器看不到我设置的变量.

这是一个例子:

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

但是 PATH 变量一切正常:

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

它会注意到 PATH 的变化:

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

可能出了什么问题?

PS 使用 $PYTHONPATH 时出现问题:

PS the problem comes when using $PYTHONPATH:

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

推荐答案

啊哈!解决方法很简单!

Aha! the solution is simple!

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

I was setting variables with plain $ A=5 command; when you use $ export B="foo" everything is fine.

那是 because export 使变量可用于子进程:

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

  • 它在 shell 中创建一个变量
  • 导出到shell环境中
  • 环境被传递给 shell 的子进程.

Plain $ A="foo" 只是在 shell 中创建变量,不对环境做任何事情.

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

从 shell 调用的解释器从父 shell 获取它的环境.所以真的应该先将变量导出到环境中.

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.

这篇关于python - os.getenv 和 os.environ 看不到我的 bash shell 的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11