Created
January 7, 2022 03:35
-
-
Save vgthengane/c2a5918f98112e8e4f782c0a03f029ea to your computer and use it in GitHub Desktop.
Convert environment.yaml to requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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