Skip to content

Create and Use a Virtual Environment

When starting a new Python project, using a virtual environment is one of the most important habits to build. A virtual environment gives each project its own isolated set of packages so dependencies do not clash across projects or with the system Python.

With a virtual environment in place, you can reliably reproduce the same package set later on another machine or in CI by reinstalling from your dependency files, which is essential for consistent behavior in development and deployment.


What is a virtual environment?

A Python virtual environment is a self-contained directory that holds its own Python interpreter and installed packages, separate from the global Python installation.

When you activate the environment, commands like python and pip resolve to this isolated environment, so installing or upgrading packages only affects the current project instead of your whole system.


Why virtual environments are useful

Virtual environments solve several practical problems:

  • Different projects can depend on different versions of the same library without conflicts.
  • Reinstalling from requirements.txt or a lockfile into a fresh environment reproduces the project’s state for CI, production, or a new machine.
  • You can experiment with upgrades in a new environment without risking your global Python or other projects.

Because of this, most modern Python workflows assume “one virtual environment per project” as a baseline practice.


Tools for managing virtual environments

Several tools can create and manage virtual environments, often with different trade-offs and target audiences.

The table below compares commonly used options you are likely to encounter:

Tool Type Key advantages Key disadvantages
venv Built-in stdlib module Included with Python 3, simple to use, no extra install, good default for most projects. Minimal features; does not handle dependency resolution or lockfiles by itself.
virtualenv External environment tool Works with multiple Python versions, can be faster and more flexible than venv in some setups. Extra dependency to install and maintain; overlaps heavily with what venv already provides.
conda Environment + package manager Excellent for data science; manages non-Python binaries, cross-platform, rich ecosystem. Heavier, own solver and channels; mixing with plain pip needs care.
Poetry Project/dependency manager Modern workflow with pyproject.toml, built-in virtualenv and lockfile support, good packaging story. More opinionated; extra learning curve and tooling overhead for very small projects.
Pipenv Dependency + env manager Combines pip and virtualenv, uses Pipfile and lockfile, integrates security checks. Development has slowed at times; behavior can feel opaque or slow on large dependency graphs.
uv Fast all-in-one manager Extremely fast, manages environments, dependencies, and lockfiles in one tool, designed as a modern replacement for pip + venv. Ecosystem is newer; some teams and docs still assume classic pip + venv workflows.

For this tutorial series, venv is the baseline because it is available everywhere, but knowing the alternatives helps you choose the right tool for your context.


Creating a virtual environment with venv

venv ships with Python 3, so you can use it without installing anything extra.

  1. From the project root, create an environment directory (commonly called .venv):

    python3 -m venv .venv
    
  2. Activate it:

    source .venv/bin/activate
    
  1. From the project root, create an environment directory (commonly called .venv):

    python -m venv .venv
    
  2. Activate it:

    .\.venv\Scripts\activate
    

Once activated, python and pip now point to the environment, and you can install project dependencies with:

pip install -r requirements.txt

This setup keeps the environment local to the project and easy to recreate.


Creating a virtual environment with virtualenv

virtualenv is an older but still widely used tool that predates venv and offers some extra flexibility.

  1. Install virtualenv globally or in a base environment:
pip install virtualenv
  1. From your project directory, create an environment:
virtualenv .venv
  1. Activate it using the same commands as for venv (source .venv/bin/activate on Unix-like systems, .\.venv\Scripts\activate on Windows).

After activation, you use pip inside the environment just like with venv.


Creating a virtual environment with conda

Conda manages both environments and packages, including non-Python dependencies, which is especially useful in data and scientific stacks.

  1. Ensure you have Anaconda or Miniconda installed.
  2. Create a new environment with a chosen Python version:
conda create -n myproject-env python=3.12
  1. Activate the environment:
conda activate myproject-env
  1. Install packages:
conda install numpy
pip install some-pypi-only-package

When mixing conda and pip, install as much as possible via conda first, then fall back to pip only for packages not available in the channels you use.


Creating a virtual environment with Poetry

Poetry manages dependencies and packaging using pyproject.toml, and it can automatically create an isolated environment for each project.

  1. Install Poetry using the official installer or your OS package manager.
  2. In a new or existing project directory, initialize metadata:
poetry init
  1. Add dependencies:
poetry add requests
  1. Let Poetry handle the environment and enter it with:
poetry shell

Poetry will create and reuse a virtual environment for that project, and the poetry.lock file records exact versions for reproducible installs.


Creating a virtual environment with Pipenv

Pipenv combines pip and virtualenv, using a Pipfile and Pipfile.lock to track dependencies and versions.

  1. Install Pipenv:
pip install pipenv
  1. In your project directory, create an environment and install a dependency:
pipenv install requests
  1. Enter the environment shell:
pipenv shell

Pipenv automatically creates and manages the environment and lockfile, so running pipenv install on another machine recreates the same dependency graph.


Creating a virtual environment with uv

uv is a newer, very fast environment and dependency manager that aims to replace pip, venv, and related tools in one CLI.

  1. Install uv following the official instructions for your OS.
  2. From your project directory, create and sync an environment:
uv init
uv add requests
  1. Use uv run to execute commands inside the managed environment:
uv run python main.py

uv manages the virtual environment automatically and records dependencies in configuration and lockfiles for fast, reproducible installs.


Which tool should you start with?

For most small to medium projects, starting with plain venv plus pip is a solid, low-friction choice that integrates smoothly with editors, CI, and common tooling.

If you work heavily with data science stacks or complex native dependencies, consider conda, and if you want an all-in-one project manager with built-in packaging workflows, try Poetry or uv once you are comfortable with the basics.