← Other topics

Miniconda / Conda for Python

Video Notes

Conda is a command line tool geared towards Python developers that lets us manage environments and packages.

Conda is very similar to pip, Python’s default package manager, but it differs on two key points:

  1. Conda can be used to install packages from other languages, not just Python
  2. Conda contains environment management functionality, while pip does not

Conda and pip are not mutually exclusive, and are often used in combination with one another. This is especially true because there are certain packages only available via pip’s package repository (PyPI Python Package Index), and others that are only available via Conda’s package repository (conda-forge).

In this guide, we’ll focus on Conda, but we’ll also see how it can be used in conjunction with PIP.

Our goals

  1. Install Conda
  2. Create a new environment
  3. Download Python and some example packages
  4. Execute an example Python script that uses those packages

Install Conda

Step 1 is to install Conda. There are two ways to do this:

  1. Download Anaconda - a bundle of software that includes not only Conda but a bundle of utilities for working with Python with a focus on scientific computing
  2. Download Miniconda - A smaller alternative to Anaconda that includes just the Conda command line utility.

This guide will focus on Miniconda, but if you want to learn more about Anaconda, check out these guides:

Knowing we’ll be focussing on Miniconda, head to https://docs.anaconda.com/free/miniconda and find the appropriate installer for your computer.

Mac users, choose the installer that matches your processor (either Intel or Apple M). Of the options pkg or bash, choose pkg.

Windows users, there is only one option so choose that.

Miniconda Installer

After downloading the installer, run it and it will walk you through a series of install screens.

You can leave all the options as the defaults except for the fifth screen in the Window’s installer. Here, you want to check off “Add miniconda3 to my PATH environment variable”

Post install

After the installer is complete, open your command line program (e.g. Terminal on Mac or Git Bash) on Windows and run the following command to initialize Conda:

> conda init

After running this command, close and reopen your command line program to make the changes take effect.

Environments

Now that Conda is installed, let’s put it to use starting with environments. In Conda, an environment is an isolated workspace that allows you to manage and organize installations of Python and its packages. Environments help avoid conflicts between different projects that may have different version requirements for their dependencies. Environments also make it easy to reproduce workspaces which is helpful if you’re working on a project with a team of other developers.

To create your first environment, we’ll call it demo, run the following in command line:

> conda create --name demo

Once your environment is created activate it with the conda activate command:

> conda activate demo

Your command prompt should be prefixed with the name of the currently active environment, e.g.:

Conda prefixes your prompt with the name of the currently active environment

Install packages

Within our new environment we can install Python with the conda install command:

> conda install python

In the example program we’re about to create we’ll also need packages called numpy and matplotlib, so install them as well:

> conda install numpy
> conda install matplotlib

To find and learn about other packages you might use in a Python project, check out the following repositories:

  • Conda Forge The community curated package repository for Conda
  • PIP A popular package manager/repository used in Python; pip is installed when you install Python and you can add pip packages via the command pip install <package-name>

Note that in the above example we installed numpy and matplotlib via Conda, but those two packages are also available via pip, so we could have also installed them with the commands pip install numpy and pip install matplotlib.

Run example program

With your environment set up, let’s run an example Python script that will draw a sine wave using the packages we installed above, numpy and matplotlib.

I’ll create this program on my Desktop and name it demo.py:

import numpy as np
import matplotlib.pyplot as plt

# Generate x values from 0 to 2*pi with a small interval
x_values = np.arange(0, 2 * np.pi, 0.1)

# Calculate corresponding y values for a sine wave
y_values = np.sin(x_values)

# Plot the sine wave
plt.plot(x_values, y_values, label='Sine Wave')

# Add labels and a legend
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sine Wave Plot')
plt.legend()

# Show the plot
plt.show()

With the file created, we can run it with this command:

> python demo.py

This should generate a Python window with a graph of a sine wave:

Sine wave generated in Python with Matplotlib

Quick Reference

Create an environment:

> conda create --name myenv

Activate an environment:

> conda activate myenv

Deactivate the current environment:

> conda deactivate

List all environments:

> conda info --envs

Remove an environment:

> conda remove --name myenv --all 

Install a new package

> conda install packagename

List installed packages:

> conda list

List installed packages, filtering results by a package name

> conda list packagename
If this info helped you out, you can send a tip via PayPal. Thanks for your support!
← Other topics