<p>我相信在构建requirements.txt时,使用来自<a href="https://github.com/jazzband/pip-tools" rel="noreferrer">pip-tools</a>的<code>pip-compile</code>是一个很好的实践。这将确保构建是可预测和确定的</p>
<p>使用<code>pip-compile</code>命令可以从依赖项编译<code>requirements.txt</code>文件,该文件在<code>setup.py</code>或<code>requirements.in</code>中指定</p>
<p>以下是我建议的构建requirements.txt的步骤(如果使用requirements.in):</p>
<ol>
<li>创建虚拟环境并在那里安装pip工具</li>
</ol>
<pre><code>$ source /path/to/venv/bin/activate
(venv)$ python -m pip install pip-tools
</code></pre>
<ol start=“2”>
<li>指定应用程序/项目的直接依赖项以满足您的需求。在文件中:</li>
</ol>
<pre><code># requirements.in
requests
boto3==1.16.51
</code></pre>
<ol start=“3”>
<li>使用pip编译生成requirements.txt</li>
</ol>
<pre><code>$ pip-compile output-file=- > requirements.txt
</code></pre>
<p>您的requirements.txt文件将包含:</p>
<pre><code>#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile output-file=-
#
boto3==1.16.51
# via -r requirements.in
botocore==1.19.51
# via
# boto3
# s3transfer
certifi==2020.12.5
# via requests
chardet==4.0.0
# via requests
idna==2.10
# via requests
jmespath==0.10.0
# via
# boto3
# botocore
python-dateutil==2.8.1
# via botocore
requests==2.25.1
# via -r requirements.in
s3transfer==0.3.3
# via boto3
six==1.15.0
# via python-dateutil
urllib3==1.26.2
# via
# botocore
# requests
</code></pre>
<p>您的应用程序应该始终使用从该生成的<code>requirements.txt</code>安装的依赖项。如果必须更新依赖项,则只需更新<code>requirements.in</code>文件并重做<code>pip-compile</code>。我相信这是一个比我看到有些人做的更好的方法</p>
<p>我想使用它的主要优点是,您可以在一个单独的<code>requirement.in</code>文件中跟踪项目的实际直接依赖关系</p>
<p>我发现这与使用<code>package.json</code>(requirements.in)和<code>package-lock.json</code>(requirements.txt)在节点应用程序项目中管理节点模块/依赖项的方式非常相似</p>