Post

Environment manager for Python

Why do you need an environment manager for Python?

  • Version Control
  • Ease of Deployment
  • Avoiding Conflicts

Python Virtual Environment (venv):

The venv module supports creating lightweight “virtual environments”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Create new environment
python -m venv myenv

# Activate environment
# On Windows:
myenv\Scripts\activate
# On Unix/MacOS:
source myenv/bin/activate

# Deactivate environment
deactivate

# Remove the virtual environment (delete the folder)
rm -rf myenv

Anaconda

Anaconda provides package, dependency, and environment management for any language.

  • Windows: setting system environment variables path
    • C:\ProgramData\anaconda3
    • C:\ProgramData\anaconda3\Library\bin
    • C:\ProgramData\anaconda3\Scripts
    • C:\ProgramData\anaconda3\Lib\site-packages

restart PC

Basic Commands

1
2
3
4
5
6
# Check Anaconda version
conda --version

# Update Anaconda
conda update conda
conda update anaconda

Disable Automatic Activation

To prevent Conda from being automatically activated as the default terminal environment when you open a terminal, you can run the following command in terminal:

1
conda config --set auto_activate_base false

Environment Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Create new environment
conda create --name myenv python=3.11

# Activate environment
conda activate myenv

# List all environment
conda env list

# Deactivate environment
conda deactivate

# Remove an environment
conda remove --name myenv --all

Package Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Install packages
conda install package_name

# Update a package
conda update package_name

# Remove a package
conda remove package_name

# List all installed packages
conda list

# Search for a package
conda search package_name

Environment Export and Import

1
2
3
4
5
# Export an environment
conda env export > environment.yml

# Create an environment from a file
conda env create -f environment.yml

uv

uv is an extremely fast Python package and project manager, written in Rust.

The steps to use:

  1. Install uv
  2. Create a new project
  3. Create a new environment in the project folder
  4. Install the required packages
  5. Add the package dependencies
  6. Run program with uv

Install & Using uv

1
2
# Install uv
pip install uv

Project management

1
2
3
4
5
6
7
8
9
10
# Create new project
uv init new_project

cd new_project

# Create new environment
uv venv

# run a script
uv run hello.py

Python management

1
2
3
4
5
6
7
8
# Install multiple Python versions
uv python install 3.10 3.11 3.12

# Set a Python version in the current directory
uv python pin 3.11

# List all python version
uv python list

Install package

1
2
3
4
5
# Install packages (much faster than pip)
uv pip install numpy pandas

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

Managing package dependencies

1
2
3
4
5
6
7
8
9
10
11
# Specify a version constraint
uv add 'package_name==2.31.0'

# Add a git dependency
uv add git+https://github.com/package_link

# remove a package
uv remove package_name

# upgrade a package
uv lock --upgrade-package package_name

Summary

Featurevenvcondauv
PurposeBasic virtual environmentsComplete environment managementFast package management
Speed★★★★★
Key StrengthBuilt into PythonHandles all dependenciesUltra-fast installations
Package SourcePyPI onlyConda + PyPIPyPI only
Multiple Python VersionsNoYesYes
Main Use CaseSingle Python projectsData Science & Scientific ComputingModern Python development

This post is licensed under CC BY 4.0 by the author.