[英]Python can't read the environment variable
I used Ubuntu16.04我用的是 Ubuntu16.04
When I use sudo python3
into the interactive shell当我在交互式 shell 中使用
sudo python3
import os
os.environ['SECRET_KEY']
it can't read the SECRET_KEY它无法读取 SECRET_KEY
I set it in the /etc/profile.d/project.sh我将它设置在 /etc/profile.d/project.sh
project.sh项目.sh
export SECRET_KEY=the_key
After reloading it but in vain.重装后,但徒劳无功。
I also set in the ~/.profile我也在 ~/.profile 中设置
But still got the error.但仍然得到错误。
How do I fixed it?我该如何解决?
[ EDIT ] [编辑]
It can work in sudo -E python3
它可以在
sudo -E python3
But the futher problem is that when I run sudo -E supervisor reread/reload
但进一步的问题是,当我运行
sudo -E supervisor reread/reload
I have the command defined in the [program:site] section to launch the gunicorn.conf.py我在 [program:site] 部分定义了命令来启动 gunicorn.conf.py
/etc/supervisor/conf.d/weather.conf /etc/supervisor/conf.d/weather.conf
[program:site]
directory=/home/nhcc/campus-weather-station/weather_station
command=/home/nhcc/venv/weather_station/bin/gunicorn -c /home/nhcc/campus-weather-station/weather_station/gunicorn.conf.py -p gunicorn.pod weather_station.wsgi
gunicorn.conf.py gunicorn.conf.py
# -*- coding: utf-8 -*-
# /usr/bin/python3
import os
bind = "{}:8080".format(os.environ['DJANGO_WEATHER_STATION_HOST'])
worders = (os.sysconf('SC_NPROCESSORS_ONLN') * 2) + 1
loglevel = 'error'
command = "WTR_VENV/gunicorn"
pythonpath = "$PROJECT/weather_station"
it will showed up the error .它会显示错误。
When you run it with sudo
, you're actually going to run the program as root
.当您使用
sudo
运行它时,您实际上将以root
身份运行该程序。 So any environment setup for your current user isn't going to apply unfortunately without special care.因此,不幸的是,如果没有特别注意,您当前用户的任何环境设置都不会适用。
Fortunately sudo -E
will do the trick.幸运的是
sudo -E
可以解决问题。 See this SO question for more details.有关更多详细信息,请参阅此 SO 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.