2. Introduction to Python Package Management#

2.1. Introduction#

2.2. Learning Objectives#

2.3. Installing Conda (Miniconda)#

2.3.1. Why Miniconda?#

2.3.2. Installation#

2.3.2.1. Windows#

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

2.3.2.2. 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

2.3.2.3. 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

2.3.3. Verifying Installation#

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

2.4. Understanding Conda Concepts#

2.4.1. Environments#

2.4.2. Channels#

2.5. 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

2.6. Troubleshooting Conda#

conda init cmd.exe

2.7. Essential Conda Commands#

2.7.1. 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

2.7.2. 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

2.7.3. 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

2.7.4. 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

2.8. Introducing uv: The Fast Alternative#

2.8.1. Installing uv#

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

2.8.2. 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

2.8.3. uv vs pip Performance#

2.9. Best Practices for Package Management#

2.9.1. Environment Management#

2.9.2. Package Installation#

2.9.3. Collaboration and Reproducibility#

2.9.4. Troubleshooting Tips#

2.10. Key Takeaways#

2.11. Exercises#

2.11.1. Exercise 1: Setting Up Your First Geospatial Environment#

2.11.2. Exercise 2: Environment Management and Reproducibility#

2.11.3. Exercise 3: Exploring uv for Fast Development#