bndr / pipreqs

pipreqs - Generate pip requirements.txt file based on imports of any project. Looking for maintainers to move this project forward.
Apache License 2.0
6.38k stars 388 forks source link

Pipreqs reports wrong version when package name != module name #265

Open kelsey-sorrels-tagcg opened 3 years ago

kelsey-sorrels-tagcg commented 3 years ago

Steps to reproduce:

python3 -m venv testenv
source testenv/bin/activate
# install older version of scikit-learn
pip install pipreqs scikit-learn==0.20.3
mkdir test
echo "import sklearn" > test/test.py
pipreqs test
cat test/requirements.txt
deactivate

Actual Output scikit_learn==0.24.2

Expected Output scikit_learn==0.20.3

This occurs because get_import_local is called with package names in the imports parameter (by way of candidates = get_pkg_names(candidates)). It tests if each import is in local. However, local is a dict where the keys are module names. This results in not finding the locally installed package and instead looking up the latest version on pypi.org.