What Is a Python Virtual Environment, and How Do You Use pip?
Sooner or later every Python tutorial says "first, install this package," and the beginner's natural move is to install it straight into the main Python on the computer. That works until two projects need different versions of the same package. The official fix is a virtual environment, and setting one up takes three commands. The commands below were run on macOS with Python 3.9 and pip. Output can differ slightly with other versions.
1. 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.
2. 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.
3. 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
4. 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, guarantees you are running the pip that belongs to the Python you are using, which removes a whole class of "the package is installed but Python cannot find it" problems.
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.
5. Step 4: Record what you installed
pip freeze prints every installed package with its exact version, in the format pip can read back. The tutorial describes the common convention of saving it to a file named 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.
Anyone with this file, including you on a new computer, can rebuild the same set of packages:
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 .venv folder itself should not be shared; it is easy to recreate from the requirements file, and it is tied to the machine it was built on.
6. 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.
7. Key takeaways
- A virtual environment is a self-contained folder with its own Python and packages, so projects cannot break each other.
- Create one with
python3 -m venv .venvand activate it before installing anything. - Use
python -m pip install package==versionto install a specific version from PyPI. python -m pip freeze > requirements.txtrecords exact versions;install -r requirements.txtrestores them.- Share the requirements file, not the
.venvfolder.