python
virtualenv
tutorial
beginners
programming

Comprehensive beginner's virtualenv tutorial?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A virtual environment gives one Python project its own interpreter context and installed packages, separate from other projects. That separation is the main reason Python dependency management stays sane once you work on more than one codebase.

Why Beginners Should Use Virtual Environments Early

Without an isolated environment, pip install usually affects a shared Python installation. That creates conflicts when one project needs one package version and another project needs a different one.

A virtual environment fixes that by creating a project-local environment with its own installed packages.

The practical outcome is simple:

  • project A can use one dependency version
  • project B can use another
  • your global Python stays cleaner

venv Versus virtualenv

For modern Python 3 projects, the built-in venv module is usually enough.

bash
python -m venv .venv

virtualenv is an external tool that offers extra features and broader compatibility, and many teams still use it.

bash
pip install virtualenv
virtualenv .venv

For a beginner, the simplest advice is:

  • use python -m venv .venv unless your team or tooling specifically requires virtualenv

Creating An Environment

A common workflow is:

bash
python -m venv .venv

This creates a directory named .venv containing the environment.

To activate it on macOS or Linux:

bash
source .venv/bin/activate

On Windows Command Prompt:

cmd
.venv\Scripts\activate.bat

On Windows PowerShell:

powershell
.\.venv\Scripts\Activate.ps1

After activation, python and pip usually point at the environment instead of the global interpreter.

Installing Packages Inside The Environment

Once the environment is active, install packages normally.

bash
pip install requests

Then verify them:

bash
pip list

The important point is that those packages now belong to the active environment, not necessarily to the system Python.

Saving Dependencies

When your environment contains the packages a project needs, save them to a requirements file.

bash
pip freeze > requirements.txt

Another developer or deployment environment can recreate the package set with:

bash
pip install -r requirements.txt

This is one of the main benefits of using environments properly: repeatable project setup.

Deactivating And Removing The Environment

When you are done working in the environment, deactivate it:

bash
deactivate

If you want to remove the environment entirely, delete the .venv directory.

bash
rm -rf .venv

On Windows, remove it through Explorer or the appropriate shell command.

The environment is disposable. That is a feature, not a bug.

A Typical Beginner Workflow

A very practical pattern for each project is:

bash
1mkdir my_project
2cd my_project
3python -m venv .venv
4source .venv/bin/activate
5pip install --upgrade pip
6pip install requests
7pip freeze > requirements.txt

That is enough to start building with isolated dependencies.

What To Commit To Git

Usually, commit:

  • your project code
  • 'requirements.txt or another dependency file'

Usually, do not commit:

  • the .venv directory

Add the environment directory to .gitignore.

gitignore
.venv/

This keeps the repository smaller and avoids platform-specific environment clutter.

Common Pitfalls

The most common mistake is forgetting to activate the environment before installing packages. Then packages end up in the wrong Python installation.

Another mistake is creating multiple environments and losing track of which one is active.

Beginners also sometimes commit the .venv directory to Git, which is usually unnecessary and noisy.

Finally, do not assume virtualenv and venv are the same command. They solve a similar problem, but they are different tools.

Summary

  • A virtual environment isolates one project's Python packages from others.
  • For modern Python 3 work, python -m venv .venv is usually the simplest choice.
  • Activate the environment before installing packages.
  • Save dependencies with pip freeze > requirements.txt.
  • Do not commit the .venv directory to version control.

Course illustration
Course illustration

All Rights Reserved.