Manage Your Virtual Environments with uv and pipx

Posted on July 15, 2025 in Python

Using uv and pipx for Python Virtual Environments

Managing Python virtual environments efficiently is crucial for clean, isolated project setups. Two tools, uv and pipx, simplify this process. Here's a concise guide with examples.

uv: Fast Virtual Environment Management

uv is a lightweight, Rust-based tool for creating and managing Python virtual environments. It’s faster than venv and integrates package management seamlessly.

Installing uv

pip install uv
Creating a Virtual Environment

Create a virtual environment with a specific Python version (e.g., 3.10):

uv venv --python 3.10 myenv
source myenv/bin/activate  # On Windows: myenv\Scripts\activate
Installing Packages

Install packages directly into the environment:

uv pip install requests
Managing Dependencies

Generate a requirements.txt file:

uv pip freeze > requirements.txt
Install from requirements.txt:
uv pip install -r requirements.txt

Why uv?

Speed: Faster environment creation and package installation. Simplicity: Unified commands for environment and package management. Cross-platform: Works on Windows, macOS, and Linux.

pipx: Isolated CLI Tools pipx installs Python CLI tools in isolated environments, preventing conflicts with project dependencies.

Installing pipx

pip install pipx
pipx ensurepath

Installing a CLI Tool Install a tool like black globally, but in an isolated environment:

pipx install black

Running a Tool Run a tool without activating an environment:

pipx run black myfile.py

Upgrading Tools Upgrade an installed tool:

pipx upgrade black

Why pipx?

Isolation: Tools don’t interfere with project environments. Ease: Run CLI tools without manual environment activation. Clean: Avoids cluttering global Python environments.

Combining uv and pipx Use uv for project-specific environments and pipx for global CLI tools. Example workflow:

Create a project environment with uv:uv venv myproject

source myproject/bin/activate
uv pip install flask

Install black globally with pipx:

pipx install black

Format code in the project:black myproject/

The Python environment is managed more efficiently with uv and pipx. While pipx maintains the accessibility and isolation of CLI tools, uv shines in project-specific environments with quick setup and package handling. When combined, they guarantee Python development that is clear and devoid of conflicts.