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 small chance that some required packages won’t be detected if they are never explicitly imported in your scripts. After installing the generated requirements in a clean/fresh environment (or building and running your Docker image), test again to identify 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

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
  • pandas >= 2.2.2
  • numpy > 2.0

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.1.0.tar.gz (18.4 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.1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minireqs-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for minireqs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 79615665c06d886966c89548862446c077d7bb29955eb414c92510a975fa15e7
MD5 b979b3d3a71c1592419bb9e931c7782b
BLAKE2b-256 43beb5c7270881f975d6727b2647b2467422f503bd5a4d4c57ba1699d8f50863

See more details on using hashes here.

File details

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

File metadata

  • Download URL: minireqs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for minireqs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 747a75aed38167c6848ce515cf23f07de2525c152c74ec35e1563b87ffe66ced
MD5 e6b0112feaee0dc353b9e45bf400934c
BLAKE2b-256 f32b943ce91999e8ad5c7b8eeeb4607d6c7277541753415ae7eedbf6a23af6d2

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