<blockquote>
<p>Any way to do this without manually re-installing the packages in a new virtualenv to get their dependencies ? This would be error-prone and I'd like to automate the process of cleaning the virtualenv from no-longer-needed old dependencies.</p>
</blockquote>
<p>这就是<em>pip tools</em>包的用途(from<a href="https://github.com/jazzband/pip-tools" rel="nofollow noreferrer">https://github.com/jazzband/pip-tools</a>):</p>
<h2>安装</h2>
<pre><code>$ pip install --upgrade pip # pip-tools needs pip==6.1 or higher (!)
$ pip install pip-tools
</code></pre>
<h2>pip编译的示例用法</h2>
<p>假设您有一个烧瓶项目,并希望将其固定以供生产。将以下行写入文件:</p>
<pre><code># requirements.in
Flask
</code></pre>
<p>现在,运行pip compile requirements.in:</p>
<pre><code>$ pip-compile requirements.in
#
# This file is autogenerated by pip-compile
# Make changes in requirements.in, then run this to update:
#
# pip-compile requirements.in
#
flask==0.10.1
itsdangerous==0.24 # via flask
jinja2==2.7.3 # via flask
markupsafe==0.23 # via jinja2
werkzeug==0.10.4 # via flask
</code></pre>
<p>它将生成您的<code>requirements.txt</code>,并固定所有烧瓶依赖项(和所有底层依赖项)。将此文件置于版本控制之下,并定期重新运行<code>pip-compile</code>以更新包。</p>
<h2>pip sync的示例用法</h2>
<p>现在您已经有了一个<code>requirements.txt</code>,您可以使用<code>pip-sync</code>来更新您的虚拟环境,以准确反映其中的内容。注意:这将安装/升级/卸载与<code>requirements.txt</code>内容匹配所需的所有内容。</p>
<pre><code>$ pip-sync
Uninstalling flake8-2.4.1:
Successfully uninstalled flake8-2.4.1
Collecting click==4.1
Downloading click-4.1-py2.py3-none-any.whl (62kB)
100% |████████████████████████████████| 65kB 1.8MB/s
Found existing installation: click 4.0
Uninstalling click-4.0:
Successfully uninstalled click-4.0
Successfully installed click-4.1
</code></pre>