Skip to content

Instantly share code, notes, and snippets.

@kaedonkers
Last active September 9, 2022 13:43
Show Gist options
  • Save kaedonkers/d304e4aaca61896346af5b9a8c0a66d3 to your computer and use it in GitHub Desktop.
Save kaedonkers/d304e4aaca61896346af5b9a8c0a66d3 to your computer and use it in GitHub Desktop.
Python script to install all conda environments on a system as ipython kernels, making the conda envs available to Jupyter
#!/path/to/miniconda3/bin python3
'''
A commandline tool to install all available conda environments as ipython kernels
- Checks for which conda envs and ipython kernels are available
- Installs the conda envs not yet installed as ipython kernels (including ipykernel if missing)
- Uninstalls ipython kernels which no longer have a corresponding conda env
'''
import jupyter_client
import subprocess
def get_output(cmd):
return bytes(subprocess.check_output(cmd, shell=True)).decode("utf-8").split("\n")
def run_cmd(cmd, **kwargs):
return subprocess.run(cmd, shell=True, **kwargs)
kernels = list(jupyter_client.kernelspec.KernelSpecManager().find_kernel_specs().keys())
envs = [s.split(" ")[0] for s in get_output('conda env list')][2:-2]
keep = ['python3']
install = list(set(envs) - set(kernels))
uninstall = list(set(kernels) - set(envs) - set(keep))
install_ipykernel = []
print(f"Conda envs installed:\n{list(set(kernels)-set(keep))}")
print(f"Conda envs not installed:\n{install}")
print(f"Conda envs to uninstall:\n{uninstall}\n\n---")
# Find which conda envs don't have ipykernel installed
for env in install:
no_ipykernel = get_output(f'conda list -n {env} ipykernel')[3]==""
if no_ipykernel:
install_ipykernel.append(env)
# Install ipykernel in the conda envs which are missing it
for env in install_ipykernel:
print(env)
run_cmd(f"conda install -n {env} ipykernel -y -q", capture_output=True)
print("installed ipykernel")
print("---")
# Install conda envs as ipython kernels
for env in install:
print(env)
run_cmd(f"source activate {env}")
run_cmd(f"ipython kernel install --name {env}")
run_cmd(f"source deactivate", capture_output=True)
print("---")
# Uninstall ipython kernels with no linked conda env
for env in uninstall:
print(env)
run_cmd(f"jupyter kernelspec uninstall {env} -y")
print("---")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment