如何为多个环境自定义一个requirements.txt? [英] How to customize a requirements.txt for multiple environments?
问题描述
我有两个分支,开发和生产.每个都有依赖关系,其中一些是不同的.开发指向本身正在开发中的依赖项.生产也是如此.我需要部署到 Heroku,它期望在名为requirements.txt"的单个文件中包含每个分支的依赖项.
I have two branches, Development and Production. Each has dependencies, some of which are different. Development points to dependencies that are themselves in development. Likewise for Production. I need to deploy to Heroku which expects each branch's dependencies in a single file called 'requirements.txt'.
最好的组织方式是什么?
What is the best way to organize?
我的想法:
- 维护单独的需求文件,每个分支一个(必须在频繁的合并中幸存下来!)
- 告诉 Heroku 我想使用哪个需求文件(环境变量?)
- 编写部署脚本(创建临时分支、修改需求文件、提交、部署、删除临时分支)
推荐答案
您可以级联您的需求文件并使用-r"标志告诉 pip 将一个文件的内容包含在另一个文件中.您可以将您的需求分解为模块化文件夹层次结构,如下所示:
You can cascade your requirements files and use the "-r" flag to tell pip to include the contents of one file inside another. You can break out your requirements into a modular folder hierarchy like this:
`-- django_project_root
|-- requirements
| |-- common.txt
| |-- dev.txt
| `-- prod.txt
`-- requirements.txt
文件内容如下所示:
common.txt:
common.txt:
# Contains requirements common to all environments
req1==1.0
req2==1.0
req3==1.0
...
dev.txt:
# Specifies only dev-specific requirements
# But imports the common ones too
-r common.txt
dev_req==1.0
...
prod.txt:
# Same for prod...
-r common.txt
prod_req==1.0
...
在 Heroku 之外,您现在可以设置如下环境:
Outside of Heroku, you can now setup environments like this:
pip install -r requirements/dev.txt
或
pip install -r requirements/prod.txt
由于 Heroku 在项目根目录中专门查找requirements.txt",因此它应该只是镜像 prod,如下所示:
Since Heroku looks specifically for "requirements.txt" at the project root, it should just mirror prod, like this:
requirements.txt:
requirements.txt:
# Mirrors prod
-r requirements/prod.txt
这篇关于如何为多个环境自定义一个requirements.txt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!