Search code examples
pythonpackagerequirements.txt

Pipreqs requirements.txt is not correct


Hello I am having troubles with the pipreqs librairy in Python. It doesn't generate the correct requirements.txt file. I am using a Python Virtual Environment and the only packages I have installed are pipreqs and selenium with

pip install pipreqs
pip install selenium

Structure of the project:

MyProject
 |- test.py

And test.py has only one line:

from selenium import webdriver

First when I do

pipreqs ./

I got the error UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3474: character maps to <undefined> which I managed to solve by using

pipreqs ./ --encoding=utf-8

But now the requirements.txt generated doesn't match my expectations. In my opinion, it should be equal to:

selenium==1.341.0

But it is equal to:

brotli==1.0.9
cryptography==3.2.1
ipaddr==2.2.0
lxml==4.6.1
mock==4.0.2
ordereddict==1.1
protobuf==3.13.0
pyOpenSSL==19.1.0
simplejson==3.17.2

Now when I try to clone this code and do pip install -r requirements.txt it doesn't install selenium and the code doesn't run.

What is happening here ?


Solution

  • So the issue I had was that my actual workspace was:

    MyProject
     |- .venv   // <- My Python Virtual Environment
     |- test.py
    

    My Python Virtual Environment was in my Project folder so when I run the command

    pipreqs ./
    

    it is looking at all the dependencies of all the files in the folder (including my virtual environment) and that is why it was generating a weird requirements.txt file.

    To fix this, I used the option --ignore of pipreqs:

    pipreqs ./ --ignore .venv
    

    And the generated requirements.txt is:

    selenium==3.141.0
    

    You may also want to ignore other folders that are causing issues like this:

    pipreqs --ignore bin,etc,include,lib,lib64,.venv
    

    The --force tag will also overwrite the existing requirements.txt