<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ilya</title>
    <description>The latest articles on Forem by Ilya (@voilalex).</description>
    <link>https://forem.com/voilalex</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F189710%2Fe8254e6a-12fd-44bc-af3a-711b50f0fb7c.jpg</url>
      <title>Forem: Ilya</title>
      <link>https://forem.com/voilalex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/voilalex"/>
    <language>en</language>
    <item>
      <title>Automatically manage Python dependencies with requirements.txt</title>
      <dc:creator>Ilya</dc:creator>
      <pubDate>Wed, 26 Jan 2022 02:57:52 +0000</pubDate>
      <link>https://forem.com/voilalex/automatically-manage-python-dependencies-with-requirementstxt-5g11</link>
      <guid>https://forem.com/voilalex/automatically-manage-python-dependencies-with-requirementstxt-5g11</guid>
      <description>&lt;p&gt;One of the most important steps in Python development that everybody go through is dependencies management. I remember, when I only started to make some projects for myself I didn’t even know that it’s possible in any automatic way. I thought that you should remember them or look at the code to find which modules are imported and install it manually. Obviously I was wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies management options
&lt;/h2&gt;

&lt;p&gt;There is basically two well-known ways to handle dependencies of your project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using requirements.txt file.&lt;/li&gt;
&lt;li&gt;Using dependency managers like &lt;a href="https://pipenv.pypa.io/en/latest/"&gt;pipenv&lt;/a&gt; or &lt;a href="https://python-poetry.org/"&gt;poetry&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dependencies with requirements.txt
&lt;/h2&gt;

&lt;p&gt;One of the oldest, most popular and native option to manage dependencies of a Python project is requirements.txt. It imply storing all the packages and versions in a file with corresponding name. The format is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;appdata==2.1.1
celery==5.0.5
numpy==1.22.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to install all required packages to your Python interpreter just use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it! Without need in some extra packages. Everything is native to Pip. It doesn’t require handling it manually, by the way. Just use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip freeze &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it will automatically generate requirements.txt for you. But there is one drawback in this approach: it puts ALL the installed packages into the file. After that the requirements.txt may become really big and it becomes hard to understand what a project really use.&lt;/p&gt;

&lt;p&gt;One of the ways to handle it is &lt;a href="https://pypi.org/project/pipreqs/"&gt;pipreqs&lt;/a&gt; library. It searches through your code and find all the dependencies that are related to the project. Just type in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipreqs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you’re done. But that’s not the only option to handle it. You should always remember to use those commands before push, and sometimes it become a headache.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatic requirements.txt update
&lt;/h2&gt;

&lt;p&gt;It becomes clear in the end that it’d be great to have some tool to update requirements.txt automatically and stay always updated without extra steps. For that purpose the library called &lt;a href="https://pypi.org/project/to-requirements.txt/"&gt;to-requirements.txt&lt;/a&gt; were developed. Basically it integrates auto-updating scripts into Pip. The major advantage is that is does not imply any changes in process for development. You does not need to use some extra steps or other commands to install dependencies. Just use Pip as you usually do.&lt;/p&gt;

&lt;p&gt;To install this package use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;to-requirement.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you read the command above you can clearly understand what package do. It installs to requirements.txt. Great wordplay as for me.&lt;/p&gt;

&lt;p&gt;And to setup your Pip scripts use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;requirements-txt setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may require admin rights, because your pip scripts might require root access rights. After that command your pip scripts are ready and you can install your dependencies in straightforward way and keep your requirements.txt file updated.&lt;/p&gt;

&lt;p&gt;To disable to-requirements.txt just uninstall the package or type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;requirements-txt config disable 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find more configuration options here:&lt;br&gt;
&lt;a href="https://requirements-txt.readthedocs.io/en/latest/"&gt;https://requirements-txt.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial I described how you can manage your dependencies and how to do it in the easier way with &lt;a href="https://pypi.org/project/to-requirements.txt/"&gt;to-requirements.txt&lt;/a&gt;. I hope it will help you with your projects. Have a nice day!&lt;/p&gt;

</description>
      <category>python</category>
      <category>automation</category>
      <category>tooling</category>
      <category>dependencies</category>
    </item>
  </channel>
</rss>
