How to Create and Use a Python Virtual Environment

Create an isolated Python virtual environment with venv, activate it, install packages, and understand why every project should use one.

Published September 17, 2026

A virtual environment is an isolated folder with its own Python and packages, so each project keeps its own dependencies. Create one with python -m venv, activate it, then install packages with pip.

The concept

Without isolation, every project shares the global site-packages, and two projects needing different versions of the same library will conflict.

venv copies (or links) the interpreter into a folder, usually .venv. Activating it puts that folder first on your PATH, so python and pip now refer to the project's copies.

Create, activate, install

python3 -m venv .venv
source .venv/bin/activate
(.venv) $
pip install requests
Successfully installed requests-2.32.0
pip freeze > requirements.txt
deactivate

Practical examples

Windows activation

.venv\Scripts\Activate.ps1

Recreate an environment from requirements

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Ignore the folder in Git

echo '.venv/' >> .gitignore

Common mistakes

  • Installing packages while the venv is not activated, polluting the global Python.
  • Committing the .venv folder instead of requirements.txt.
  • Moving or renaming the folder, which breaks its hard-coded paths — recreate it instead.

Key takeaways

  • One venv per project.
  • python -m venv .venv → activate → pip install.
  • Share requirements.txt, not the venv.

FAQ

venv vs virtualenv vs conda?

venv ships with Python and is enough for most projects; conda also manages non-Python packages.

How do I know it is active?

The prompt shows (.venv) and `which python` points inside the folder.

More Python articles