Skip to main content

Python-Requirements-Guesser

⚠️ This is alpha quality software. Work in progress

Attempt to guess requirements.txt modules versions based on Git history.

What is the problem ?

Did you ever clone a repo with python code that didn't specify library versions in a requirements.txt file ? Or even worst: a repo without a requirements.txt...

Reproducing results is hard, it's even harder when you have mismatched library versions.

Solution

There is a fair chance that the owner of the repo you just cloned installed most of it's packages using

pip install <package name>

This would have installed the latest available version at the time the command was runned.

Based on this, we look at the git commit history to find out when a package was first imported in the code or when it was first added to the requirements.txt file.

We then query Pypi to retrieve the version available at the commit date.

Usage

Py-Requirements-Guesser should be runned inside a git repository.

py-requirements-guesser --write {requirements.txt path}

You will be prompted by a serie of choice to orient the guessing process.

Python Requirements Guesser

Installation

This package doesn't have any dependencies. To install the Py-Requirements-Guesser:

pip3 install py-requirements-guesser

Package name mapping - Pipreqs

There might be mismatches between the name of a package on Pypi and the name used to import it (Ex : pip install PyYAML & import yaml ). There doesn't seem to be a straightforward way to do the mapping between Pypi name and import name.

The great PipReqs package (which was an inspiration for this package) manually maintains a mapping file between Pypi names and the import names. They also maintain a list of the standard library module names.

For now, we grab the mapping and stdlib files at commit 90102acdbb23c09574d27df8bd1f568d34e0cfd3.

Thanks guys !

Additional arguments

Py-Requirements-Guesser can take 2 additional parameters :

--keep_unused_packages: By default, unused packages are ignored. This parameter will force version guessing for the packages in requirements.txt that are not imported in the code anywhere.

--force_guess {package1},{package2},..: By default, if your code contains a module named yaml.py, import yaml statements won't be analyzed. Use this argument if local modules have conflicting names with Pypi packages to force version guessing.

TODO

  • Guess/Pin the dependencies tree of the package Ex : Torch package will install numpy, etc
  • Poetry support ?
  • Jupyter notebook support
  • Add guessing choice where user can choose version between the time the package was first imported and the date of the last commit on a python file
  • Detect python & os versions. Some package versions might not be available for certain os or python versions
  • Better output/UX

License

GNU GPLV3 see License

Contributing

Pull requests are welcomed ! Fill up an issue if you encounter any problem !

Metadata

Release files for py-requirements-guesser 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-requirements-guesser 0.1.0
File Size Uploaded
py-requirements-guesser-0.1.0.tar.gz 20.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-requirements-guesser 0.1.0
File Interpreter ABI Platform
py_requirements_guesser-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 42.8 kB

Release files / py-requirements-guesser-0.1.0.tar.gz

Download URL py-requirements-guesser-0.1.0.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e7e7d1fff65733f2736e22d9a51926f9bb32006ecdf7453f77aec14cb79696a8
BLAKE2b-256 checksum
How to use checksums
44edfb88075be6acc9d9b690555ad72ee0fba181fcec03c38e98dc3cdf62bc37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.9

Release files / py_requirements_guesser-0.1.0-py3-none-any.whl

Download URL py_requirements_guesser-0.1.0-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
130ae21072c4d895c067391bbabafbdbe10cbf325c3032871330453ccfd0d8f7
BLAKE2b-256 checksum
How to use checksums
69934e2630c78c7001e5db99ccf059daa47b9e6f4fe48511b07d5dfc16e6496a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page