title: How can I optimize my requirements.txt to include only the packages my application uses directly?
body: <p>Say for example I add django to my venv with <code>pip install django</code>. The usual way to generate my requirements.txt file is with <code>pip freeze &gt; requirements.txt</code> which produces:</p>
  
  <pre><code>asgiref==3.2.3
  Django==3.0.2
  pytz==2019.3
  sqlparse==0.3.0
  </code></pre>
  
  <p>This is a lie. My only requirement is django and the (perfectly effective) requirements.txt I want is:</p>
  
  <pre><code>Django&gt;=3.0.2, &lt;4
  </code></pre>
  
  <p>There are times when this matters. With a real application I will end up with many packages that have overlapping dependencies. When upgrading this may or may not cause a problem but there will almost always be problems if I've pinned all those dependent packages I don't care about (in the example above, they are up to the django developers, not me).</p>
  
  <p>In order to optimize a dependency bloated requirements.txt file I can manually inspect the source code (resulting in an empty requirements.txt above ;)) but for any real application this quickly becomes impractical.</p>