Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Introduction to Python Package Management

Introduction

Learning Objectives

Installing Conda (Miniconda)

Why Miniconda?

Installation

Windows

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o .\miniconda.exe
start /wait "" .\miniconda.exe /S
del .\miniconda.exe

macOS

mkdir -p ~/miniconda3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
mkdir -p ~/miniconda3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -o ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
source ~/miniconda3/bin/activate
conda init --all

Linux

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
source ~/miniconda3/bin/activate
conda init --all

Verifying Installation

conda --version
conda info
conda config --set auto_activate_base false

Understanding Conda Concepts

Environments

Channels

Creating Your First Geospatial Environment

# Create a new environment named 'geo' with Python 3.12
conda create -n geo python=3.12

# Activate the environment
conda activate geo

# Install mamba for faster package management
conda install -n base mamba -c conda-forge

# Install essential packages for geospatial programming
mamba install -c conda-forge pygis

Troubleshooting Conda

conda init cmd.exe

Essential Conda Commands

Creating and Managing Environments

# Basic environment with specific Python version
conda create -n myenv python=3.12

# Environment with multiple packages from the start
conda create -n geoenv python=3.12 numpy pandas matplotlib

# Create environment with packages from specific channels
conda create -n geoenv2 python=3.12 -c conda-forge geopandas
conda activate myenv
conda deactivate
conda env list
# or
conda info --envs
# Remove entire environment and all its packages
conda remove -n myenv --all

# Alternative method using env remove
conda env remove -n myenv
# Create a copy of an existing environment
conda create -n newenv --clone oldenv

Installing and Managing Packages

# Install a package from the main channel
conda install numpy

# Install multiple packages from the main channel
conda install scipy matplotlib seaborn

# Install specific versions
conda install numpy=1.24.0 pandas>=1.5.0
# Install without activating the environment
conda install -n myenv pandas

# Useful for setting up environments remotely
conda install -n geoenv -c conda-forge geopandas rasterio
# Install from conda-forge (recommended for geospatial packages)
conda install -c conda-forge geopandas
# Update all packages in current environment
conda update --all

# Update specific packages
conda update numpy pandas

# Update conda itself
conda update conda
# Search for packages
conda search scikit-learn
conda search "*gdal*"  # wildcard search

# Get package information
conda search -c conda-forge geopandas --info

# List all installed packages
conda list

# List packages matching a pattern
conda list "*geo*"
# Remove a single package
conda remove numpy

# Remove multiple packages
conda remove scipy matplotlib

# Remove packages and their dependencies (if not needed by others)
conda remove numpy --all

Using Mamba (Faster Package Management)

# Install mamba in the base environment (do this once)
conda install -n base mamba -c conda-forge
# These commands are much faster with mamba
mamba create -n geofast python=3.12
mamba activate geofast
mamba install -c conda-forge geopandas rasterio geemap leafmap

# All conda commands work with mamba
mamba list
mamba update --all
mamba remove geopandas

Environment Files for Reproducibility

# Export all packages and versions
conda env export > environment.yml

# Export with specific name
conda env export -n myenv > myenv.yml
# Create environment from exported file
conda env create -f environment.yml

# Create with different name
conda env create -f environment.yml -n newname

Introducing uv: The Fast Alternative

Installing uv

curl -LsSf https://astral.sh/uv/install.sh | sh
pip install uv

Basic uv Usage

# Navigate to your project directory
cd /path/to/your/project

# Create a virtual environment
uv venv

# Create with specific Python version
uv venv --python 3.12

# Activate the environment (varies by OS)
# On macOS/Linux:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activate
# Install packages
uv pip install jupyterlab leafmap

# Install from requirements file
uv pip install -r requirements.txt

# Run Python directly in the environment
uv run python script.py

# Run Jupyter directly
uv run jupyter lab

uv vs pip Performance

Best Practices for Package Management

Environment Management

Package Installation

Collaboration and Reproducibility

Troubleshooting Tips

Key Takeaways

Exercises

Exercise 1: Setting Up Your First Geospatial Environment

Exercise 2: Environment Management and Reproducibility

Exercise 3: Exploring uv for Fast Development