Skip to content

Instantly share code, notes, and snippets.

@cosmoscalibur
Last active August 2, 2018 16:22
Show Gist options
  • Save cosmoscalibur/e96f053e0a3fc8301d12a9173c706831 to your computer and use it in GitHub Desktop.
Save cosmoscalibur/e96f053e0a3fc8301d12a9173c706831 to your computer and use it in GitHub Desktop.
This is a convenient way to install packages in all conda environments
"""
This script help you to list your conda environments and install packages in multiples environments.
You need conda package manager installed (Anaconda/Miniconda) and Python Anaconda.
:Authors:
Edward Villegas-Pulgarin <cosmoscalibur at gmail dot com>.
"""
#%%
import subprocess as sub
#%%
def conda_env_list():
p = sub.Popen("conda env list", shell=True, stdout=sub.PIPE, encoding="utf-8")
p.wait()
out = p.communicate()[0].splitlines()
envs = [out[line].split()[0] for line in range(2, len(out)-1)]
return envs
#%%
def conda_env_install(envs, packages, confirm=True, channel="default"):
TEMPLATE = "conda install {confirm} -c {channel} -n {env} {packages} "
if isinstance(envs, str):
envs = [envs]
if isinstance(packages, list):
packages = " ".join(packages)
if confirm == True:
confirm = "-y"
else:
confirm = ""
for env in envs:
cmd = TEMPLATE.format(confirm=confirm, packages=packages, \
channel=channel, env=env)
p = sub.Popen(cmd, shell=True, stdout=sub.PIPE, encoding="utf-8")
p.wait()
print(p.communicate()[0])
### Example (uncomment following lines and change packages)
#envs = conda_env_list()
#packages = ["git", "ffmpeg"]
#conda_env_install(envs, packages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment