Skip to content

Instantly share code, notes, and snippets.

@Xion
Created January 25, 2014 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xion/8624077 to your computer and use it in GitHub Desktop.
Save Xion/8624077 to your computer and use it in GitHub Desktop.
requirements.txt parser for setup.py
def read_requirements(filename='requirements.txt'):
"""Reads the list of requirements from given file.
:param filename: Filename to read the requirements from.
Uses ``'requirements.txt'`` by default.
:return: Requirments as list of strings.
"""
# allow for some leeway with the argument
if not filename.startswith('requirements'):
filename = 'requirements-' + filename
if not os.path.splitext(filename)[1]:
filename += '.txt' # no extension, add default
def valid_line(line):
line = line.strip()
return line and not any(line.startswith(p) for p in ('#', '-'))
def extract_requirement(line):
egg_eq = '#egg='
if egg_eq in line:
_, requirement = line.split(egg_eq, 1)
return requirement
return line
with open(filename) as f:
lines = f.readlines()
return list(map(extract_requirement, filter(valid_line, lines)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment