360 Secure SkillHub
360 安全技能中心

Requirements Checker

这是纯标准库实现的Python requirements.txt工具,核心功能含格式验证、依赖Lint(如未固定版本)、排序、文件对比。适用于CI校验、PR评论、依赖维护场景,支持文本、JSON、Markdown输出,可开启严格模式触发CI失败,还能忽略指定规则。

charlie-morrison
charlie-morrison
热度 51.36
最近更新 2026-05-11 17:42:36
分类:开发依赖:代码执行 / 读写文件应用场景:DevOps / 代码审查 / 数据处理 / 自动化脚本

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add requirements-checker
Metadata
name
requirements-checker
description
Validate, lint, and sort Python requirements.txt files for best practices and CI.
version
1.0.0

requirements-checker

Validate, lint, sort, and compare Python requirements.txt files. Pure stdlib — no external dependencies required.

Validate

Check a requirements file for format errors, invalid specifiers, duplicates, and problematic patterns.

python3 scripts/requirements-checker.py validate requirements.txt

# JSON output for automation
python3 scripts/requirements-checker.py validate requirements.txt --format json

# Strict mode — exit 1 on any issue (CI)
python3 scripts/requirements-checker.py validate requirements.txt --strict

Lint

All validation checks plus best-practice rules: unpinned deps, missing upper bounds, VCS deps, non-alphabetical order, mixed operator styles.

python3 scripts/requirements-checker.py lint requirements.txt

# Markdown output (for PR comments, reports)
python3 scripts/requirements-checker.py lint requirements.txt --format markdown

# Strict mode — exit 1 on warnings too
python3 scripts/requirements-checker.py lint requirements.txt --strict

# Ignore specific rules
python3 scripts/requirements-checker.py lint requirements.txt --ignore unpinned --ignore no-upper-bound

Duplicates

Find packages listed more than once (case-insensitive, PEP 503 normalised).

python3 scripts/requirements-checker.py duplicates requirements.txt

python3 scripts/requirements-checker.py duplicates requirements.txt --format json

Sort

Sort requirements alphabetically. By default writes to stdout; use --write to update the file in-place.

# Preview sorted output
python3 scripts/requirements-checker.py sort requirements.txt

# Write sorted file in-place
python3 scripts/requirements-checker.py sort requirements.txt --write

Compare

Diff two requirements files — shows added, removed, and changed packages with version changes.

python3 scripts/requirements-checker.py compare requirements.txt requirements-new.txt

python3 scripts/requirements-checker.py compare base.txt updated.txt --format markdown

Global Options

Option Description
--format text|json|markdown Output format (default: text)
--strict Exit code 1 on any issue, including warnings/info (CI mode)
--ignore RULE Ignore a named rule; repeatable

Validation Checks

Rule Severity Description
invalid-format error Line doesn't match PEP 508
invalid-specifier error Unknown operator or unparseable version spec
duplicate-package error Same package name appears more than once
editable-install warning -e editable installs in production requirements
vcs-dependency warning git+, hg+, svn+, bzr+ URL dependencies
custom-index-url warning --index-url / --extra-index-url present
url-dependency info Direct URL dependencies
requirement-include info -r nested includes
trailing-whitespace info Line has trailing spaces or tabs
whitespace-only-line info Line contains only whitespace
missing-final-newline info File doesn't end with newline

Lint Rules (in addition to validation)

Rule Severity Description
unpinned warning Dependency has no version specifier
no-upper-bound warning >= used without a < / <= upper bound
non-alphabetical warning Packages are not in alphabetical order
mixed-operators info File mixes == exact pins and >= range specifiers

Example Output

File: requirements.txt
  [ERROR] line 4  (duplicate-package)  Duplicate package 'requests' (first seen on line 2)
           requests==2.31.0
  [WARNING] line 7  (no-upper-bound)  'django' uses >= without an upper bound
           django>=4.0
  [WARNING] line 1  (non-alphabetical)  'zope' is out of alphabetical order
           zope==5.0

Summary: 3 issue(s) — 1 error(s), 2 warning(s), 0 info(s)