管理 Python 虚拟环境的 requirements.txt 的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39406177/
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
Managing contents of requirements.txt for a Python virtual environment
提问by Amistad
So I am creating a brand new Flask app from scratch. As all good developers do, my first step was to create a virtual environment.
所以我正在从头开始创建一个全新的 Flask 应用程序。正如所有优秀的开发人员所做的那样,我的第一步是创建一个虚拟环境。
The first thing I install in the virtual environment is Flask==0.11.1
. Flask installs its following dependencies:
我在虚拟环境中安装的第一件事是Flask==0.11.1
. Flask 安装其以下依赖项:
- click==6.6
- itsdangerous==0.24
- Jinja2==2.8
- MarkupSafe==0.23
- Werkzeug==0.11.11
- wheel==0.24.0
- 点击==6.6
- 它的危险==0.24
- Jinja2==2.8
- 标记安全==0.23
- Werkzeug==0.11.11
- 轮==0.24.0
Now, I create a requirements.txtto ensure everyone cloning the repository has the same version of the libraries. However, my dilemma is this:
现在,我创建了一个requirements.txt以确保克隆存储库的每个人都拥有相同版本的库。但是,我的困境是这样的:
- Do I mention each of the Flask dependencies in the requirements.txtalong with the version numbers OR
- Do I just mention the exact Flask version number in the requirements.txtand hope that when they do a pip install requirements.txt, Flask will take care of the dependency management and they will download the right versions of the dependent libraries
- 我是否在requirements.txt 中提及每个 Flask 依赖项以及版本号或
- 我是否只是在requirements.txt 中提到确切的 Flask 版本号,并希望当他们执行pip install requirements.txt 时,Flask 将负责依赖管理,他们将下载正确版本的依赖库
采纳答案by Klaus D.
Both approaches are valid and work. But there is a little difference. When you enter all the dependencies in the requirements.txt
you will be able to pin the versions of them. If you leave them out, there might be a later update and if Flask has something like Werkzeug>=0.11
in its dependencies, you will get a newer version of Werkzeug installed.
这两种方法都是有效的并且有效。但有一点不同。当您在 中输入所有依赖项时,requirements.txt
您将能够固定它们的版本。如果你忽略它们,可能会有一个更新,如果 FlaskWerkzeug>=0.11
在它的依赖项中有类似的东西,你将安装一个较新版本的 Werkzeug。
So it comes down to updates vs. defined environment. Whatever suits you better.
所以它归结为更新与定义的环境。哪个更适合你。
回答by Vishvajit Pathak
One good thing here is you are using virtualenv, which will make your task very easy.
这里的一件好事是您正在使用 virtualenv,这将使您的任务变得非常容易。
- Activate virtualenv (
$source path_to_virtualenv/bin/active
) - Go to your project root directory
Get all the packages along with dependencies in requirements.txt
pip freeze > requirements.txt
You dont have to worry anything else apart from making sure next person installs the requirements recursively by following command
pip install -r requirements.txt
- 激活 virtualenv (
$source path_to_virtualenv/bin/active
) - 转到您的项目根目录
在 requirements.txt 中获取所有包以及依赖项
pip freeze > requirements.txt
除了确保下一个人通过以下命令递归安装要求之外,您不必担心其他任何事情
pip install -r requirements.txt
回答by hjpotter92
You can (from your active virtual environment) do the following
您可以(从您的活动虚拟环境)执行以下操作
pip freeze > requirements.txt
which'll automatically take care of all libraries/modules available in your project.
这将自动处理您项目中可用的所有库/模块。
The next developer would only have to issue:
下一个开发人员只需发布:
pip install -r requirements.txt
回答by Deke
If you only want to see what packages you have installed then just do pip freeze
.
如果您只想查看已安装的软件包,则只需执行pip freeze
.
but if you want all these packages in your requirement.txt, then do
pip freeze > requirements.txt
但是如果你想要在你的requirements.txt中的所有这些包,那么做
pip freeze > requirements.txt