Pipenv:多种环境

问题描述 投票:2回答:2

现在我正在使用virtualenv,只是切换到Pipenv。今天在virtualenv我加载不同的环境变量和设置取决于我是在developmentproduction,或testingby设置DJANGO_SETTINGS_MODULEmyproject.settings.developmentmyproject.settings.productionmyproject.settings.testing

我知道我可以设置一个.env文件,但是如何才能拥有该.env文件的多个版本?

python virtualenv pipenv
2个回答
2
投票

您应该根据环境创建具有不同前缀的不同.env文件,例如production.envtesting.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。


2
投票

我远不是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

© www.soinside.com 2019 - 2024. All rights reserved.