Skip to content

Instantly share code, notes, and snippets.

@rcastill
Created November 26, 2019 16:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcastill/dab85c234dd10fa7af56755116c75aee to your computer and use it in GitHub Desktop.
Save rcastill/dab85c234dd10fa7af56755116c75aee to your computer and use it in GitHub Desktop.
Convert Pipfile.lock into requirements.txt
#!/usr/bin/env python
"""
Convert Pipfile.lock into requirements.txt
It generates an output similar to `pipenv lock -r`
without the need to actually locking the file.
See https://github.com/pypa/pipenv/issues/3493
This script was created to include the definition of sources
at the beginning of the requirements file. Useful when
working with private sources.
"""
import json
import sys
from os.path import expandvars as exenv
if __name__ == "__main__":
fname = "./Pipfile.lock"
if len(sys.argv) == 2:
fname = sys.argv[1]
elif len(sys.argv) > 2:
print("Usage: {} [Pipfile.lock]".format(sys.argv[0]))
sys.exit(1)
lines = []
with open(fname) as f:
lock = json.load(f)
# add sources
sources = lock.get("_meta", dict()).get("sources", [])
if len(sources) > 0:
lines.append("-i {}".format(exenv(sources[0]["url"])))
for extra in sources[1:]:
lines.append("--extra-index-url {}"
.format(exenv(extra["url"])))
# requirements
pkgs = lock.get("default", dict())
for pkg, meta in pkgs.items():
lines.append(pkg + meta.get("version", ""))
# write iff went OK
print("\n".join(lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment