Install Conda

Install only Miniconda as Anaconda is bloated and doesn’t make sense anyway without understanding Miniconda.

Initialise Conda

This means adding the conda paths to your shell session profile or registry so the conda command is available every time you start up.

  • Windows CMD: C:\ProgramData\miniconda3\condabin\conda init writes an autorun to the registry path Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor. This can interfere when you use subprocess in python and call the script from within powershell as subprocess uses the CMD. Delete this Autorun if you use PowerShell
  • Windows PowerShell C:\ProgramData\miniconda3\condabin\conda init writes to $PROFILE.CurrentUserAllHosts
  • Linux Bash and Powershell echo ". /home/<user>/miniconda3/etc/profile.d/conda.sh" >> ~/.profile Writes to the ~/.profile file which is used by all shells. Change the to your username.

Initialise Mamba

In your profile, comment out the conda related init script and instead directly add C:\ProgramData\mambaforge\condabin to your system path.

Now you can use conda and mamba commands, but they both refer back to mambaforge.

You need to restart pycharm for this path to update there.

Turn off the annoying (base) notification if you use oh-my-posh

conda config --set changeps1 False

Create environment

NOT conda env create :

conda create -n <env_name>

But if you are creating from a yml file you need to use:

conda env create -f requirements.yml

Install Requirements to Environment

To install into current environment:

conda env update -f environment.yml

To install a package including requirements in that package just run pip from within a conda environment. Here ‘path’ is a directory containing a setup.py, a repo gir+ssh address or a package on PyPi:

pip install path

If you just want to install the txt format of requirements:

conda install -r requirements.txt

Mixing pip and conda

If pip was originally installed with conda in your environment i.e. with conda install pip or installed implicitly as part of an env.yml file, conda does manage pip dependencies and versioning too. So it is ok to mix, but better to install with conda.

The following warning message may occur though:

WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(143): Could not remove or rename

Clear out your packages

conda clean --all

Channels

Search for all packages in a channel:

conda search --override-channels -c channel '*'

Understand the commands

Always activate the relevant environment for your repo before installing as it is non trivial to bulk uninstall all the custom packages in the base environment.

Some key commands:

conda … means packages

conda env … means environment. Except if you are creating one. then its just conda create -n ...

.condarc file

Guidance

The .condarc file is found in the home directory and it contains the configuration settings for conda.

Add to the condarc file:

always_yes: True
channel_priority: true
Channels:
- conda-forge

Install Mamba

Mamba is just a much quicker way of installing packages. Install Mamba with:

conda update -y --all
conda install -c conda-forge mamba

Then you can just use the mamba command word instead of conda in all circumstances. For example the two commands are equivalent but mamba is faster:

conda install black
mamba install black

Mamba uses the condarc config file too.

Set the env directory

All envs will be installed to the path set n the condarc:

In Linux should be:

envs_dirs: 
  - /home/sebastian/miniconda3/envs

In windows should be:

envs_dirs:
  - C:\ProgramData\Miniconda3\envs

The environment.yaml file

When you want to export an environment with conda you can run the following command to generate an environment file:

conda env export > environment.yml

You can only import a new environment from an environment file, not a requirements.txt file.

mamba env create --file .\env.yml --name <name>
mamba activate <name>

If you don’t have an env.yml file, you have to create an empty environment and then install after:

mamba env create --name <name>
mamba activate <name>

Note that update will not install any new packages from an environment file. It will only update.

mamba env update --file .\env.yml --name <name> 

Remove an environment if you mess it up. It is not possible to reset an environment:

conda env remove --name rapyds

Spyder Troubleshooting

  1. uninstall pyQt using

conda uninstall pyqt

  1. uninstall sip package

conda uninstall sip

  1. then install these packages in following order

conda install sip

conda install pyqt

conda upgrade spyder

this worked for me.

not creating entire environment straight away https://stackoverflow.com/questions/43145667/anaconda-is-not-creating-full-environment

Environment variables

In a python environment you have many of the same environment variables as in the shell.

  • PowerShell environment variables are given by ls env: and accessed with $env:name. I copied them into a list called winpath.
  • Python environment variables are given by pprint.pprint(dict(os.environ).keys()) and accessed with os.environ['name']

os.environ is case sensitive on linux but not on windows. Nevertheless, certain enviroment variable strings are stored in Uppercase in windows and lowercase in python:

The PowerShell environment variables are not in python:

>>> [p for p in map(lambda x: x.lower(), winpath) if p not in map(lambda x: x.lower(), dict(os.environ).keys())]

['conda_prompt_modifier', 'powershell_distribution_chann…']

The python environment variables are not in PowerShell

>>> [p for p in map(lambda x: x.lower(), ) if p not in map(lambda x: x.lower(), dict(os.environ).keys())]
['CONDA_DEFAULT_ENV',
 'CONDA_PREFIX',
 'CONDA_PYTHON_EXE',
 'CONDA_SHLVL',
 'POWERSHELL_DISTRIBUTION_CHANNEL']

``

Errors

Random loss of ability to use conda

I turned off Documents onedrive sync, which changed the registry path for my user shell folder for my documents from one drive (where my powerershell currentuserallhosts profile is stored) .

That is where the conda init script for powershell goes to:  $Home\Documents\PowerShell\Profile.ps1. Just had to run conda init again.

Unnamed Environments

https://stackoverflow.com/questions/57527131/conda-environment-has-no-name-visible-in-conda-env-list-how-do-i-activate-it-a

A newer version of conda exists

==> WARNING: A newer version of conda exists. <==
  current version: 4.10.3
  latest version: 22.11.1

You have to update using the conda-forge channel, not the defaults channel.

conda update -n base -c conda-forge conda

You cache is likely corrupted

conda clean --all

A package which appears in conda list cannot be imported

Don’t use py for python as doesn’t point towards your conda executable it points towards:

C:\Windows\py.exe

Check that the capitalisation and spelling of every package is identical to that on the repo. Pint seems to have changed from pint to Pint after version 0.2, and I spend literally hours reinstalling and debugging my entire system trying to figure out why I couldn’t import the module. Also, specifying >=0.9 was not as affective as saying =0.9.*

Just don’t name modules with capital letters.

Mixing mamba and conda could cause problems.