RTM
RTM

Reputation: 789

Is there any way to install a package in all python enviroments

I have created several conda environments of python. But sometimes, I encounter some utility package that can be helpful to all the environments that I have on my system. Is there any way to do this without switching back and forth between all the environments and installing them individually.

Thanks

Upvotes: 1

Views: 97

Answers (2)

cosmoscalibur
cosmoscalibur

Reputation: 1173

In that case you can install the package in the base (root) environment. All packages related with command line utilities (example git) and graphical user interfaces (example spyder) in that default environment are visibles in all your conda environments.

Update

You can use my script. Uncomment last 3 lines and change list of packages. You can use a manual list of envs or use automatic in all envs.

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, channel="default"):
    TEMPLATE = "conda install {confirm} -c {channel} -n {env} {packages} "
    if isinstance(envs, str):
        envs = [envs]
    if isinstance(packages, list):
        packages = " ".join(packages)
    confirm = "-y"
    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])

envs = conda_env_list()
packages = ["git"]
conda_env_install(envs, packages)

Upvotes: 1

phd
phd

Reputation: 94483

If you store all you virtual environments in one place or if you already use virtualenvwrapper that stores virtualenv in ~/.virtualenvs/ you can use command allvirtualenv from virtualenvwrapper; if you don't use virtualenvwrapper you have to install it first.

allvirtualenv pip install somepackages

I use the following bash script to run a command over all environments in ~/.virtualenvs/ and in~/.tox/:

#! /usr/bin/env bash

if source virtualenvwrapper.sh; then
   allvirtualenv eval "$@"

   for tox_envs in ~/.tox/*; do
      if [[ "$tox_envs" = */.tox/\* ]]; then
         exit 0
      fi
      WORKON_HOME="$tox_envs" allvirtualenv eval "$@"
   done
fi

Upvotes: 0

Related Questions