avatarLiu Zuo Lin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3026

Abstract

aste it, let’s use the following command to write it to a file.</p><div id="b82a"><pre>pip freeze > requirements.<span class="hljs-property">txt</span></pre></div><p id="2ea4">^ 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 id="3c27">3) It is better to use a Python virtual environment to create a requirements.txt</h1><p id="e7e7">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 id="769f">When we use a Python virtual environment, we create a new instance of Python that is separate from your main Python.</p><div id="17e2"><pre>python -m venv <span class="hljs-built_in">env</span> <span class="hljs-comment"># create a virtual environment called 'env'</span></pre></div><div id="206c"><pre><span class="hljs-comment"># activating our Python virtual environment</span>

<span class="hljs-built_in">env</span>\Scripts\activate.bat <span class="hljs-comment"># Windows</span>

<span class="hljs-built_in">source</span> <span class="hljs-built_in">env</span>\bin\activate <span class="hljs-comment"># MacOS/Linux</span></pre></div><p id="3649">After we create a new virtual environment, it has NOTHING installed. So we need to install everything we need for our project (only once). And when we <code>pip freeze</code>, the stuff that ends up in our <code>requirements.txt</code> will contain only the stuff we installed for the project.</p><h1 id="bcea">4) We can leave out the version in requirements.txt</h1><p id="cf42">Here’s a normal <code>requirements.txt</code></p><div id="c2aa"><pre><span class="hljs-attribute">numpy</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-number">1.23</span>.<span class="hljs-number">5</span> <span class="hljs-attribute">pandas</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-number">1.5</span>.<span class="hljs-number">2</span></pre></div><ul><li><code>numpy</code> with a specific version <code>1.23.5</code> will be installed</li><li><code>pandas</code> with a specific version <code>1.5.2</code> will be installed</li></ul><p id="682e">Now, here’s a <code>requirements.txt</code> without the versions</p><div id="b744"><pre>numpy pandas</pre></div><ul><li><code>numpy</code> with the latest version will be installed</li><li><code>pandas</code> with the latest veersion will be installed</li></ul><p id="a472">We don’t necessarily need to include the version all the time.</p><h1 id="fe80">5) It doesn’t have to be named requirements.txt</h1><div id="57eb"><pre>pip install -r requirements.<span class="hljs-property">txt</span></pre></div><ul><li><code>-r</

Options

code> means to <code>read</code></li><li><code>requirements.txt</code> is just an arbitrary text file named out of convention</li></ul><p id="e361">We can actually name it whatever as long as it’s a valid text file name.</p><div id="f227"><pre>pip install -r applepie<span class="hljs-selector-class">.txt</span></pre></div><p id="2899">^ pip will then look for a <code>applepie.txt</code>, and install every single line inside.</p><p id="a230">Note — if you’re working on an enterprise-level app, please ensure that you name your <code>requirements.txt</code> files properly eg. <code>requirements_local.txt</code></p><h1 id="244d">6) requirements.txt can contain other requirements.txt files</h1><p id="570f">Let’s say we have a 2 <code>requirements.txt</code> files</p><div id="8c04"><pre>requirements.txt requirements_windows.txt</pre></div><ul><li><code>requirements.txt</code> contains the main stuff</li><li><code>requirements_windows.txt</code> contains specialized windows libraries</li></ul><p id="5a3a">Since we want <code>requirements_windows.txt</code> to also contain the stuff inside <code>requirements.txt</code>, we can do this. In <code>requirements_windows.txt</code>:</p><div id="b589"><pre><span class="hljs-keyword">library</span>-one <span class="hljs-keyword">library</span>-two -r requirements.txt</pre></div><ul><li><code>library-one</code> and <code>library-two</code> will be installed</li><li>everything in <code>requirements.txt</code> will also be installed</li></ul><h1 id="2a56">Conclusion</h1><p id="27e8">Hope you learnt at least one new thing here today!</p><h1 id="e705">Some Final words</h1><p id="44b6"><i>If this story was helpful and you wish to show a little support, you could:</i></p><ol><li><i>Clap 50 times for this story</i></li><li><i>Leave a comment telling me what you think</i></li><li><i>Highlight the parts in this story that resonate with you</i></li><li><i>Sign up for a Medium membership using <a href="https://zlliu.medium.com/membership">my link</a> ($5/month to read unlimited Medium stories)</i></li></ol><p id="d75e"><i>These actions really really help me out, and are much appreciated!</i></p><p id="431a"><b>My Ebooks: <a href="https://zlliu.co/books">https://zlliu.co/ebooks</a></b></p><p id="faad"><b>My LinkedIn: <a href="https://www.linkedin.com/in/zlliu/">https://www.linkedin.com/in/zlliu/</a></b></p><div id="d9d8" class="link-block"> <a href="https://zlliu.medium.com/subscribe"> <div> <div> <h2>Get an email whenever Liu Zuo Lin publishes.</h2> <div><h3>Get an email whenever Liu Zuo Lin publishes. By signing up, you will create a Medium account if you don't already have…</h3></div> <div><p>zlliu.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*-DG7lgJZxM9kLa8y)"></div> </div> </div> </a> </div></article></body>

6 Things I Wish I Knew Earlier About requirements.txt

# Learnt from working with enterprise Python apps

1) How to install everything inside requirements.txt

requirements.txt contains the required libraries for a project that needs to be installed (usually using pip). A simple example:

library-one==1.0.1
library-two==3.1.4
library-three==100.1.0

To install all 3 libraries, we can run the command:

pip install -r requirements.txt

which will install every single line inside requirements.txt

2) How to generate requirements.txt

pip freeze

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):

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

This is essentially the stuff inside a requirements.txt 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.

pip freeze > requirements.txt

^ whatever output from pip freeze will be written to a text file with the name requirements.txt. And there we have it — how to generate a requirements.txt file!

3) It is better to use a Python virtual environment to create a requirements.txt

When we pip freeze, EVERY SINGLE installed library will appear in your requirements.txt. Which can get annoying — what if your project simply needs numpy and pandas, but requirements.txt will install another 100 unnecessary libraries.

When we use a Python virtual environment, we create a new instance of Python that is separate from your main Python.

python -m venv env    # create a virtual environment called 'env'
# activating our Python virtual environment

env\Scripts\activate.bat    # Windows

source env\bin\activate     # MacOS/Linux

After we create a new virtual environment, it has NOTHING installed. So we need to install everything we need for our project (only once). And when we pip freeze, the stuff that ends up in our requirements.txt will contain only the stuff we installed for the project.

4) We can leave out the version in requirements.txt

Here’s a normal requirements.txt

numpy==1.23.5
pandas==1.5.2
  • numpy with a specific version 1.23.5 will be installed
  • pandas with a specific version 1.5.2 will be installed

Now, here’s a requirements.txt without the versions

numpy
pandas
  • numpy with the latest version will be installed
  • pandas with the latest veersion will be installed

We don’t necessarily need to include the version all the time.

5) It doesn’t have to be named requirements.txt

pip install -r requirements.txt
  • -r means to read
  • requirements.txt is just an arbitrary text file named out of convention

We can actually name it whatever as long as it’s a valid text file name.

pip install -r applepie.txt

^ pip will then look for a applepie.txt, and install every single line inside.

Note — if you’re working on an enterprise-level app, please ensure that you name your requirements.txt files properly eg. requirements_local.txt

6) requirements.txt can contain other requirements.txt files

Let’s say we have a 2 requirements.txt files

requirements.txt
requirements_windows.txt
  • requirements.txt contains the main stuff
  • requirements_windows.txt contains specialized windows libraries

Since we want requirements_windows.txt to also contain the stuff inside requirements.txt, we can do this. In requirements_windows.txt:

library-one
library-two
-r requirements.txt
  • library-one and library-two will be installed
  • everything in requirements.txt will also be installed

Conclusion

Hope you learnt at least one new thing here today!

Some Final words

If this story was helpful and you wish to show a little support, you could:

  1. Clap 50 times for this story
  2. Leave a comment telling me what you think
  3. Highlight the parts in this story that resonate with you
  4. Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)

These actions really really help me out, and are much appreciated!

My Ebooks: https://zlliu.co/ebooks

My LinkedIn: https://www.linkedin.com/in/zlliu/

Python
Programming
Pip
Python Programming
Recommended from ReadMedium