Python 如何为多个环境自定义一个requirements.txt?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17803829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize a requirements.txt for multiple environments?
提问by Charles R
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'.
我有两个分支,开发和生产。每个都有依赖关系,其中一些是不同的。开发指向本身正在开发中的依赖项。生产也是如此。我需要部署到 Heroku,它期望在一个名为“requirements.txt”的文件中包含每个分支的依赖项。
What is the best way to organize?
什么是最好的组织方式?
What I've thought of:
我想到的:
- Maintain separate requirements files, one in each branch (must survive frequent merges!)
- Tell Heroku which requirements file I want to use (environment variable?)
- Write deploy scripts (create temp branch, modify requirements file, commit, deploy, delete temp branch)
- 维护单独的需求文件,每个分支一个(必须在频繁的合并中幸存下来!)
- 告诉 Heroku 我想使用哪个需求文件(环境变量?)
- 编写部署脚本(创建临时分支、修改需求文件、提交、部署、删除临时分支)
回答by Christian Abbott
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:
您可以级联您的需求文件并使用“-r”标志告诉 pip 将一个文件的内容包含在另一个文件中。您可以将您的需求分解为模块化文件夹层次结构,如下所示:
`-- django_project_root
|-- requirements
| |-- common.txt
| |-- dev.txt
| `-- prod.txt
`-- requirements.txt
The files' contents would look like this:
文件的内容如下所示:
common.txt:
常见的.txt:
# Contains requirements common to all environments
req1==1.0
req2==1.0
req3==1.0
...
dev.txt:
开发.txt:
# Specifies only dev-specific requirements
# But imports the common ones too
-r common.txt
dev_req==1.0
...
prod.txt:
产品.txt:
# Same for prod...
-r common.txt
prod_req==1.0
...
Outside of Heroku, you can now setup environments like this:
在 Heroku 之外,您现在可以设置如下环境:
pip install -r requirements/dev.txt
or
或者
pip install -r requirements/prod.txt
Since Heroku looks specifically for "requirements.txt" at the project root, it should just mirror prod, like this:
由于 Heroku 在项目根目录中专门查找“requirements.txt”,因此它应该只是镜像 prod,如下所示:
requirements.txt:
要求.txt:
# Mirrors prod
-r requirements/prod.txt
回答by Christian Abbott
A viable option today which didn't exist when the original question and answer was posted is to use pipenvinstead of pip to manage dependencies.
今天发布原始问题和答案时不存在的可行选项是使用pipenv而不是 pip 来管理依赖项。
With pipenv, manually managing two separate requirement files like with pip is no longer necessary, and instead pipenv manages the development and production packages itself via interactions on the command line.
使用 pipenv,不再需要像 pip 那样手动管理两个单独的需求文件,而是 pipenv 通过命令行上的交互来管理开发和生产包本身。
To install a package for use in both production and development:
要安装用于生产和开发的软件包:
pipenv install <package>
To install a package for the development environment only:
要仅为开发环境安装软件包:
pipenv install <package> --dev
Via those commands, pipenv stores and manages the environment configuration in two files (Pipfile and Pipfile.lock). Heroku's current Python buildpack natively supports pipenv and will configure itself from Pipfile.lock if it exists instead of requirements.txt.
通过这些命令,pipenv 在两个文件(Pipfile 和 Pipfile.lock)中存储和管理环境配置。Heroku 当前的 Python buildpack 本身就支持 pipenv,并且如果它存在而不是 requirements.txt,则会从 Pipfile.lock 配置自身。
See the pipenv link for full documentation of the tool.
有关该工具的完整文档,请参阅 pipenv 链接。
回答by nurettin
If your requirement is to be able to switch between environments on the same machine, it may be necessary to create different virtualenv folders for each environment you need to switch to.
如果您的要求是能够在同一台机器上的环境之间切换,则可能需要为您需要切换到的每个环境创建不同的 virtualenv 文件夹。
python3 -m venv venv_dev
source venv_dev/bin/activate
pip install -r pip/common.txt
pip install -r pip/dev.txt
exit
python3 -m venv venv_prod
source venv_prod/bin/activate
pip install -r pip/common.txt
exit
source venv_dev/bin/activate
# now we are in dev environment so your code editor and build systems will work.
# let's install a new dev package:
# pip install awesome
# pip freeze -r pip/temp.txt
# find that package, put it into pip/dev.txt
# rm pip/temp.txt
# pretty cumbersome, but it works.