Skip to content
New guide: build a RAG chatbot over your own docs →

Python Virtual Environments Explained: venv in Plain English

Learn how Python virtual environments work, create one with venv, manage packages with pip, and explore uv for modern Python projects.

Python Virtual Environments Explained: venv in Plain English

Installing a Python package seems simple until two projects need different versions of the same library. One application runs perfectly, while another suddenly breaks after an innocent upgrade. Python virtual environments solve this problem by giving each project its own isolated space for dependencies. This guide explains how venv works, how to create and manage environments on Windows, macOS, and Linux, and when newer tools like uv make sense.

Quick Answer: A Python virtual environment is an isolated workspace that lets a project use its own Python packages without interfering with other projects. Python includes a built-in tool called venv to create these environments. Each environment has its own package installation directory and Python executable, making it easier to manage dependencies, avoid version conflicts, and reproduce projects on different computers.

What Is a Python Virtual Environment?

A Python virtual environment is a project-specific Python installation environment. It uses an existing Python installation as its foundation while keeping installed packages separate from your other projects.

Imagine you are building two applications. The first uses Django 5.2, while the second needs a newer Django release. Installing dependencies globally can create conflicts when both applications expect different package versions.

Virtual environments let each application maintain its own dependencies. Upgrading Django in one environment does not automatically change Django in the other.

How Virtual Environments Work

When you create a virtual environment, Python generates a directory containing an environment-specific Python executable, configuration files, and space for installed packages.

According to the official Python documentation, venv has been included in the standard library since Python 3.3. Python 3.13 also introduced automatic Git ignore file creation for environments, a behavior retained in Python 3.14.

The environment references a base Python installation rather than duplicating your entire operating system or Python installation. This makes it much lighter than a virtual machine.

For example, your workspace might contain these directories:

  • website-project/.venv/ contains dependencies for a Flask website.
  • data-project/.venv/ contains pandas, NumPy, and other analysis libraries.
  • automation-project/.venv/ contains Requests and Beautiful Soup for automation scripts.

Each project can use different package versions without requiring separate computers or operating systems.

Why Should You Use Python Virtual Environments?

Virtual environments solve three common development problems: conflicting dependencies, unpredictable upgrades, and inconsistent project setups.

1. Prevent Dependency Conflicts

Suppose one project requires Requests 2.31 while another expects Requests 2.32. Installing both versions into one global package directory is not practical.

Separate environments allow both projects to work with their required versions. This becomes particularly useful when maintaining older applications alongside newer projects.

2. Keep Your System Python Clean

Some Linux distributions depend on their system Python installation for operating system utilities. Installing or upgrading global packages can interfere with that environment.

Virtual environments reduce this risk by keeping project packages separate. They also help avoid externally managed environment errors that can appear when attempting global pip installations on modern Linux systems.

3. Make Projects Easier to Reproduce

A virtual environment keeps local dependencies organized, but sharing the environment directory itself is not recommended.

Instead, teams record dependencies in a requirements.txt file or use a project configuration and lockfile. Another developer can recreate the environment using compatible Python and package versions.

If you are learning the fundamentals, our guide to start programming in 2026 explains how to build practical programming habits from the beginning.

How to Create a Python Virtual Environment Using venv

Python 3.13 and 3.14 both include venv, so you do not need to install a separate virtual environment package for this approach.

The steps below use a directory named my-python-project and a virtual environment named.venv. This naming convention is widely supported by development tools.

Step 1: Check Your Python Installation

Open Windows Terminal, PowerShell, or a terminal on macOS or Linux. Check your installed version:

Windows: py –version

macOS/Linux: python3 –version

Python 3.14 became a stable release on October 7, 2025, according to the Python Software Foundation. Python 3.13 remains useful for projects whose dependencies have not yet been verified against newer versions.

Step 2: Create Your Project Directory

Use these terminal commands:

mkdir my-python-project

cd my-python-project

This keeps your source code and environment in one project directory.

Step 3: Create the Virtual Environment

Run the appropriate command for your operating system:

Windows: py -m venv.venv

macOS/Linux: python3 -m venv.venv

The -m option tells Python to run its built-in venv module. The final argument specifies the directory where the environment will be created.

If you need a specific installed Python version, you can use the Windows command py -3.13 -m venv.venv or an available version-specific executable such as python3.13 on macOS or Linux.

Step 4: Activate the Environment

Activation changes your terminal’s command lookup path so that Python and pip commands use the environment rather than your default installation.

Platform Activation Command
Windows PowerShell .venv\Scripts\Activate.ps1
Windows Command Prompt .venv\Scripts\activate.bat
macOS/Linux Bash or Zsh source.venv/bin/activate

After activation, many terminal prompts display (.venv) before the current directory. This is a convenient indicator, although prompt customization can hide it.

Step 5: Install a Python Package

With the environment activated, install a package using pip:

python -m pip install requests

Using python -m pip is generally preferable to calling pip directly because it explicitly selects the pip module belonging to the Python interpreter you are using.

Check the installed packages with python -m pip list. You should see Requests and its required dependencies.

If Windows cannot find pip, follow our troubleshooting guide to fix pip not recognized.

Step 6: Deactivate the Environment

When finished, run:

deactivate

Your terminal returns to its previous Python command lookup configuration. The installed packages remain inside.venv, ready for the next session.

Python venv vs pip vs uv: What Is the Difference?

These tools handle related tasks, but they are not interchangeable. Understanding their responsibilities makes Python project management considerably easier.

Tool Main Purpose Typical Command
venv Creates isolated Python environments python -m venv.venv
pip Installs and manages Python packages python -m pip install flask
uv Manages environments, dependencies, and Python versions uv sync

When Should You Use venv and pip?

