<?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: Arnav Raina</title>
    <description>The latest articles on Forem by Arnav Raina (@arnavrneo).</description>
    <link>https://forem.com/arnavrneo</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%2F1243462%2F44099cbc-5f51-4127-b703-e04e94b101d5.jpg</url>
      <title>Forem: Arnav Raina</title>
      <link>https://forem.com/arnavrneo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arnavrneo"/>
    <language>en</language>
    <item>
      <title>Automate the task of creating requirements.txt with pyreqs!</title>
      <dc:creator>Arnav Raina</dc:creator>
      <pubDate>Thu, 28 Dec 2023 15:50:24 +0000</pubDate>
      <link>https://forem.com/arnavrneo/automate-the-task-of-creating-requirementstxt-with-pyreqs-85</link>
      <guid>https://forem.com/arnavrneo/automate-the-task-of-creating-requirementstxt-with-pyreqs-85</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;All us python programmers have been in a position when our Python project is just ready and its time to share or ship it and we need to create our python dependencies (&lt;code&gt;requirements.txt&lt;/code&gt;). And hence comes the time-consuming procedure of looking at our imports and adding them to our &lt;code&gt;requirements.txt&lt;/code&gt; 😮‍💨.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;We can easily go for &lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt; that adds all the packages in our current python environment to the file. But there are some downsides to that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pip freeze&lt;/code&gt; only saves the packages that are installed with pip install in our environment; and &lt;/li&gt;
&lt;li&gt;it saves all packages in the environment including those that we don't use in our current project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During my journey of learning &lt;code&gt;go&lt;/code&gt;, I came to realize its beauty in the creation of CLI applications giving me a great project idea to work on the above mentioned issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  pyreqs
&lt;/h3&gt;

&lt;p&gt;So I created &lt;a href="https://github.com/arnavrneo/pyreqs" rel="noopener noreferrer"&gt;&lt;code&gt;pyreqs&lt;/code&gt;&lt;/a&gt;. It is a command-line application written in &lt;code&gt;go&lt;/code&gt; for automating the task of creating &lt;code&gt;requirements.txt&lt;/code&gt; for any python project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful7l3nogceo0vjkt28ym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ful7l3nogceo0vjkt28ym.png" alt="main-cmd-win"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just get a compatible release for your system, and go to your python project. To create &lt;code&gt;requirements.txt&lt;/code&gt;, just run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pyreqs&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pyreqs&lt;/code&gt; analyzes all the imports that have been imported in our &lt;code&gt;.py&lt;/code&gt; files and is intelligent enough to ignore directory imports and only add the relevant ones to our &lt;code&gt;requirements.txt&lt;/code&gt;. We can also specify whether the imports versioning need to be taken from our virtual environment (&lt;code&gt;venv&lt;/code&gt;) directory or not by passing in the &lt;code&gt;-v&lt;/code&gt; flag along with the &lt;code&gt;venv&lt;/code&gt; directory path.&lt;/p&gt;

&lt;p&gt;We can get a detailed info about the imports, by running with &lt;code&gt;--debug&lt;/code&gt; flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pyreqs&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwkcnpmvlmo21palhphpu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwkcnpmvlmo21palhphpu.png" alt="pyreqs-with-debug"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;create&lt;/code&gt; command is available with many other flags:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F21d2l2anmkt5aj1vi3fd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F21d2l2anmkt5aj1vi3fd.png" alt="create-cmd"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, sometimes we have the &lt;code&gt;requirements.txt&lt;/code&gt; file and we just want to clean it up according to our project imports, &lt;code&gt;pyreqs&lt;/code&gt; does that with the &lt;code&gt;clean&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pyreqs&lt;/span&gt; &lt;span class="n"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;requirements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are various optional flags that can be used with &lt;code&gt;clean&lt;/code&gt; as seen below:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6yxua8ux8ibph3wqluc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6yxua8ux8ibph3wqluc.png" alt="clean-cmd"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this tool makes the development process a little smooth for the fellow python developers out there! Peace ✌️.&lt;/p&gt;

</description>
      <category>python</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
