The conda Package Manager

The purpose of the conda package manager is to maintain a Python environment.

The base Python Environment

In Anaconda the base Python environment is a Python distribution and should not be modified outwith the standard conda images available from Anaconda covered in the Updating Anaconda tutorial. The reason for this is the Python distribution has a large number of packages and changing a package that is a dependency for the other packages will normally result in a number of these packages being removed leading to an unstable Python environment.

To recap the base Python environment is found in:

%USERPROFILE%\Anaconda3

This contains a python.exe which is the base Python environment:

img_001

This base Python environment also has a Lib folder which contains the Python standard modules:

img_002

Such as email (as a subfolder):

img_003

datetime (as a module):

img_004

And the site-packages folder which contains the third-party libraries:

img_005

such as numpy, pandas and matplotlib:

img_006

The envs folder

Additional Python environments are found in the environments folder envs:

%USERPROFILE%\Anaconda3\envs
img_007

By default there are no additional Python environments and this folder is empty:

img_008

A Python environment is essentially a sub-installation of Python which is used to install Python alongside a number of third-party Python libraries.

The use of Python environments for example allows installation of the latest version of each IDE without breaking the functionality of the (base) Python environment.

conda Package Manager

Overview

An overview about the conda package manager can be seen by opening up the Anaconda PowerShell Prompt and inputting the PowerShell command:

conda
img_009

This gives the following output:

img_010
commanddescription
cleanRemove unused packages and caches.
compareCompare packages between conda environments.
configModify configuration values in .condarc.
createCreate a new conda environment from a list of specified packages.
doctorDisplay a health report for your environment.
infoDisplay information about current conda install.
initInitialize conda for shell interaction.
installInstall a list of packages into a specified conda environment.
listList installed packages in a conda environment.
noticesRetrieve latest channel notifications.
packageCreate low-level conda packages.
removeRemove a list of packages from a specified conda environment.
renameRename an existing environment.
runRun an executable in a conda environment.
searchSearch for packages and display associated information using the MatchSpec format.
updateUpdate conda packages to the latest compatible version.
buildSee conda build –help.
content-trustSee conda content-trust –help.
convertSee conda convert –help.
debugSee conda debug –help.
developSee conda develop –help.
envSee conda env –help.
indexSee conda index –help.
inspectSee conda inspect –help.
metapackageSee conda metapackage –help.
packSee conda pack –help.
renderSee conda render –help.
repoSee conda repo –help.
serverSee conda server –help.
skeletonSee conda skeleton –help.
tokenSee conda token –help.
verifySee conda verify –help.

Conda Channels

There are two main channels used by the conda package manager:

channel namechannel description
conda-forgecommunity channel maintained by developers
anacondachannel maintained by the Anaconda company

The community channel is maintained directly by Python developers. A subset of the packages from the community channel are further tested by the Anaconda company for use in the Anaconda Python distribution.

Therefore popular packages in the community channel are usually more up to date with respect to packages in the conda channel. And less popular packages in the community channel are unlikely to be in the anaconda channel.

A Python environment is normally unstable if it uses mixed channels and most custom end-user Python environments will only use packages in the conda-forge channel.

Config and .condarc file

The conda config command is used to create a .condarc file which can be used to change the solver used by the conda package manager and the channel and channel priority used by the conda package manager.

Open the Anaconda PowerShell Prompt. Recall the default location the Anaconda PowerShell Prompt opens in is %USERPROFILE%. This is the location a .condarc file gets placed:

img_011

If an old .condarc file is present delete it using:

del .condarc
img_012
img_013

The conda package manager uses the legacy solver by default. The newer solver libmamba should be used instead:

conda config --set solver libmamba

The community conda-forge should be added:

conda config --add channels conda-forge

And the anaconda channel (defaults) can be removed:

conda config --remove channels defaults

Additional performance can be achieved by setting the channel priority to strict:

conda config --set channel_priority strict
img_014

This .condarc is optimised for creating new Python environments using packages from the community channel conda-forge. It should not be used to update Anaconda; which should only use the anaconda package from the anaconda channel.

img_015

The .condarc looks like the following when opened in a text editor:

img_016
solver: libmamba
channels:
  - conda-forge
  - defaults
channel_priority: strict

Close the Anaconda PowerShell Prompt to refresh changes made by the .condarc.

Create

A Python environment can be created using the syntax:

conda create -n notbase

where "notbase" is the environment name. Input y to proceed:

img_017

Notice in envs, the notbase subfolder is created:

img_018

Activate

Once the Python environment is created it can be activated (selected) using:

