-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconda_update_from_pip_list.py
66 lines (50 loc) · 2.28 KB
/
conda_update_from_pip_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# overwrites pip dependencies' section in environment.yml file with output of "pip list" command (set --overwrite to true)
# however, pip list also displays packages installed by conda/mamba, so it is preferable to double check environment.yml file
# if you'll run this python script
import subprocess
import sys
import yaml
def install_packages(packages):
installed_packages = []
for package in packages:
subprocess.run(['pip', 'install', package], capture_output=True, text=True)
version_number = get_version_number(package)
installed_packages.append(f'{package}=={version_number}')
return installed_packages
def update_environment_file(installed_packages, overwrite=True):
with open("environment.yml", "r") as f:
env = yaml.safe_load(f)
if 'dependencies' not in env:
env['dependencies'] = []
if 'pip' not in env['dependencies'][-1]:
env['dependencies'][-1]['pip'] = []
if overwrite:
env['dependencies'][-1]['pip'] = installed_packages
else:
for package in installed_packages:
if package not in env['dependencies'][-1]['pip']:
env['dependencies'][-1]['pip'].append(package)
env['dependencies'][-1]['pip'] = sorted(env['dependencies'][-1]['pip'])
with open('environment.yml', 'w') as f:
yaml.dump(env, f)
def get_version_number(package):
version = subprocess.run(['pip', 'show', package], capture_output=True, text=True)
version_number = next(line for line in version.stdout.split('\n') if line.startswith('Version:')).split(': ')[1]
return version_number
def get_installed_packages():
installed_packages = []
result = subprocess.run(['pip', 'list'], capture_output=True, text=True)
for line in result.stdout.split('\n')[2:]:
if not line:
continue
package, version = line.split()
installed_packages.append(f'{package}=={version}')
return installed_packages
if __name__ == "__main__":
packages = sys.argv[1:]
overwrite = "--dont_overwrite" not in sys.argv
if len(packages) > 0:
installed_packages = install_packages(packages)
update_environment_file(installed_packages)
all_installed_packages = get_installed_packages()
update_environment_file(all_installed_packages, overwrite)