Quick commands

# Create a new environment from a yaml config file
conda env create -f env.yml -n <env_name>

# Create a new environment and manually specify the packages
conda create -n <env_name> python=3.6 pip=20 notebook

# List all environments
conda env list

# Remove an environment (but not its Jupyter kernel mapping)
conda remove --name <env_name> --all

# List all packages in an environment
conda list -n <env_name>

# See how much space you'd free if you removed unused packages from the cache
conda clean --packages --dry-run

# Actually free said space
conda clean --packages

# Other `conda clean` options
conda clean --all --dry-run
conda clean --index-cache --dry-run
conda clean --tarballs --dry-run
conda clean --tempfiles --dry-run

– via Conda Docs

Sample environment file

  - conda-forge
  - python=3.7
  - pip=20
  - lightgbm=3
  - notebook
  - pip:
      - azureml-sdk[notebooks]==1.17
      - scikit-learn==0.23
      - pandas==1.1
      - mlxtend==0.17
      - pandas-profiling==2.9
      - mlflow==1.11

Create a new Jupyter kernel for a Conda env

conda activate <your_conda_env>
pip install --user ipykernel
python -m ipykernel install --user --name=<your_conda_env_but_can_be_something_else_too>

Calling conda activate from a shell script

Conda activate is meant to be run from interactive shell sessions, so use conda run instead

conda run -n <env_name> python script.py --live-stream

An alternative is mark the script as running in an interactive shell and conda activate

#!/usr/bin/env bash -l

conda activate <env_name>
python script.py

– via StackOverflow

Run a Python script in a specific conda environment, as a cron job

Use conda run, make the conda path explicit, using the one from .zshrc, specifically the # >>> conda initialize >>> part.

45 9 * * * 	bash ~/scripts/<the-bash-script>.sh
cd /Users/vladi/Projects/<project> && /Users/vladi/miniconda3/bin/conda run -n <the-env> python <the-script>.py