6 Things I Wish I Knew Earlier About requirements.txt
<h1>1) How to install everything inside requirements.txt</h1>
<p><code>requirements.txt</code> contains the required libraries for a project that needs to be installed (usually using pip). A simple example:</p>
<pre>
library-one==1.0.1
library-two==3.1.4
library-three==100.1.0</pre>
<p>To install all 3 libraries, we can run the command:</p>
<pre>
pip install -r requirements.txt</pre>
<p>which will install every single line inside <code>requirements.txt</code></p>
<h1>2) How to generate requirements.txt</h1>
<pre>
pip freeze</pre>
<p>This command prints out in the terminal 1) every external module you have installed in whatever Python you are using 2) their exact versions. Something like this will print in the terminal (truncated):</p>
<pre>
absl-py==1.4.0
altair==5.0.1
anyio==3.6.2
appdirs==1.4.4
appnope==0.1.3
argon2-cffi==21.3.0</pre>
<p>This is essentially the stuff inside a <code>requirements.txt</code> file. Since we want these lines in a text file, but don’t want to manually copy paste it, let’s use the following command to write it to a file.</p>
<pre>
pip freeze > requirements.txt</pre>
<p>^ whatever output from <code>pip freeze</code> will be written to a text file with the name <code>requirements.txt</code>. And there we have it — how to generate a <code>requirements.txt</code> file!</p>
<h1>3) It is better to use a Python virtual environment to create a requirements.txt</h1>
<p>When we <code>pip freeze</code>, EVERY SINGLE installed library will appear in your <code>requirements.txt</code>. Which can get annoying — what if your project simply needs <code>numpy</code> and <code>pandas</code>, but <code>requirements.txt</code> will install another 100 unnecessary libraries.</p>
<p>When we use a Python virtual environment, we create a new instance of Python that is separate from your main Python.</p>
<p><a href="https://levelup.gitconnected.com/6-things-i-wish-i-knew-earlier-about-requirements-txt-1f89b689f362">Read More</a></p>