使用 sudo 无法使用 Python 访问环境变量
Environment variable not accessible with Python with sudo
我的 python 脚本有问题
首先我定义了一个环境变量为
export TEST=test
我的Python脚本很简单"test.py"
import os
print os.environ['TEST']
所以当我 运行 它与
~ $ python test.py
我已经打印出预期结果 test
。但是,如果我 运行 脚本
~ $ sudo python test.py
我有一个 KeyError: 'TEST'
错误。
我错过了什么?
Sudo 在不同的环境下运行。
要保持当前环境,请使用 -E
标志。
sudo -E python test.py
-E, --preserve-env
Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to
preserve the environment.
我的 python 脚本有问题
首先我定义了一个环境变量为
export TEST=test
我的Python脚本很简单"test.py"
import os
print os.environ['TEST']
所以当我 运行 它与
~ $ python test.py
我已经打印出预期结果 test
。但是,如果我 运行 脚本
~ $ sudo python test.py
我有一个 KeyError: 'TEST'
错误。
我错过了什么?
Sudo 在不同的环境下运行。
要保持当前环境,请使用 -E
标志。
sudo -E python test.py
-E, --preserve-env
Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to
preserve the environment.