flask - 在cmd中设置了环境变量,进入python后失效如何解决?
ringa_lee
ringa_lee 2017-04-17 17:27:10
0
4
511

在自学flask-mail,其中一步是从环境变量中获取邮箱帐号和密码,后来发现无法获取,在venv环境下,cmd测试如下:

set MAIL_USERNAME = 'XXXX'
set MAIL_USERNAME

会显示XXXX,表明设置成功,然后我在cmd里输入

python hello.py shell
import os
print(os.environ.get('MAIL_USERNAME'))

显示None,表明MAIL_USERNAME为空

后来通过查询,这其实是在读取系统的环境变量。而set的方法(linux下是export)是设置临时环境变量,cmd(或bash)一关闭就会丢失。但临时变量在linux似乎是允许不同的程序使用,只是bash之间相互独立,而在win下用set设置的“临时变量”,一进入python shell就用不了了,所以应该叫“普通环境变量”而不是“临时环境变量”。然而,我在系统里设置了永久的环境变量,用os.environ.get()依然无法访问到。很想知道为什么,求教,谢谢!

ringa_lee
ringa_lee

ringa_lee

reply all(4)
黄舟

You should first understand that variables and environment variables are not the same thing.
Variables set by set will not take effect on other processes.

python os.putenv() I don’t know why it doesn’t take effect.
I saw an article on the Internet that suggested changing it directly

 os.environ['aaaa']='test'
 os.environ['aaaa']
'test'

os.environ also creates temporary variables.

batch-cn is a good command tool package manager. Produced by bathome, I recommend it.

Download third-party command tools and use these tools to modify system variables, setx.ezxe, conset.exe
http://batch-cn.qiniudn.com/tool/setx.exe

One is to batch change the registry and restart, and the other is to batch adjust WMIC. Search for it yourself.

The above three methods should be commonly used.

Rapid Environment Editor is a very good environment variable management tool.
Backup, import and export, and you can also modify environment variables through the command line.

伊谢尔伦

You have to manually set it in My Computer-Environment Variables to make it effective. This is global. The set mode setting is only valid for the current command window. It will disappear when you close the command window. This setting has scope.

阿神

Find your ~/.vimrc, modify it, and then source it. Specifically, under Baidu~~set only takes effect temporarily on the current window

大家讲道理

The names are the same? It looks like the names in your question are different

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!