最近由于conda涉及版权问题,更换成使用micromamba配置python环境
由于vscode对micromamba支持不佳
曾经使用conda配置环境时,只需要在项目中与.vscode同级文件夹内创建一个.env文件,然后使用run and debug即可自动读取所有环境变量
project
├── .vscode/
├── .env
└── .gitignore
但使用micromamba之后,经常发送无法读取环境变量的问题
首先,需要在项目的config/init.py文件中放置环境变量,因为这是项目启动时最先读取的。
然后再config/dev.py文件中修改DATABASE和RabbitMq相关配置
最后,init.py中有一个读取环境变量的默认方法需要修改
def get_env_or_raise(key):
"""Get an environment variable, if it does not exist, raise an exception
"""
value = os.environ.get(key)
if not value:
raise RuntimeError(
('Environment variable "{}" not found, you must set this variable to run this application.').format(key)
)
return value
这样无法读取本地的环境变量,需要使用globals
def get_env_or_raise(key):
"""Get an environment variable, if it does not exist, raise an exception
"""
value = os.environ.get(key)
if not value:
value = globals()[key]
if not value:
raise RuntimeError(
('Environment variable "{}" not found, you must set this variable to run this application.').format(key)
)
return value