Conda is a cross-platform, language-agnostic package manager and environment manager. While it is widely associated with Python, its capabilities extend to managing packages and dependencies for other languages like R, Java, and C++. This makes it exceptionally powerful for data science, scientific computing, and other multi-language projects. Miniconda is a minimal installer for conda. It includes only conda, Python, the packages they depend on, and a small number of other useful packages. This is in contrast to the full Anaconda distribution, which installs over 250 data science packages by default. Miniconda is preferred by many users because it allows for a clean, minimal base installation from which you can build only the environments you need, conserving disk space and avoiding potential conflicts from pre-installed packages.

Understanding Conda Environments

The core strength of conda lies in its environment management system. An environment is an isolated directory that contains a specific collection of conda packages. For instance, you can have one environment for a project that requires Python 3.8 and Django 3.2, and another for a different project that uses Python 3.11 and the latest version of TensorFlow. They are completely independent; installing, updating, or removing a package in one environment has no effect on the others. This solves the “Project X requires version 1.0, but Project Y requires version 2.0” dilemma, a classic problem in software development. Environments are not just for different projects; they are also crucial for testing code against different versions of Python or library dependencies.

To create a new environment named my_project_env with a specific Python version, use the create command. The -n flag specifies the name.

conda create -n my_project_env python=3.9

To use this environment, you must activate it. Activation prepends the environment’s binary directory to your system’s PATH, ensuring that when you run python or pip, it uses the version inside the environment.

conda activate my_project_env

Once activated, your command prompt will typically change to show the active environment’s name (e.g., (my_project_env) $). To deactivate and return to your base environment, run:

conda deactivate

A common pitfall is forgetting to activate the correct environment before installing packages or running your code, which can lead to packages being installed in the base environment or your code running with the wrong interpreter. Always verify your environment is active.

Installing Packages with Conda

With an environment activated, you can install packages using the conda install command. Conda installs packages from repositories called “channels,” which are the URLs where packages are located. The default channel is maintained by Anaconda, Inc.

conda activate my_project_env
conda install numpy pandas scikit-learn

You can specify a precise version of a package using the = operator, or a version range using > or <.

conda install django=3.2.15

A major best practice is to install all related packages in a single command. Conda’s dependency resolver runs once for the entire command, ensuring a consistent set of compatible packages is selected. Installing packages one by one can lead to dependency conflicts that force subsequent installs to downgrade or remove previously installed packages.

The Conda-Forge Channel

While the default channel is extensive, the community-driven conda-forge channel often provides more up-to-date packages and a wider selection. It is a cornerstone of the conda ecosystem. To install a package from conda-forge, you can use the -c (channel) flag.

conda install -c conda-forge geopandas

To set conda-forge as the priority channel for an environment, you can configure the channel priority to be strict. This tells conda to prefer packages from conda-forge over the default channel when both have the same package. This is best done before installing any packages into the environment.

conda create -n my_geospatial_env
conda activate my_geospatial_env
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
conda install geopandas

Exporting and Recreating Environments

A critical workflow for collaboration and reproducibility is the ability to export your environment’s configuration. The command conda env export generates a detailed environment.yml file listing all packages and their exact versions, including dependencies pulled in from PyPI via pip.

conda activate my_project_env
conda env export > environment.yml

The resulting file can be shared with others. They can then create an identical environment on their machine using the create command with the -f (file) flag.

conda env create -f environment.yml

For a more portable file that only lists the packages you explicitly requested (not all dependencies), use conda env export --from-history. This is often preferable as it is less fragile and more likely to be cross-platform.

Mixing Conda and Pip

While conda should be your first choice, some Python packages are only available on PyPI. You can use pip inside a conda environment. However, a critical best practice is to always use conda first and only then use pip. Install as many dependencies as possible with conda, then use pip for the remaining items. Finally, avoid running pip install and conda install for the same package, as this can create a corrupted state. To minimize the risk of dependency conflicts, it is highly recommended to use the --no-deps flag with pip when possible and let conda manage all dependencies.

conda install numpy
pip install --no-deps package-only-on-pypi