Skip to content
 

Python Package Installations with Conda

- Posted by: Barbra Lashbrook - January 13, 2021 Back to CIRA Wiki Home



Topic Catagories:
Tags: , , , ,

Conda is a package management system that comes with Anaconda and Miniconda distributions of Python that can be used to install, run, update, and remove various Python packages and their dependencies. There are a number of conda “channels” with official and community-contributed Python packages that are accessible using conda. The sections below describe commonly used conda commands.

Installing a Package from the Default Channel

conda install [package-name]. Example: conda install netcdf4

Installing a Package from a Community Channel

Many packages do not exist on the default conda channel, but are available from other sources. If you try a basic conda install and see PackagesNotFoundError, try googling “conda install [package-you-are-interested-in]”. More often than not, you’ll see it available from a different channel, which can be added to your conda install command using a -c flag. Example: conda install -c conda-forge pyhdf. Note: In general, it’s better to stick with as few channels as possible for compatibility, so choose wisely if there are several options.

Installing a Package With a Version Specification

Often, there are many versions of packages available. By default, conda will usually grab the most recent, which may or may not be what you want. If you need to specify a version, add it to the end of the package name like so: conda install -c conda-forge pyhdf=0.9.0

Installing a Package With a Version and Build Specification

Occasionally, you will care about both the version and the build of your package (e.g. if you need the Python 2.7 one for compatibility with old code). The build ID can be added behind the version ID in your package definition. Example: conda install -c conda-forge pyhdf=0.9.0=py27_2

Performing Multiple Installations at Once

Conda installations can take awhile, and sometimes you’d prefer to do all the ones you need at once. Multiple packages can be combined into one command, separated by spaces. Example: conda install -c conda-forge numpy scipy matplotlib basemap basemap-data-hires cartopy netcdf4 pandas pygrib pyhdf toml ipython xarray

Creating an Environment Using a .yml File

Frequently, projects will keep a list of their required packages and the version of python they need in a .yml file.  Typically this file is named environment.yml.  Conda allows you to use a command to create an environment using one of these .yml files. Example: conda env create -f environment.yml

Creating an environment.yml File

It’s usually a good idea to maintain an environment.yml file for your project to keep track of your dependencies and to make it easy to move your environment across systems.  The format of the file is very simple: simply provide a name for your environment, optionally provide any community channels if you are using them, and then list your needed packages and the version of python you’re using.  Example:

name: stats
dependencies:
  - numpy
  - pandas

More Complex Example:

name: stats2
channels:
  - javascript
dependencies:
  - python=3.6
  - bokeh=0.9.2
  - numpy=1.9.*
  - nodejs=0.10.*
  - flask
  - pip:
    - Flask-Testing

Applying environment.yml File Changes

As a project matures, it is likely that you will need to add or remove libraries to your environment.  Conda allow you to keep a pre-existing environment in-sync with changes you’ve made to your environment.yml file.  Example: conda env update --file environment.yml --prune

Additional Information on Using Environments:

https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html