What Is a Python Virtual Environment, and How Do You Use pip?
Use a virtual environment to give each project its own Python and packages when projects need different package versions. Create it, activate it, then install packages with pip. The commands below were run on macOS with Python 3.9 and re-run on Python 3.13. Output can differ slightly with other versions.
On this page▾
The problem a virtual environment solves
The Python tutorial explains the issue with a simple example. Application A needs version 1.0 of a module, and application B needs version 2.0. With only one Python installation, installing either version leaves one application unable to run.
The tutorial's solution is a virtual environment: a self-contained folder that contains a Python installation for a particular version of Python, plus its own set of packages. Each project gets its own environment, so upgrading a package for project B cannot break project A.
Step 1: Create the environment
Open a terminal in your project folder and run the venv module:
python3 -m venv .venv
This creates a folder named .venv. Listing it shows what was made:
bin
include
lib
pyvenv.cfg
The tutorial notes that .venv is a common name for this folder. The leading dot keeps it hidden in most terminal listings, the name explains what it is, and it avoids clashing with .env files that some tools use for settings. The tutorial also points out that the environment uses whichever Python version ran the command, so python3.12 -m venv .venv would create a 3.12 environment.
On Windows, the command is usually typed as python -m venv .venv.
Writing the script you will run inside the environment is covered in Writing Your First Python Script with Functions.
Step 2: Activate it
Activating tells your terminal to use the environment's Python and pip instead of the system ones. The tutorial gives these commands.
On macOS or Linux:
source .venv/bin/activate
On Windows:
.venv\Scripts\activate
The tutorial notes that activation changes your shell prompt to show the environment's name, usually as (.venv) at the start of the line. You can confirm it worked by asking which Python is now in use:
which python
python -c "import sys; print(sys.prefix != sys.base_prefix)"
The first command printed a path ending in .venv/bin/python, and the second printed True, which means Python is running inside a virtual environment. A fresh environment starts nearly empty. python -m pip list showed only pip and setuptools on Python 3.9; newer Python versions may list only pip.
When you are done working, type:
deactivate
Step 3: Install packages with pip
The tutorial describes pip as the program for installing, upgrading and removing packages, and says that by default it installs from the Python Package Index (PyPI). PyPI describes itself as a repository of software for the Python programming language, where the Python community shares packages.
With the environment active:
python -m pip install requests==2.31.0
The ==2.31.0 part pins an exact version. Leave it off to get the latest version. Running python -m pip show requests afterward printed:
Name: requests
Version: 2.31.0
Notice that the commands use python -m pip rather than plain pip. That form, which the tutorial uses throughout, runs pip through the Python you invoked, so packages are installed for that same Python (the one in the active environment).
Other everyday commands from the tutorial:
| Command | What it does |
|---|---|
python -m pip install --upgrade requests |
Upgrades to the latest version |
python -m pip uninstall requests |
Removes the package |
python -m pip show requests |
Shows details about one package |
python -m pip list |
Lists every installed package |
The tutorial also notes that if you run an install command for a version that is already installed, pip notices and does nothing.
Step 4: Record what you installed
pip freeze prints installed packages in requirements format, but omits bootstrap tools by default: pip, setuptools, wheel and distribute on Python 3.11 and earlier; only pip on Python 3.12 and later. Use --all to include those tools. Save the dependency list to requirements.txt:
python -m pip freeze > requirements.txt
cat requirements.txt
Example output (your version numbers will differ):
certifi==2026.7.22
charset-normalizer==3.5.2
idna==3.20
requests==2.31.0
urllib3==2.6.3
Installing one package pulled in four others it depends on, and freeze records all of them.
This file lets you request those pinned package versions in a compatible new environment:
python -m pip install -r requirements.txt
If you uninstall requests and then run that command, pip reads the file and reinstalls requests 2.31.0.
The tutorial suggests committing requirements.txt to version control so it ships with the project. The environment itself can be recreated from the requirements file, so the file is what needs to travel with the project.
A routine for every new project
- Make a project folder and open a terminal in it.
python3 -m venv .venv- Activate it (
source .venv/bin/activateon macOS or Linux). - Install what you need with
python -m pip install. - Save the list with
python -m pip freeze > requirements.txt. deactivatewhen you finish.
Sources
About this guide
Howlane operator · Checked against the official sources listed above. It is general information, not professional advice.
AboutEditorial standardsCorrectionsReport an error
Edit history
- Updated
- Updated
- Updated
- Updated
- Published