Created
April 22, 2020 17:51
-
-
Save pemagrg1/f959c19ec18fee3ce2ff9b3b86b67c16 to your computer and use it in GitHub Desktop.
convert environment.yml to requirement.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