Skip to main content

Detect unused packages in requirements.txt by scanning imports

Project description

philiprehberger-req-check

Tests PyPI version Last updated

Detect unused packages in requirements.txt by scanning imports.

Installation

pip install philiprehberger-req-check

Usage

from philiprehberger_req_check import check

# Find unused packages in a project directory
unused = check("./my-project")
print(unused)  # ["some-unused-package"]

Scan Imports

from philiprehberger_req_check import scan_imports

imports = scan_imports("./my-project")
print(imports)  # {"requests", "flask", "os", "sys", ...}

Read Requirements

from philiprehberger_req_check import read_requirements

packages = read_requirements("requirements.txt")
print(packages)  # ["requests", "flask", "pillow"]

Finding missing dependencies

from philiprehberger_req_check import find_missing

# Imports used in code but NOT declared in requirements.txt
missing = find_missing("requirements.txt", "./src")
print(missing)  # {"yaml"}

Comparing requirements

from philiprehberger_req_check import compare

diff = compare("requirements.old.txt", "requirements.new.txt")
print(diff)
# {"added": ["httpx"], "removed": ["yaml"], "common": ["requests"]}

CLI

python -m philiprehberger_req_check ./my-project

Exits with code 1 if unused packages are found.

API

Function Description
check(path, *, requirements=None) Find unused packages by comparing imports against requirements
scan_imports(path) Scan Python files for top-level import names
read_requirements(path) Read package names from requirements.txt
find_missing(requirements_path, source_dir) Find imports used in source but not declared in requirements
compare(req_a, req_b) Diff two requirements files into added / removed / common

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_req_check-0.2.0.tar.gz (186.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_req_check-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_req_check-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_req_check-0.2.0.tar.gz
Algorithm Hash digest
SHA256 27c1fdf3c5ce594da0140a59a6492db238b6d94eac486ff43b14eed17801dc7a
MD5 3e0591cea8b29b9fd51e9b9ebb723b7f
BLAKE2b-256 cbd317d64d582dacad14e531de16ac0a3b8137b1dfb9109adb9d30f63700caa2

See more details on using hashes here.

File details

Details for the file philiprehberger_req_check-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_req_check-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8780ded553292106d91c0cba4ff020704cdc746e9249e3e22e9c8fe2f42e15d5
MD5 fe152093d319d6e3f13d1db1009ccaf2
BLAKE2b-256 8127ca2cac9e8fa4bb161d65d761edac61e7a31bfc85df2d2570a437c88b5706

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page