conda activate notbase
img_019

Once activated, any changes made using the conda package manager in the Anaconda PowerShell Prompt will apply to this Python environment instead of base. The prompt now has the (notbase) prefix indicates the (notbase) Python is selected:

img_020

If the Anaconda PowerShell Prompt is closed and reopened, the default Python environment base will be selected. The Python environment notbase will have to be activated.

The conda activate comand does not change Python environment in the Windows Terminal because the WIndwos Terminal always uses the Python environment listed in the Windows Environmental Variables path.

The conda-forge community channel can be searched for a package in this case the package "python" using:

conda search -c conda-forge python
img_021

Notice each Python has a version, a build number and a channel:

img_022

Specifying the channel is not necessary as the .condarc file already specifies use of the conda-forge channel however it is good practice to specify the channel. The package ipython can be searched for using:

conda search -c conda-forge ipython
img_023
img_024

Install

Multiple packages can be installed using the syntax:

conda install -c conda-forge python ipython
img_025

Details about the packages to be installed are listed:

img_026

Input y in order to proceed:

img_027
img_028

Notice the Python environment notbase, now has its own python.exe:

img_029

Its own Lib subfolder:

img_030

This has the standard modules such as email (which is a multi-file module in a folder):

img_031

datetime which is a single module:

img_032

Third party libraries are in the site-packages subfolder:

img_033

This has ipython and some of its dependencies such as the matplotlib backend matplotlib_inline:

img_034

The data science libraries numpy, pandas and matplotlib (the full library) as they are not installed as they are not dependencies.

Remove

The command remove can be used to remove an installed package:

conda remove python ipython
img_035

If other packages rely on the packages being removed as dependencies, they will be removed. Since Python itself is being removed, and all packages are in turn dependent on Python, they will all be removed:

img_036
img_037

Input y to proceed with the changes. The changes will then be made:

img_038

Notice the python.exe is gone alongside the Lib subfolder.

Install Specific Package Version

During installation version numbers can be specified:

conda install -c conda-forge python=X.Y.Z

Where X is the major number, Y is the minor version number and Z is the patch number. For example:

conda install -c conda-forge python=3.11.3

In the previous output when conda search was used. Each Python version 3.11.1 onwards had a build number of h628c8c_0. This can also be speciifed during installation:

conda install -c conda-forge python=3.11.3=h2628c8c_0_cpython

Some packages for example ipython have multiple variants that have the same version number, for example ipython has 3 variants that are at version 8.14.0 on the conda-forge channel. These each have unique build numbers pyh08f2357_0, py41d4057_0 and pyhd1c38e8_0 as seen in the previous output when conda search was used. Normally this is because there is a slightly seperate variant for different minor Python versions e.g. Python 3.11, 3.10 and 3.9.

A specific version of python and ipython can be installed:

conda install -c conda-forge python=3.11.3=h2628c8c_0_cpython ipython=8.14.0=pyh08f2357_0
img_040

Details about the other packages installed will display. Input y in order to install the packages:

img_041

The changes will be made:

img_042

Update

A package can be updated to the latest version using:

conda update -c conda-forge python
img_043

Since an older version of Python was installed, the newer version 3.11.4 is available. To install it input y:

img_044

Alternatively all packages in the environment can be updated using:

conda update -c conda-forge --all
img_045

Note sometimes some packages may be downgraded in order to upgrade some other packages. Sometimes packages require an older version of another package as a dependency. To install the updates input y:

img_046

The updates are now installed:

img_047

List

Packages can be listed in the Python environment using:

conda list
img_048

The conda list command can be used with the option –revisions:

conda list --revision
img_049

Install Revision

The conda install command can be used with the option –revisions and assigned to a revision number. For example, the revision 3 before the update can be reverted to using:

conda install -c conda-forge --revision=3
img_050

Details about the changes will be provided including the downgrades:

img_051

Input y in order to proceed and the proposed downgrade will be implemented:

img_052

Env Export

The currently activated Python environment can also be exported to a yet another markdown language yml file:

conda env export > Documents\notbase.yml
img_053

This creates a notbase.yml file in Documents:

img_054

This can be opened in notepad:

img_055

This is a very small file which can be shared on GitHub or emailed.

Env Create

An environment can be created from a yml file for example the notbase.yml in the Documents folder using:

conda env create -n notbase2 -f Documents\notbase.yml

Note this is done with the base Python environment activated.

img_056

The environment is created and can later be activated:

img_057

The name of the new environment notbase2 displays as a subfolder in envs:

img_058

Env Remove

