Skip to content

Instantly share code, notes, and snippets.

@pemagrg1
Created April 22, 2020 17:51
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pemagrg1/f959c19ec18fee3ce2ff9b3b86b67c16 to your computer and use it in GitHub Desktop.
Save pemagrg1/f959c19ec18fee3ce2ff9b3b86b67c16 to your computer and use it in GitHub Desktop.
convert environment.yml to requirement.txt
import ruamel.yaml
yaml = ruamel.yaml.YAML()
data = yaml.load(open('environment.yml'))
requirements = []
for dep in data['dependencies']:
if isinstance(dep, str):
package, package_version, python_version = dep.split('=')
if python_version == '0':
continue
requirements.append(package + '==' + package_version)
elif isinstance(dep, dict):
for preq in dep.get('pip', []):
requirements.append(preq)
with open('requirements.txt', 'w') as fp:
for requirement in requirements:
print(requirement, file=fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment