Python Pipreqs: Generate requirements file from the imported packages

Edit

Published on September 14, 2022

Author: Meet Rajesh Gor

#python

Introduction

Pipreqs is a python package that allows us to list all the pacakges which are imported in a python project. This is a great package for reducing the amount of redundant packages for a project.

Install pipreqs

You can install pipreqs with one of the many ways with pip, pipx, or any other pacakge management tool. I personally use pipreqs with pipx as it remains isolated from the rest of my project dependencies.

Using simple pip install

We can install with pip by creating a virtual environment or in a existing virtual environment.


pip install virtualenv venv
source venv/bin/activate

pip install pipreqs

Using pipx

We can install pipreqs with pipx. Pipx is also a python package but used as a tool to install any cli specific tool with the isolated environment.


pipx install pipreqs
pipx run pipreqs

Using pipreqs

We need to specify the encoding, which is used for reading the files while capturing the imports from the project.


pipx run pipreqs --encoding=utf-8 .

Additionaly, we can specify the path or filename where it will be used to save the imported packages. The --savepath option takes in the path to the file where you want to generate the list of the packages to be installed.


pipx run pipreqs --encoding=utf-8 --savepath reqs.txt . 

Though this doesn't guarentee all the requirements for a file, it is really helpful for explicitly used packages in the python project.

<a class='prev' href='/golang-read-rss-feed'>

    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M13.5 8.25L9.75 12L13.5 15.75" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"> </path>
    </svg>
    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>prev</p>
        <p class='prevnext-title'>Read a Rss Feed with a URL in Golang</p>
    </div>
</a>

<a class='next' href='/django-list-models'>

    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>next</p>
        <p class='prevnext-title'>Django: Get list of all models and associated fields in a django project</p>
    </div>
    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M10.5 15.75L14.25 12L10.5 8.25" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
    </svg>
</a>