Using Conda for a Python Project

What is Conda and why use it?

Conda is an open-source package and environment management system available for Windows, macOS and Linux, systems.

As it is both a package manager and an environment manager Conda simplifies the process of setting up an environment through it’s ability to locate and install packages whilst you are creating and configuring your environment.

With Conda you can easily setup and switch between different environments from a single computer which is handy if you are working on multiple projects that require different and sometimes conflicting dependancies.

Designed originally for Python, you can use Conda to package and distribute software for any programming language.

In this post we will go through an example of using Conda with python.


Example: Fibonacci script


Photo by Adrian Vieriu of a fossilised Ammonite.

The Fibonacci sequence is found throughout nature in the geometry of snail shells, flowers, galaxies and storm patterns (to name a few!).

In a Fibonacci sequence each number is the sum of the two previous numbers.

Below is a python script Fib_example.py that generates a Fibonacci sequence of length n, where n is a number we give the script.

It uses numpy to build an array to store our sequence and creates an indexed data frame using pandas.

The script finally prints out the data frame which contains our indexed Fibonacci sequence. You can download the script from here.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import numpy as np
import pandas as pd

# Function to generate the Fib sequence of length n
def fibonacci(n):
    fib_seq = np.zeros(n, dtype=int)
    # To ensure the sequence starts at 1 - some may object..
    fib_seq[0] = 1
    fib_seq[1] = 1
    for i in range(2, n):
        fib_seq[i] = fib_seq[i-1] + fib_seq[i-2]
    return fib_seq

# Specify the length of Fib sequence (arg[1])
number=sys.argv[1]
number=int(number)

# Generate Fib sequence
sequence=fibonacci(number)

# Create a pandas dataframe to store and index the Fib sequence
sequence_df = pd.DataFrame({'Index': range(number), 'Fibonacci Number': sequence})

# Make it more human readable
sequence_df['Index'] += 1

# Print the dataframe 
print(sequence_df)


Setting up your Conda environment

1. Make a project directory to store your code.

Download the Fib_example.py and make a project directory. You can activate a conda environment anywhere so this is not a requirement, but making a project directory helps to keep your code and project files organised!

2. Create your conda environment.

Let’s use conda create to build your environment. This environment will be where you install necessary packages and programs for your project.

First provide a name for your environment -n and specify the python version via python=

Note there is an option -c which tells conda to install from the specified channel, or location where packages are stored.

So if there are issues with downloading packages you can try to download them from a different conda channel. E.g the conda-forge channel is a GitHub organisation containing repos of conda recipes.

For this example we are just using the default channel, so we do not need to specify -c.

conda create -n fib_example python=3 

3. Activate your Conda environment.

Activate your new conda environment using the command below:

conda activate fib_example

Note that when you activate your conda environment you should see the environment name (fib_example) at the start of your terminal prompt eg:

(fib_example)computername:user$

4. Install your dependancies into your new environment

Now we use conda install to install the necessary packages, in this case we are installing numpy and pandas packages necessary to run the Fib_example.py script.

conda install numpy pandas

4. Run your program.

Finally you can try running the script!

Usage: python3 Fib_example.py 20

When you have finished you can deactivate your conda environment using:

conda deactivate


Using yaml files

To make it even easier to share our environments we can create a yaml file.

The yaml file provides all the same information we just typed into the command-line but this time we only need to run conda create to create our conda environment with our yaml file as input.

This makes the process of setting up a conda environment faster, particularly when we have multiple dependancies. It’s helpful for our colleagues too!

So, let’s make a quick yaml file:

name: Fib_example
channels:
  - defaults
  - conda-forge
dependencies:
  - python=3.8
  - numpy
  - pandas

In this example I have added the conda-forge channel to illustrate that we can add multiple channels here.

Now we can create the conda environment

conda env create -f Fib_example.yml

Remember we still need to activate the conda environment to use it.

conda activate Fib_example

Try running the script again and see if it works.

python3 Fib_example.py 20
    Index  Fibonacci Number
0       1                 1
1       2                 1
2       3                 2
3       4                 3
4       5                 5
5       6                 8
6       7                13
7       8                21
8       9                34
9      10                55
10     11                89
11     12               144
12     13               233
13     14               377
14     15               610
15     16               987
16     17              1597
17     18              2584
18     19              4181
19     20              6765

Note that if you want to remove your environment later on you can use conda remove after deactivating your environment:

conda deactivate
conda remove -n Fib_example --all