search

Home  >  Q&A  >  body text

python - 用os.environ能获取到的环境变量,在os.environ.getenv却得不到,这是为什么?

>>> import os
>>> os.path.exists('.env')
True
>>> for line in open('.env'):
...     var = line.strip().split('=')
...     print var
...
['FLASK_CONFIG ', ' production']
>>> if len(var) ==2:
...     os.environ[var[0]] = var[1]
...
>>> print var
['FLASK_CONFIG ', ' production']
>>> print os.environ
{'TERM': 'xterm', 'SHELL': '/bin/bash', 
'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'SHLVL': '1', 'SSH_TTY': '/dev/pts/0', 'OLDPWD': '/root', 
'FLASK_CONFIG ': ' production', # 在这里
'PWD': '/home/Xu',  '/usr/bin/python', 
}
>>> print os.getenv('FLASK_CONFIG')
None
>>> os.environ.get('FLASK_CONFIG')
>>> print os.environ.get('FLASK_CONFIG')
None

用os.environ能获取到的FLASK_CONFIG,在os.environ.getenv却得不到这是为什么?怎么办?

阿神阿神2777 days ago988

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:23:07

    Look at the code you posted: ['FLASK_CONFIG ', ' production'],引号内似乎多了空格?然后取值时:print os.getenv('FLASK_CONFIG')There are no spaces.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:23:07

    There is a space missing 0.0

    reply
    0
  • Cancelreply