13 Conda Commands for Data Scientists
Let’s get to it! 🚀

The Need

Whether working locally or on a server in the cloud, you want a virtual environment to isolate your Python version and packages so that you can:

use different package versions for different projects experiment with bleeding edge package versions get the same Python and package versions as your teammates so your shared code works similarly

Do I have to use Conda?

Nope. It’s very common in data analysis and scientific computing, but not so common outside.

There are lots of tools you can use to manage virtual Python environments. conda, Venv, pyenv, pipenv, poetry, Docker, and virtualenv are the most prominent. Trust me, you don’t even want to go down this rabbit hole. 🐇

But if you must, here’s a Hacker News thread for a taste of the fun. And Yufeng G, Developer Advocate for Google Cloud, has a nice video on conda, virtualenv, and pyenv here.

Today we’re exploring conda, so let’s dive in!

Anaconda

Anaconda is both the name of the company behind the software and the name of the fully featured software distribution. The Anaconda company offers consulting and three Anaconda editions: individual, team, and enterprise. Individual is common and powerful. It’s not like there are freeware gotchas. 😉 It’s what I use.

Distributions: Anaconda vs Miniconda

You can choose to download and install one of two distributions: Anaconda or Miniconda.

Similarities

Both Anaconda and Miniconda get you conda, the package manager and virtual environment manager in one.

Both can be installed on Windows, Mac, or Linux systems.

Both come with Python and pip installed.

Differences

The Anaconda distribution installs many Python packages and tools that are common for data science. Miniconda does not. It’s the difference between a big, batteries-included download and a minimal download without many packages installed for you.

Miniconda install ranges between 50 and 90 MiB in mid-2020, depending on your operating system .

The Anaconda distribution requires a “minimum 5 GB disc space to download and install” — according to the docs in mid-2020.

The Anaconda distribution installer also includes options to install a GUI and other popular software such as the VSCode text editor.

If you are getting started with scientific computing using Python and have the space on your computer, I suggest you download the Anaconda distribution. It’s a lot of stuff, but it’s good stuff. 👍

If you’ve worked with conda previously, I suggest you download Miniconda. You can add just the packages you need as you go. 😀

See install instructions for Anaconda here and for Miniconda here.

Source: pixabay.com

Conda commands

If you installed the Anaconda distribution you can use the GUI installer, but I suggest using the command line interface to help you work faster. You are a programmer after all (or becoming one)! 😉

Let’s look at common conda commands to create and manage conda environments. Everything is the same whether you installed Anaconda or Miniconda. It’s all conda at this point. 😀

Create conda environments

conda create -n myenv pandas jupyterlab Create a new conda environment named myenv with the latest version of Python available on the main conda channel. Install pandas and jupyterlab packages into the environment. -n is short for --name .

Conda asks for confirmation with package changes. Press y when asked whether you want to proceed.

conda create -n myclone --clone myenv Duplicate the environment named myenv into the new environment named myclone. All the packages come along for the ride! 🚙

conda create -n myenv python=3.8 Create a new conda environment named myenv with the Python version specified. Pip and about 15 other packages are installed.

Only snake picture in this guide, I promise. Source: pixabay.com

Manage conda environments

conda activate myenv Activate the myenv virtual environment.

You can tell you are in an active virtual environment because your command line prompt will start with the name of your virtual environment in parentheses. Like this:

(base) Jeffs-MBP:~ jeffhale$

The default conda environment is named base.

Conda can be set up to activate an environment whenever you open your terminal. When installing the software the installer will ask you if you want to initialize it “by running conda init". I suggest you say yes so that your shell scripts get modified. Then you’ll start your terminal sessions in a conda environment.

Wherever you navigate in your on CLI, you will be within your activated conda virtual environment. 🎉

conda deactivate — Deactivate the current environment. Now you won’t be in a virtual environment. The conda environment you were in still exists, you just aren’t in it. 👍

conda env list — List conda environments.

conda env remove --name myenv — Remove the myenv conda environment. Delete it forever.

Source: pixabay.com

Manage packages in active environment

Use these commands from within an active conda environment. These are the conda commands I use most frequently. 😀

conda list — List installed packages in the active environment. The output includes the package version and the conda channel the package was installed from. 👍

Conda packages

conda install pandas — Download and install the pandas package from the main conda channel.

conda install -c conda-forge pandas — Download and install the pandas package from the main conda-forge channel.

The conda-forge channel is “A community-led collection of recipes, build infrastructure and distributions for the conda package manager.” Use the conda-forge channel when a package doesn't exist on the main channel or the version on the main channel isn't as new as you would like. Packages often get updated on conda-forge before the main conda channel. ☝️

You can also specify some other channel, but conda-forge is where you will find many packages.

conda update pandas — Download and install the latest pandas package from the main conda channel.

conda update all — Download and install the the latest versions of all installed packages. Can be very slow if you have a lot of outdated packages. ⚠️

conda uninstall pandas — Uninstall the pandas package in your conda environment.

Pip

Pip is the most common package installer. It ships with Python.

When in an active conda environment, pip will install packages into that active conda environment. ⚠️

pip install -U pandas — Install or update the pandas package from PyPI, the Python package index. -U specifies to update all dependent packages.

pip uninstall pandas — Uninstall the pandas package that was installed by pip.

PyPI is the most common place to find Python packages. PyPI often has packages that are not on conda or conda-forge. PyPI often gets the latest version of packages first.

Occasionally conda has packages that aren’t on PyPI, but that is less common.

Conda and PyPI packages generally play together nicely. 🎉

Make sure that if you are installing a version of a package from PyPI you don't already have the same package installed from conda. If you do have it installed, then uninstall the conda version first. Otherwise conda won’t use the new PyPI version. By default, the official conda channel is the highest priority channel and conda will use package versions from higher priority channels first.☝️

The official conda recommendation is to try to install packages from the main conda channel first, then conda-forge as necessary, and then pip. This helps avoid conflicts. In general, installing from any of those sources should work fine.

If things ever go sideways

Sometimes things get messed up. Generally uninstalling packages or creating a new conda environment is all you need to fix the situation.

If that doesn’t work, conda update conda updates the conda software itself.

If that doesn’t work, you can uninstall and reinstall conda. 👍

There are lots of conda users out there and lots of documentation, so as always, the search engine is your friend. 😉

Source: pixabay.com

Wrap

I hope you’ve enjoyed this overview of conda. If you did, please share it on your favorite social media so other folks can find it, too. 😀

I write about Python, SQL, Docker, and other tech topics. If any of that’s of interest to you, sign up for my mailing list of awesome data science resources and checkout out some of my 60+ articles you grow your skills here. 👍
['Jeff Hale']
['Machine Learning', 'Data Science', 'Python', 'Artificial Intelligence', 'Technology']