Created
June 5, 2025 13:26
-
-
Save slint/7576f206356c9a486cd6a1dd305a95c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S uv run | |
# /// script | |
# requires-python = ">=3.10" | |
# /// | |
"""Convert a Pipfile to requirements.txt format. | |
Usage: | |
pipfile-to-reqs.py [-d|--dev] [Pipfile] | |
This will output the dependencies in a format suitable for a requirements.txt file. | |
Example: | |
pipfile-to-reqs.py > requirements.txt | |
""" | |
import sys | |
from collections import ChainMap | |
import tomllib | |
include_dev = False | |
if len(sys.argv) == 2: | |
if "-d" in sys.argv[1]: | |
infile = "Pipfile" | |
include_dev = True | |
else: | |
infile = sys.argv[1] | |
else: | |
infile = "Pipfile" | |
with open(infile, "rb") as fp: | |
data = tomllib.load(fp) | |
if include_dev: | |
deps = ChainMap(data["packages"], data["dev-packages"]) | |
else: | |
deps = ChainMap(data["packages"]) | |
for k, v in deps.items(): | |
if isinstance(v, dict): | |
if "git" in v: | |
print(f"{k} @ git+{v['git']}@{v['ref']}") | |
elif "path" in v: | |
print(f"-e {v['path']}") | |
elif "version" in v: | |
if "extras" in v: | |
print(f"{k}[{','.join(v['extras'])}]{v['version']}") | |
else: | |
print(f"{k}{v['version']}") | |
else: | |
print(f"{k}{v}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment