Python Pipreqs: Generate requirements file from the imported packages

Edit

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='/python-pipreqs'>

    <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'>Python Pipreqs: Generate requirements file from the imported packages</p>
    </div>
</a>

<a class='next' href='/golang-sort-package-basic'>

    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>next</p>
        <p class='prevnext-title'>Golang: Sort Package Introduction</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>