Skip to main content

A lightweight Python utility that scans .py files to detect all import statements, identifies the corresponding PyPI packages, and determines their installed version numbers. This tool automatically generates an accurate and minimal requirements.txt (or dependency list), making it ideal for packaging, deployment, and environment reproduction.

Project description

minireqs

A lightweight Python utility that automatically generates requirements.txt files by scanning Python files, detecting import statements, and identifying the corresponding PyPI packages with their installed versions.

Purpose

Automatically extract minimal dependency requirements from your Python codebase, making it ideal for packaging, deployment, and environment reproduction.

Notes: (1) The package must be installed in the same Python environment as the project so it can detect the packages and versions actually in use. (2) Only detection of packages installed via pip has been tested so far. (3) Even if all packages are installed via pip, there is still a chance that some required packages won’t be detected if they are never explicitly imported in your scripts. Once the generated requirements are installed in a clean/fresh environment, test again—or build and run your Docker image—to check for any missing dependencies.

Main Functions

extract_pip_requirement(script_path, req_path, QA=False)

Main function that extracts pip requirements from a Python file or directory.

  • Parameters:
    • script_path: Path to a Python file or directory (default: 'main.py')
    • req_path: Output path for requirements file (default: 'req_main.txt')
    • QA: If True, prints the results DataFrame
  • Returns: DataFrame with import names and their corresponding pip package requirements

get_imports(script_path)

Extracts all import statements from a Python file using AST parsing.

  • Parameters: script_path - Path to a Python file
  • Returns: List of import names found in the file

find_pip_pkg(import_name)

Finds the corresponding PyPI package name and version for an import statement.

  • Parameters: import_name - The import name (e.g., 'pandas', 'numpy')
  • Returns: Tuple of (package_name, version) or (None, None) if not found

Utility Functions

  • find_py_files(script_path): Recursively finds all .py files in a directory or returns a single file path
  • safe_import(name): Safely imports a module by progressively stripping attributes

Installation

pip install minireqs

or

pip install minireqs[Numpy1]

Usage

from minireqs.pip_package_extract import extract_pip_requirement

# Extract requirements from a file or directory
df = extract_pip_requirement('my_script.py', 'requirements.txt')

Requirements

  • Python >= 3.10
  • Default option: (numpy >= 2.0.0, pandas >= 2.2.2)
  • [Numpy1] option: (1.21.0 <= numpy < 2.0.0, 2.0.0 <= pandas < 2.2.2)

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

minireqs-0.2.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

minireqs-0.2.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file minireqs-0.2.1.tar.gz.

File metadata

  • Download URL: minireqs-0.2.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for minireqs-0.2.1.tar.gz
Algorithm Hash digest
SHA256 676465a14e3a9bd8bb6cf099ad27586fb7d0c1d9c7b3f8b3499a1963437549cc
MD5 7f62c45ca99be0257e4c66c17d951208
BLAKE2b-256 4990a84331f0ad8902858c39347ea10abee446c3db4c85c6bbcda01f7541d50d

See more details on using hashes here.

Provenance

The following attestation bundles were made for minireqs-0.2.1.tar.gz:

Publisher: publish.yml on drmathesis/minireqs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file minireqs-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: minireqs-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for minireqs-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 697ffed766744450f1025983189c6ac37dffe46e5e683037017f5352c0abc9b1
MD5 3369c4c5e3de9b66d8a1ebd9d7c9d966
BLAKE2b-256 bc298893ea2be6047436845395d90e2a3462d6b1c682122a99f27be2d740a433

See more details on using hashes here.

Provenance

The following attestation bundles were made for minireqs-0.2.1-py3-none-any.whl:

Publisher: publish.yml on drmathesis/minireqs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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