现在我正在使用virtualenv
,只是切换到Pipenv
。今天在virtualenv我加载不同的环境变量和设置取决于我是在development
,production
,或testing
by设置DJANGO_SETTINGS_MODULE
到myproject.settings.development
,myproject.settings.production
和myproject.settings.testing
。
我知道我可以设置一个.env
文件,但是如何才能拥有该.env
文件的多个版本?
您应该根据环境创建具有不同前缀的不同.env
文件,例如production.env
或testing.env
。使用pipenv
,您可以使用PIPENV_DONT_LOAD_ENV=1
环境变量来阻止pipenv shell
自动导出.env
文件并将其与export $(cat .env | xargs)
结合使用。
export $(cat production.env | xargs) && PIPENV_DONT_LOAD_ENV=1 pipenv shell
将为生产配置环境变量,然后在虚拟环境中启动shell。
我远不是Python大师,但我能想到的一个解决方案是创建运行shell脚本以更改Pipenv scripts并运行启动命令的PIPENV_DOTENV_LOCATION
。
示例Pipfile脚本:
[scripts]
development = "./scripts/development.sh"
development.sh示例:
#!/bin/sh
PIPENV_DOTENV_LOCATION=/path/to/.development_env pipenv run python test.py
然后运行pipenv run development