An environment can be removed using the command:

conda env remove -n notbase

Note this is done with the base Python environment activated as a Python environment cannot be removed if it is activated:

img_059

The operation will proceed:

img_060

The folder notbase will be removed in envs:

img_061

rename

A Python environment can be renamed by using the command:

conda rename -n notbase2 notbase

notbase2 is the original name and notbase is the new name. Note this is done with the base Python environment activated as a Python environment cannot be renamed if it is activated:

img_062

The notbase folder in the envs folder is now renamed notbase2:

img_063

Env List

To list Python environments use:

conda env list
img_064

Clean

A backup of all previously downloaded versions is available which can occupy a large amount of disk space. These can be cleaned using:

conda clean --all
img_065

Python Environments for IDEs

Previously the commands create and install were used seperately and when the list of revision was examined revision 0 had no packages. The conda create command can be used to list a series of packages to be installed when creating a Python Environment. This command will be used to create a Python Environment suitable for the latest version of each Python IDE discussed.

IDLE Python Environment

conda create -n idle -c conda-forge python cython seaborn scikit-learn sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate

Installing seaborn will install numpy, pandas and matplotlib as these are dependencies for seaborn.

openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy and tabulate are file format convertors used for pandas.

IDLE should be launched from its own Python environment using the Anaconda PowerShell Prompt.

Thonny Python Environment

conda create -n thonny -c conda-forge python cython seaborn scikit-learn sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate

Installing seaborn will install numpy, pandas and matplotlib as these are dependencies for seaborn.

openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy and tabulate are file format convertors used for pandas.

The Python interpretter needs to be selected in Thonny.

Spyder Python Environment

conda create -n spyder -c conda-forge python spyder cython seaborn scikit-learn sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate pyqt

Installing seaborn will install numpy, pandas and matplotlib as these are dependencies for seaborn.

openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy and tabulate are file format convertors used for pandas.

pyqt is required for an interactive matplotlib plotting backend.

Spyder should be launched from its own Python environment using the Anaconda PowerShell Prompt or its own Start Menu Shortcut.

JupyterLab Python Environment

conda create -n jupyterlab -c conda-forge python jupyterlab cython seaborn scikit-learn sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate nodejs ipywidgets plotly jupyterlab-variableinspector ipympl pyqt

Installing seaborn will install numpy, pandas and matplotlib as these are dependencies for seaborn.

openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy and tabulate are file format convertors used for pandas.

nodejs allows installaiton of JupyterLab extensions.ipywidgets and plotly can be used to create widgets and plots using Python code, under the hood JavaScript is used to display these in the browser. The variableinspector gives a variable inspector, similar in functionality to variables in Thonny.

pyqt and ipympl are required for an interactive matplotlib plotting backends.

JupyterLab should be launched from its own Python environment using the Anaconda PowerShell Prompt.

VSCode Python Environment

conda create -n vscode -c conda-forge python notebook cython seaborn scikit-learn sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate nodejs ipywidgets plotly ipympl pyqt

Installing seaborn will install numpy, pandas and matplotlib as these are dependencies for seaborn.

openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy and tabulate are file format convertors used for pandas.

nodejs allows installation of JupyterLab extensions.ipywidgets and plotly can be used to create widgets and plots using Python code, under the hood JavaScript is used to display these in VSCode which behaves like a browser.

pyqt and ipympl are required for an interactive matplotlib plotting backends.

The Python interpretter needs to be selected in VSCode.

PyCharm Python Environment

conda create -n pycharm -c conda-forge python cython seaborn scikit-learn sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate pyqt

Installing seaborn will install numpy, pandas and matplotlib as these are dependencies for seaborn.

openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy and tabulate are file format convertors used for pandas.

pyqt is required for an interactive matplotlib plotting backends.

The Python interpretter needs to be selected in PyCharm.

R Programming Language

While Anaconda is mainly associated with Python. The programming language R can also be installed using Anaconda:

JupyterLab

Recall that Jupyter is a loose acronym for Julia Python and R. To install R activate the jupyterlab Python environment created above using:

conda activate jupyterlab

And install r-irkernel from conda-forge using:

conda install -c conda-forge r-irkernel r-tidyverse r-ggthemes r-palmerpenguins

This will set it up for use with R. An R option will display under Notebook and Console allowing the R programming language to be ran in an interactive notebook or console instead of Python.

RStudio

Currently only legacy versions of rstudio are available on the r, conda-forge and anaconda channels. It is generally better to install rstudio externally outwith Anaconda to get the latest version.

Return to Anaconda Tutorial