Skip to content

Instantly share code, notes, and snippets.

@vgthengane
Created January 7, 2022 03:35
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 vgthengane/c2a5918f98112e8e4f782c0a03f029ea to your computer and use it in GitHub Desktop.
Save vgthengane/c2a5918f98112e8e4f782c0a03f029ea to your computer and use it in GitHub Desktop.
Convert environment.yaml to requirements.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