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.txtor 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.
Once activated, python and pip now point to the environment, and you can install project dependencies with:
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.
- Install
virtualenvglobally or in a base environment:
- From your project directory, create an environment:
- Activate it using the same commands as for
venv(source .venv/bin/activateon Unix-like systems,.\.venv\Scripts\activateon 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.
- Ensure you have Anaconda or Miniconda installed.
- Create a new environment with a chosen Python version:
- Activate the environment:
- Install packages:
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.
- Install Poetry using the official installer or your OS package manager.
- In a new or existing project directory, initialize metadata:
- Add dependencies:
- Let Poetry handle the environment and enter it with:
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.
- Install Pipenv:
- In your project directory, create an environment and install a dependency:
- Enter the environment 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.
- Install
uvfollowing the official instructions for your OS. - From your project directory, create and sync an environment:
- Use
uv runto execute commands inside the managed environment:
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.