1. Home
  2. Blog
  3. Software Development

Software Development

Python Virtual Environment Tutorial: venv, pip and uv

Stop dependency conflicts for good. Learn how to create, activate and manage a Python virtual environment, then try the faster uv tool.

7 min read
Python virtual environment tutorial: create and manage venv, pip and uv

Key takeaways

  • Create: python -m venv .venv
  • Activate: .venv\Scripts\activate (Windows) or source .venv/bin/activate (macOS/Linux)
  • Install with pip install package; save with pip freeze > requirements.txt
  • Never commit the environment folder to Git.
  • uv is a fast alternative for creating environments and installing packages.

You install a package for one project and suddenly another project stops working. That is the classic dependency conflict, and it is why every developer needs a Python virtual environment.

A virtual environment gives each project its own isolated set of packages, so versions never collide. It takes about 30 seconds to set up and saves hours of debugging. In this tutorial you will create and activate one with venv, install packages with pip, save dependencies, try uv and fix common errors.

What is a Python virtual environment?

Without one, pip install puts packages in your global Python. Project A may need requests 2.28 while project B needs a newer version, and only one can win. A virtual environment is a folder with its own interpreter link and its own site-packages, so each project keeps the versions it needs.

Test automation projects pin versions of Selenium, Playwright or pytest so results are repeatable on every laptop and CI server. A virtual environment plus a requirements file is the foundation of that. New to the language? Start with our Python tutorial for beginners.

Create a Python virtual environment with venv

Step 1: check your Python version

python --version

On some systems the command is python3. The venv module ships with modern Python, so no extra install is needed. See the official venv documentation.

Step 2: create the environment

cd my-project
python -m venv .venv

This creates a .venv folder in your project. The name is a convention that many tools and editors detect automatically.

Step 3: activate it

System Command
Windows (PowerShell) .venv\Scripts\Activate.ps1
Windows (cmd) .venv\Scripts\activate.bat
macOS / Linux source .venv/bin/activate

When active, your prompt shows (.venv). Type deactivate to leave. If PowerShell blocks the script, allow local scripts for your user with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.

Install packages in your Python virtual environment with pip

pip install requests
pip list
pip install --upgrade requests

Check you are using the environment’s pip with pip --version; the path should point inside .venv. The pip user guide covers more options.

Save and restore dependencies

pip freeze > requirements.txt
# later, on another machine
python -m venv .venv
pip install -r requirements.txt

Commit requirements.txt so teammates and CI can rebuild the same environment. Not sure about commits? Read our Git tutorial for beginners. Add .venv/ to your .gitignore, because environments are large, machine specific and easy to recreate.

Try uv: a faster alternative

uv is a fast Python package and project manager written in Rust. It creates environments and installs packages much faster than plain pip and works with the same requirements.txt files.

uv venv
uv pip install requests
uv pip freeze > requirements.txt

Quick tip: If pip and venv already work for you, you do not need to switch. Consider uv when installs are slow, such as in large CI pipelines, and test it on a branch first.

Using a Python virtual environment in real projects

  • VS Code: press Ctrl+Shift+P, choose “Python: Select Interpreter” and pick the .venv interpreter.
  • CI pipelines: create a fresh environment, install from the requirements file and run tests, as in our GitHub Actions CI/CD guide.
  • Containers: the same idea applies in images; see our Docker tutorial for beginners.
  • API and test scripts: a small requests project in its own environment pairs well with API testing with Postman.

Common errors and fixes

Problem Fix
“python is not recognized” Reinstall Python with “Add Python to PATH” ticked, or use the py launcher on Windows
Script execution disabled in PowerShell Use the RemoteSigned command above, or activate via cmd
Packages install globally You forgot to activate; look for (.venv) in the prompt
Broken environment after moving the folder Delete .venv and recreate it from requirements.txt
Wrong Python version Create it with the exact interpreter, such as python3.12 -m venv .venv

Best practices: one environment per project, name it .venv in the project root, pin dependencies, ignore the folder in Git, recreate rather than repair, and document setup steps in the README.

Final thoughts

A Python virtual environment is the simplest habit that makes projects reproducible. Create one with python -m venv .venv, activate it, install what you need and freeze your dependencies. Once that feels natural, experiment with uv for speed, then put your environment to work on a small automation script or test project.

Frequently asked questions

How do I create a virtual environment in Python?

Run python -m venv .venv in your project folder, then activate it with the script for your operating system.

How do I activate a virtual environment on Windows?

In PowerShell run .venv\Scripts\Activate.ps1; in Command Prompt run .venv\Scripts\activate.bat.

What is the difference between venv and virtualenv?

The venv module is built into Python 3, while virtualenv is a third-party tool with extra features and older-Python support. For most projects, venv is enough.

Should I commit my virtual environment to Git?

No. Commit requirements.txt instead and add the environment folder to .gitignore.

Is uv better than pip?

uv is usually much faster and works with common pip workflows, but pip remains the standard and is fine for most projects.

Keep learning

Work with Stack Quora

Need help with testing, WordPress or SEO?

We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Databases & SQL

SQL GROUP BY and HAVING Explained With Examples

SQL GROUP BY and HAVING explained with sample tables, aggregate functions, WHERE vs HAVING and common mistakes, with queries you…

Read article →
Software Development

HTTP Status Codes Cheat Sheet for Developers and Testers

An HTTP status codes cheat sheet for developers and testers: every common 1xx to 5xx code, what it means and…

Read article →
Test Automation

Selenium WebDriver Waits Explained: Implicit, Explicit and Fluent

Selenium waits explained: implicit, explicit and fluent waits with Python examples, when to use each and how to remove flaky…

Read article →
Software Testing

Test Automation Mistakes Beginners Make and How to Avoid Them

The 10 test automation mistakes beginners make, from flaky waits to automating everything, and the practical fixes that keep your…

Read article →
Artificial Intelligence

Claude Opus 5.5 vs Sonnet 5.5: Pricing, Speed and When to Use Each

Claude Opus 5.5 vs Sonnet 5.5 compared: listed pricing, context, speed and a simple guide to which model fits coding,…

Read article →
Artificial Intelligence

Best AI Code Review Tools for Developers in 2026

Compare the best AI code review tools in 2026: CodeRabbit, GitHub Copilot, Qodo, Greptile and more. See what each does,…

Read article →
Software Development

What Is an API? A Simple Guide for Beginners

What is an API? Learn in plain English how APIs work, the main types, HTTP methods and status codes, with…

Read article →
Test Automation

Playwright Locators Cheat Sheet With Examples

A practical Playwright locators cheat sheet: getByRole, getByText, getByLabel, test IDs, CSS, filtering and chaining, with copy-paste examples for stable…

Read article →
Software Testing

How to Write Test Cases: Format, Examples and Best Practices

Learn how to write test cases with a clear format, login and checkout examples, a reusable template and best practices…

Read article →
Artificial Intelligence

Gemini 4 Argon: What’s New and How It Compares to Earlier Gemini Models

Gemini 4 Argon is Google's new flagship reasoning model. See its API limits, app features, coding focus and how to…

Read article →
Databases & SQL

SQL Joins Explained With Examples (Inner, Left, Right, Full)

SQL joins explained step by step with sample tables and queries: INNER, LEFT, RIGHT, FULL, CROSS and SELF joins, plus…

Read article →
Software Development

Python Virtual Environment Tutorial: venv, pip and uv

Learn how to create, activate and manage a Python virtual environment with venv, pip and uv, including requirements files, .gitignore…

Read article →
Test Automation

Playwright vs Cypress in 2026: Which Should You Pick?

Playwright vs Cypress compared for 2026: browsers, languages, speed, debugging, parallel runs, CI and cost. Find out which end-to-end testing…

Read article →
Software Testing

Smoke Testing vs Sanity Testing: Differences With Real Examples

Smoke testing checks that a build is stable enough to test. Sanity testing checks that a specific fix works. Learn…

Read article →
Artificial Intelligence

Claude Haiku 5.5: Features, Pricing and Use Cases for Developers

Claude Haiku 5.5 is Anthropic's fastest and lowest-priced model. See its 1M-token context, pricing, API changes and the best use…

Read article →
Software Testing

End to End Testing Best Practices: Boost Reliability in 2026

Flaky tests waste time and hide real bugs. Learn end to end testing best practices that make your tests stable,…

Read article →
Test Automation

Best Test Automation Framework 2026: Choose the Right One

Not sure which test automation framework to pick? Compare the main types, use a simple checklist, and see how Playwright,…

Read article →
Artificial Intelligence

AI Agents Best Practices: Deploy Reliable Intelligent Bots

AI agents can save hours of work, but only when they are built with care. Learn AI agents best practices…

Read article →
Test Automation

Test Automation Framework Example: Build a Scalable Suite

See a real test automation framework example with folder layout, page objects, test data and CI steps. Copy the structure…

Read article →
Software Testing

Best Test Automation Tools 2026: Top Picks for QA Teams

Compare the best test automation tools in 2026 for web, mobile and API testing. See what each tool does well,…

Read article →
Software Development

REST API Best Practices 2026: Build Scalable Services

Learn REST API best practices for 2026: clear URLs, correct status codes, versioning, pagination, security and error handling, with simple…

Read article →
Artificial Intelligence

MCP Stateless Architecture: What’s New in MCP 2026

The MCP 2026-07-28 spec removes sessions and the handshake. See how stateless MCP works, what breaks, what gets easier to…

Read article →
Artificial Intelligence

WebMCP Explained: How AI Agents Will Interact With Websites

WebMCP lets a website publish tools that AI agents can call directly. Learn the Declarative and Imperative APIs, code examples,…

Read article →
Databases & SQL

SQL Tutorial for Beginners: Complete Guide to SQL in 2026

Learn SQL from scratch. Create tables, insert and query data, filter, sort, group, join tables and use subqueries and indexes,…

Read article →
Software Development

Git Tutorial for Beginners: Complete Guide to Git and GitHub in 2026

Learn Git from zero. Create repositories, commit, branch, merge, fix mistakes and work with GitHub pull requests, using simple commands…

Read article →
DevOps

Docker Tutorial for Beginners: Complete Guide to Docker in 2026

New to Docker? Learn containers, images, Dockerfiles, volumes, networks and Docker Compose with simple commands and a small project you…

Read article →
Databases & SQL

Vector Databases Explained: How Vector Search and AI Databases Work in 2026

Vector databases search by meaning, not just keywords. Learn embeddings, similarity search, HNSW indexes, pgvector and how to choose the…

Read article →
Software Testing

API Testing with Postman: A Complete Guide for Beginners and QA Testers

Learn API testing with Postman from scratch. Send requests, read status codes, add auth, write test scripts, build collections and…

Read article →
DevOps

GitHub Actions CI/CD: A Complete Guide to Automating Software Builds, Tests and Deployments

Learn GitHub Actions from scratch. Build a CI/CD workflow that runs tests, builds your app and deploys it, with simple…

Read article →