Reading history

Python Virtual Environment (venv) and pip Guide

3 min read 0 views

Learn Python virtual environments: create and activate venv on Windows, macOS and Linux, install packages with pip, and use requirements.txt.

A virtual environment is a private folder of packages for one project. Without one, every project on your computer shares the same installed packages, so upgrading a library for one project can break another. With venv (built into Python) and pip, each project gets exactly the versions it needs, and a requirements.txt file lets anyone recreate the setup.

In this lesson you will learn

  • Why virtual environments matter
  • How to create and activate one on Windows, macOS and Linux
  • How to install, list and remove packages with pip
  • How to save and restore dependencies with requirements.txt
  • How to check from code whether a venv is active

Create and Activate a Virtual Environment

Open a terminal in your project folder and run:

# create a folder called .venv holding a private Python
python -m venv .venv

# activate it (Windows)
.venv\Scripts\activate

# activate it (macOS / Linux)
source .venv/bin/activate

Once it is active, your prompt shows (.venv), and both python and pip point to the private copies. Type deactivate to leave.

Install and Manage Packages

python -m pip install requests          # install the latest version
python -m pip install "requests==2.32.3" # install an exact version
python -m pip list                       # show installed packages
python -m pip show requests              # details about one package
python -m pip uninstall requests         # remove a package
python -m pip install --upgrade pip      # update pip itself

Writing python -m pip instead of plain pip guarantees the package goes into the Python you are actually running.

Save and Restore Dependencies

python -m pip freeze > requirements.txt     # save exact versions
python -m pip install -r requirements.txt  # recreate on another machine

Commit requirements.txt to version control, but add the .venv folder to .gitignore. The folder is large and specific to one machine; anyone can rebuild it from the requirements file.

Check From Code Whether a venv Is Active

import sys

in_venv = sys.prefix != sys.base_prefix
print("Inside a virtual environment:", in_venv)
print(f"Python {sys.version_info.major}.{sys.version_info.minor}")

Output inside the activated .venv

Inside a virtual environment: True
Python 3.13

Output with the system Python

Inside a virtual environment: False
Python 3.13

Inside a venv, sys.prefix points to the environment folder while sys.base_prefix still points to the original Python installation. Your version number may differ.

Related Tools

ToolWhat it adds
venvBuilt in; creates environments
pipBuilt in; installs packages from PyPI
uv, poetry, pipenvFaster installs, lock files and project management
condaEnvironments that can also include non-Python libraries, popular in data science

Common Mistakes

  1. Installing before activating, which puts packages into the system Python.
  2. Committing the .venv folder to Git.
  3. Activation blocked on Windows PowerShell. Run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, or use Command Prompt.
  4. Moving or renaming the venv folder. Environments contain fixed paths; delete and recreate instead.

Practice Exercises

Exercise 1. Create a venv called env, activate it, and install requests.

Show answer
python -m venv env
source env/bin/activate        # Windows: env\Scripts\activate
python -m pip install requests

Exercise 2. A teammate sends you a project with a requirements.txt. What do you run?

Show answer

Create and activate a new venv, then run python -m pip install -r requirements.txt.

Frequently Asked Questions

Do I need a virtual environment for every project?

It is the recommended habit. Even small scripts benefit once they use any third-party package.

Where should the venv folder go?

Inside the project folder, usually named .venv. Most editors, including VS Code, detect it automatically.

Summary

  • python -m venv .venv creates an isolated environment.
  • Activate it before installing anything; deactivate leaves it.
  • python -m pip installs, lists and removes packages.
  • requirements.txt records exact versions so others can rebuild the environment.

Next: Python Lambda Functions.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *