Skip to main content
Conda/Mamba

Conda/Mamba #

Warning: This post hasn't been updated for over a year. The information may be out of date.

References:

Managing Environments #

Toggling channel priority:

# see the current setting
$ conda config --describe channel_priority

# set to flexible if environment conflict just won't resolve
$ conda config --set channel_priority flexible

# set to strict if feeling lucky
$ conda config --set channel_priority strict

Create an environment from file:

$ conda env create --file environment.yml
$ conda env create -f environment.yml

Update an environment:

$ conda env update

Delete an environment:

$ conda remove --name my-env --all --yes
$ conda remove -n my-env --all -y

Manage Jupyter Notebook Installation #

dependencies:
  - ipykernel

  # If need Jupyter Lab
  - jupyterlab

  # If need extensions
  - jupyter_contrib_nbextensions
  … (List of extensions that need installation)

Managing Packages #

Check Package Version #

$ conda list --full-name <package-name>

Force Re-installation #

First, deactivate the current environment and activate again.

If that doesn’t work:

$ conda install --force-reinstall <a-bad-package>

If that doesn’t work:

  1. Open conda’s package directories. (example: Miniconda)
    • Check: conda info
    • /opt/miniconda3/pkgs/
    • /opt/miniconda3/envs/my-env/lib/python3.7/site-packages/
  2. Remove the package.
  3. Run conda env update.

If that doesn’t work:

  1. Switch to base environment or run conda deactivate
  2. Delete my-env with conda remove -n my-env --all -y
  3. Create it again with conda env create -f environment.yml

Magic commands #

Doc: Built-in magic commands — IPython 8.21.0 documentation

System commands #

# Prints current working directory
%pwd

Show function definition #

Ref: Can Python print a function definition? - Stack Overflow

# help
function_name?

# definition
function_name??

Use with direnv #

Auto activate environment #

.envrc:

cd /usr/local/Caskroom/miniconda/base/bin
source activate myenv
cd -

Use local package #

.envrc:

export PROJECT_ROOT=$PWD
export PYTHONPATH=$PROJECT_ROOT:$PYTHONPATH # prioritise local packages

The environment directory must look like this:

.
├── .envrc
├── environment.yml
├── my_package
│   ├── __init__.py
│   └── ...
└── ...