DEV Community

Cover image for Python Environment Setup: venv vs uv (A Practical Guide for Modern Developers)
Alex Murithi
Alex Murithi

Posted on Edited on

Python Environment Setup: venv vs uv (A Practical Guide for Modern Developers)

Whether you're building an API, ETL pipeline, data analysis project, automation script, applications, or desktop tool, setting up your development environment is the first step before writing code.

The two common approaches explored in this article:

  1. The traditional venv + pip workflow
  2. The modern uv workflow

Why Environment Setup Matters

A properly configured development environment helps you:

  • Isolate project dependencies
  • Prevent version conflicts
  • Reproduce projects across different machines
  • Share dependencies with teammates
  • Maintain cleaner repositories
  • Follow industry best practices

Without an isolated environment, a package installed for one project can accidentally break another project.

Understanding Virtual Environments

A virtual environment is an isolated Python workspace containing its own:

  • Python interpreter
  • Installed packages
  • Project dependencies

Instead of installing libraries globally on your machine, they are installed only for the project that needs them.

For example:

Project A → pandas 2.2
Project B → pandas 1.5
Enter fullscreen mode Exit fullscreen mode

Because each project has its own environment, they can coexist without conflicts.

Approach 1: Using venv (The Traditional Workflow)

For years, venv has been the standard way to create isolated Python environments.

The module ships with Python, making it available on virtually every installation.

i. Create a Project Directory

mkdir my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

ii. Create a Virtual Environment

python -m venv venv
Enter fullscreen mode Exit fullscreen mode

This creates a folder called venv that contains the isolated environment.

iii. Activate the Environment

Git Bash

source venv/Scripts/activate
Enter fullscreen mode Exit fullscreen mode

PowerShell

.\venv\Scripts\Activate.ps1
Enter fullscreen mode Exit fullscreen mode

Command Prompt

venv\Scripts\activate.bat
Enter fullscreen mode Exit fullscreen mode

After activation, your terminal prompt should look similar to:

(venv)
Enter fullscreen mode Exit fullscreen mode

iv. Upgrade pip

It's good practice to update the package manager before installing dependencies.

python -m pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode

v. Install Packages

pip install pandas requests
Enter fullscreen mode Exit fullscreen mode

vi. Save Dependencies

Create a requirements file:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Example:

pandas==2.3.0
requests==2.32.0
Enter fullscreen mode Exit fullscreen mode

vii. Recreate the Environment

If another developer clones the repository:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

The same dependencies will be installed.

Typical venv Project Structure

my-project/
│
├── venv/
├── data/
├── output/
├── src/
│   └── main.py
│
├── requirements.txt
├── README.md
└── .gitignore
Enter fullscreen mode Exit fullscreen mode

Limitations of venv

As projects grow, developers may encounter:

  • Slower package installation
  • Manual dependency tracking
  • Multiple tools for environment management
  • Extra setup steps

This is where modern tools like uv come in.


Approach 2: Using uv (The Modern Workflow)

uv is a fast Python package and project manager written in Rust.

It combines several common Python development tasks into a single tool, providing a cleaner developer experience.

Many developers now use uv for new projects because it simplifies dependency management and significantly speeds up package installation.

i. Install uv

Windows

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Enter fullscreen mode Exit fullscreen mode

Verify Installation

uv --version
Enter fullscreen mode Exit fullscreen mode

ii. Create a New Project

Initialize a project:

uv init my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

This automatically generates a modern Python project structure.

iii. Create a Virtual Environment

uv venv
Enter fullscreen mode Exit fullscreen mode

The environment is typically created in:

.venv/
Enter fullscreen mode Exit fullscreen mode

iv. Activate the Environment

Git Bash

source .venv/Scripts/activate
Enter fullscreen mode Exit fullscreen mode

PowerShell

.\.venv\Scripts\Activate.ps1
Enter fullscreen mode Exit fullscreen mode

v. Install Dependencies

uv add pandas requests
Enter fullscreen mode Exit fullscreen mode

Unlike the traditional workflow, dependencies are automatically recorded for you.

vi. Run Python Programs

uv run main.py
Enter fullscreen mode Exit fullscreen mode

You can execute scripts directly inside the project's environment.

vii. Sync Dependencies

When moving between machines or cloning a project:

uv sync
Enter fullscreen mode Exit fullscreen mode

This recreates the project's environment based on its configuration files.

Typical uv Project Structure

my-project/
│
├── .venv/
├── src/
│   └── main.py
│
├── pyproject.toml
├── uv.lock
├── README.md
└── .gitignore
Enter fullscreen mode Exit fullscreen mode

Important Files Explained

requirements.txt

Used primarily in traditional Python projects.

Example:

pandas==2.3.0
requests==2.32.0
Enter fullscreen mode Exit fullscreen mode

Dependencies are installed with:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

pyproject.toml

The modern standard for Python project configuration.

Example:

[project]
name = "my-project"
version = "0.1.0"
dependencies = [
    "pandas",
    "requests"
]
Enter fullscreen mode Exit fullscreen mode

uv.lock

Locks dependency versions to ensure consistent environments across machines.

This helps eliminate the classic "works on my machine" problem.

.gitignore

Whether you use venv or uv, avoid committing environment files to GitHub.

# Virtual environments
venv/
.venv/

# Python cache
__pycache__/
*.pyc

# IDE settings
.vscode/

* Environment variables
.env

# Log**logs/

# OS files
.D*_Store
Thumbs.db
Enter fullscreen mode Exit fullscreen mode

venv vs uv

Feature venv uv
Package Installation Speed Standard Very Fast
Dependency Management Manual Automatic
Workflow Traditional Modern
Project Initialization Manual Built-in

Final Thoughts

A properly managed environment leads to:

  • Cleaner projects
  • Reproducible builds
  • Easier collaboration
  • Fewer dependency issues
  • Better development habits

The fundamentals remain the same. The tools are simply evolving to make the process faster, cleaner, and more reliable.

Top comments (0)