PythonMastery
reference 5 min read · lesson 25 of 25 in Reference

pip and Virtual Environments Cheat Sheet

1 · The lesson

read

pip is the tool that installs packages other people wrote, such as requests or pandas. A virtual environment (venv) is a separate folder with its own Python packages. Each project gets the package versions it needs, and one project cannot break another. This page lists the commands you type in a terminal on your own computer. For the full tutorials, see Virtual environments and pip and the standard library.

On this site you don't use pip. Type a package name in the editor's package box and press Install. The site then installs the package in your browser with micropip, a version of pip made for the browser.

1. Create a virtual environment

Run this inside your project folder. It makes a folder named .venv with its own Python and its own pip. Make one per project.

bash
python -m venv .venv        # Windows, macOS and Linux
py -m venv .venv            # Windows, if "python" is not found
python3 -m venv .venv       # macOS and Linux, if "python" is not found

2. Turn it on and off

"Activate" means: for this terminal, python and pip now mean the ones inside .venv. The prompt starts with (.venv) while the venv is on. You need to activate again in every new terminal.

SystemCommand
Windows PowerShell.venv\Scripts\Activate.ps1
Windows cmd.venv\Scripts\activate.bat
macOS and Linuxsource .venv/bin/activate
Any (turn it off)deactivate

You can also skip activation and run the venv's Python by its path: .venv\Scripts\python.exe app.py on Windows, .venv/bin/python app.py on macOS and Linux.

3. Install and remove packages

bash
python -m pip install requests                # install the newest version
python -m pip install "requests==2.32.3"      # install one exact version
python -m pip install "pandas>=2.0"           # install version 2.0 or newer
python -m pip install --upgrade requests      # update requests to the newest version
python -m pip uninstall requests              # remove requests (add -y to skip the question)
python -m pip install --upgrade pip           # upgrade pip itself

Put quotes around a version rule. Without them, the shell reads > as "write the output to a file".

RuleMeaning
==2.32.3exactly this version
>=2.31,<32.31 or newer, but below 3
~=2.312.31 or any newer 2.x version, never 3.0
!=2.30.0any version except 2.30.0

4. Why python -m pip and not pip

A computer often has more than one Python. A bare pip command can belong to a different Python than the one python starts. python -m pip runs the pip that belongs to that exact python, so the package goes where your code will look for it.

bash
python -m pip --version     # also shows which Python folder pip installs into

5. See what is installed and share it

bash
python -m pip list                           # list every package and its version
python -m pip show requests                  # show its version, folder and the packages it needs
python -m pip freeze > requirements.txt      # save the exact versions to a file
python -m pip install -r requirements.txt    # install every package listed in that file

requirements.txt is a plain text file with one package per line, such as requests==2.32.3. Commit it to git. Anyone can then rebuild the same environment:

bash
python -m venv .venv
source .venv/bin/activate          # or the Windows line from section 2
python -m pip install -r requirements.txt

6. Which Python is running? (from Python)

These lines work in any Python, including this page. sys.prefix is the folder of the Python that is running. sys.base_prefix is the folder of the Python the venv was made from. They are different only inside a virtual environment.

python
import sys

print(sys.prefix != sys.base_prefix)   # True inside a virtual environment
print(sys.executable)                  # the full path of the Python running this code

importlib.metadata.version tells you which version of a package is installed, as a string. If the package is not installed, it raises PackageNotFoundError.

python
from importlib.metadata import version, PackageNotFoundError

for name in ["pip", "no-such-package"]:
    try:
        print(name, version(name))
    except PackageNotFoundError:
        print(name, "is not installed here")

From a terminal, where python (Windows cmd) or which python (macOS and Linux) shows which python runs.

7. Delete it and start again

A venv holds nothing you wrote, so you can always delete it and rebuild it from requirements.txt. Run deactivate first. A venv also cannot be moved or renamed: delete it and create it again instead.

bash
Remove-Item -Recurse -Force .venv     # Windows PowerShell
rmdir /s /q .venv                     # Windows cmd
rm -rf .venv                          # macOS and Linux

Common mistakes

Installing before you activate. pip install requests in a terminal where the venv is off puts the package in your main Python. Your project then says ModuleNotFoundError. Fix: activate first, or use .venv's Python by its path, then install again.

Committing .venv to git. The folder is large and only works on your computer. Add this line to .gitignore and commit requirements.txt instead:

text
.venv/

"error: externally-managed-environment". Many Linux systems and Homebrew on macOS protect the system Python so pip can't break the tools that need it. Create a venv and install there. See externally-managed-environment.

sudo pip install. This changes files that your system needs. Use a venv instead.

pip and python from different installs. pip install x works, then python app.py says ModuleNotFoundError. The two commands belong to different Pythons. Always type python -m pip install x.

"running scripts is disabled on this system". PowerShell blocks Activate.ps1 by default on some computers. Run this once, then activate again:

bash
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

Found this useful? Share it with a friend who’s learning Python.