Choose venv and pip when learning Python, following existing tutorials, or maintaining projects that use requirements.txt. Both are established tools, and venv ships with Python.

This combination also makes it easier to understand what happens during environment creation and dependency installation.

When Does uv Make More Sense?

uv, developed by Astral, is a modern Python package and project manager written in Rust. It supports environment creation, dependency resolution, Python version management, and lockfiles.

According to Astral’s documentation, uv can create a default environment using uv venv. For projects using pyproject.toml, uv sync installs dependencies into the project’s environment and synchronizes them with its lockfile.

uv is useful when working across multiple projects or maintaining reproducible development workflows. However, beginners should still understand virtual environment fundamentals before depending entirely on automated tooling.

How to Manage Dependencies with requirements.txt

Creating an environment is only the beginning. You also need a practical way to record which packages your application depends on.

After installing your packages, generate a dependency list:

python -m pip freeze > requirements.txt

This records installed package versions, including transitive dependencies, in a text file.

For example, a simple Flask application might contain Flask and several supporting packages. Recording exact versions helps another developer recreate a comparable environment.

Recreate an Environment on Another Computer

After cloning a project, create and activate a new virtual environment. Then install its recorded dependencies:

python -m pip install -r requirements.txt

This approach works for small projects and many established Python applications.

For more complex projects, tools such as uv can use pyproject.toml and uv.lock to manage dependencies with stronger consistency across installations.

Remember that a requirements file does not guarantee identical behavior across operating systems or Python versions. Native dependencies, platform differences, and external services may still affect reproducibility.

Common Python Virtual Environment Mistakes

Most venv problems come from using the wrong interpreter, installing packages outside the intended environment, or misunderstanding which files belong in version control.

Installing Packages into the Wrong Python

If a package installs successfully but your application reports ModuleNotFoundError, verify the interpreter being used.

Run python -m pip –version and python -c “import sys; print(sys.executable)” to inspect the relevant paths.

In Visual Studio Code, select the correct interpreter through the Python interpreter selection command. Otherwise, the editor may run a different Python installation from your activated terminal.

Committing the.venv Directory to Git

Do not normally commit virtual environment directories to Git repositories. They contain installed dependencies, platform-specific files, and interpreter paths that may not work on another computer.

Add.venv/ to your.gitignore configuration. Python 3.13 and newer versions create a Git ignore file inside environments by default, but explicitly ignoring.venv/ in your project’s root configuration remains sensible.

Moving an Existing Virtual Environment

Virtual environments are generally not portable between directories or machines. Installed scripts can reference absolute interpreter paths.

If you move a project, recreate its environment and reinstall dependencies rather than copying the old environment directory.

Confusing Virtual Environments with Docker

A virtual environment isolates Python packages, not an entire operating system. Docker containers provide broader application-level isolation, including filesystem layers and runtime configuration.

For a local Flask project, venv may be sufficient. A deployed application needing a specific system library, service configuration, or containerized workflow may benefit from Docker as well.

Best Practices for Python Projects in 2026

A few consistent habits prevent most dependency problems, regardless of whether you use Flask, Django, FastAPI, pandas, or automation scripts.

  • Use one environment per project. Avoid sharing dependencies between unrelated applications.
  • Name the environment.venv. It is a recognizable convention supported by popular editors and tooling.
  • Record your dependencies. Use requirements.txt or a project lockfile when appropriate.
  • Specify compatible Python versions. Test before upgrading an established project from Python 3.13 to 3.14.
  • Recreate rather than transfer. Treat environments as generated files, not permanent project assets.
  • Verify your interpreter. Check the selected Python installation before troubleshooting missing imports.

These practices apply whether your project is a simple automation script or a production API. If you are choosing your first programming language, our Python vs JavaScript comparison explains the practical differences between their ecosystems.

Frequently Asked Questions

1. Do I need a virtual environment for every Python project?

You should generally create a separate virtual environment for every Python project that uses third-party packages. It prevents dependencies from interfering with one another and makes installations easier to reproduce. Very small scripts that only use Python’s standard library may not need one, but adopting the habit early helps avoid future problems.

2. What is the difference between venv and virtualenv?

venv is Python’s built-in virtual environment module, while virtualenv is a separately installed third-party tool. Both create isolated environments, but virtualenv offers additional configuration options and supports certain workflows involving older Python versions. For most new projects using Python 3.13 or 3.14, the standard venv module is a practical starting point.

3. How do I know if my Python virtual environment is activated?

Many terminals display an environment name such as (.venv) when activation succeeds. For a more reliable check, run python -c “import sys; print(sys.prefix!= sys.base_prefix)”. If it prints True, that interpreter is running inside a virtual environment. This check works even when your terminal prompt does not display an environment name.

4. Can I delete a Python virtual environment safely?

Yes. You can delete a virtual environment directory without removing your project’s source code, provided the directory does not contain files you intentionally stored there. Deactivate the environment first, then remove its folder. You can recreate it with venv and reinstall dependencies from requirements.txt or your project’s dependency configuration.

5. Is uv better than venv and pip in 2026?

uv can simplify and speed up dependency management, especially for projects using pyproject.toml and lockfiles. It also handles Python installations and virtual environment creation. However, venv and pip remain useful, straightforward tools for learning and maintaining established projects. The better choice depends on your workflow, compatibility requirements, and preferred level of automation.

Conclusion

Python virtual environments are one of the simplest ways to make development more reliable. By isolating dependencies, they prevent unrelated projects from affecting each other and make your applications easier to maintain. Start with Python’s built-in venv module, create a.venv directory for each project, install packages through python -m pip, and record your dependencies for future installations.

As your projects become more complex, explore uv for streamlined dependency and Python version management. The essential takeaway is straightforward: keep each Python project’s dependencies isolated, documented, and easy to recreate. That single habit will prevent many common development problems.