Skip to content

Instantly share code, notes, and snippets.

@shenoy-anurag
Last active November 22, 2024 01:14
Show Gist options
  • Save shenoy-anurag/7b6a2e68c7b79d40484803c1adbb5821 to your computer and use it in GitHub Desktop.
Save shenoy-anurag/7b6a2e68c7b79d40484803c1adbb5821 to your computer and use it in GitHub Desktop.
Convert the format of `pip list` to requirements.txt file
"""
Sometimes when you run `pip freeze > requirements.txt` when using a conda environment,
it spits out the exact locations of the packages on the local machine.
This script allows you to run `pip list` instead, and
convert the output of `pip list` into the format used by `requirements.txt` files.
"""
import re
pip_list_str = """
addict 2.4.0
cookiecutter 2.6.0
jupyterlab 4.2.2
pandas 2.2.1
requests 2.31.0
torch 2.4.0
"""
lines = pip_list_str.split("\n")
for l in lines:
s = re.sub("\s+", "==", l)
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment