pip & Virtual Environments
Python's standard library is large, but sooner or later you will want a package written by someone else: requests for calling web APIs, pandas for data, Django or Flask for websites. You install these with pip, Python's package installer. You should install them inside a virtual environment, a private folder of packages that belongs to one project.
This lesson shows the everyday commands. None of them run in the browser editor, so open a terminal on your own computer (see Install Python & Set Up).
What pip does
pip downloads packages from the Python Package Index (PyPI), the public online store of free Python packages, and installs them so that import can find them. The safest way to call pip is through the Python you want to use, with python -m pip:
python -m pip install requests
python -m pip list
python -m pip show requests
python -m pip install --upgrade requests
python -m pip uninstall requestsinstall adds a package and anything it depends on, list shows what is installed, show prints details about one package, --upgrade moves to the newest version and uninstall removes it. On Windows with the Python install manager you can write py -m pip instead of python -m pip.
Why you need a virtual environment
Imagine Priya has two projects. An old college project needs version 4 of a package, and her new internship project needs version 5. If both projects share one global set of packages, installing one version breaks the other project.
A virtual environment (often called a "venv") solves this. It is a folder inside your project that holds its own copy of pip and its own installed packages. Each project gets its own environment, so their packages never clash. It also keeps your main Python installation clean, and makes it easy to tell exactly which packages a project needs.
Creating and activating a venv
Python includes the venv module for this. In your project folder, run:
python -m venv .venvThis creates a folder called .venv (the leading dot is a common convention that keeps it out of the way). Next you activate it, which tells your terminal to use the Python and pip from inside that folder. The command depends on your system:
| System and shell | Activate command |
|---|---|
| Windows PowerShell | .venv\Scripts\Activate.ps1 |
| Windows Command Prompt | .venv\Scripts\activate.bat |
| macOS or Linux (bash, zsh) | source .venv/bin/activate |
Once it is active, your prompt starts with (.venv). Now python and pip refer to the environment, and anything you install goes into .venv only:
(.venv) PS C:\projects\weather> python -m pip install requests
(.venv) PS C:\projects\weather> python main.py
(.venv) PS C:\projects\weather> deactivatedeactivate switches the terminal back to your normal Python. Code editors such as VS Code detect a .venv folder in your project and offer to use it automatically.
You can check from Python whether you are inside a virtual environment. The first line of the output shows your own project path:
import sys
print(sys.prefix)
print("In a venv:", sys.prefix != sys.base_prefix)C:\projects\weather\.venv In a venv: True
Saving dependencies in requirements.txt
Your project's dependencies are the packages it needs. To share the project, you record them so others can install the same versions. pip freeze prints every installed package with its exact version, and > saves that output to a file:
python -m pip freeze > requirements.txtAfter installing only requests, the file looks something like this (your version numbers will be different, because packages keep releasing updates):
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
requests==2.34.2
urllib3==2.8.0requests brought four other packages with it. Kabir, who clones the project on his laptop in Lucknow, creates and activates his own .venv, then installs everything in one step:
python -m pip install -r requirements.txtpyproject.toml: the modern project file
Larger projects describe themselves in a file called pyproject.toml. It is the standard place for the project name, version, required Python version and dependencies, and most modern tools read it. A small one looks like this:
[project]
name = "weather-report"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = [
"requests>=2.32",
]Here >=2.32 means "version 2.32 or newer". You do not have to write this file by hand: tools like uv create and update it for you.
uv: a fast alternative
uv is a popular, very fast tool that handles Python versions, virtual environments and packages in one program. Many developers now use it instead of calling venv and pip separately. You can install it with python -m pip install uv or with the installer described in the official uv documentation.
uv init weather-report
cd weather-report
uv add requests
uv run main.pyuv init creates a project with a pyproject.toml. uv add installs a package into the project's .venv (creating it if needed) and records it in pyproject.toml and a lock file, uv.lock, which pins exact versions. uv run runs your script inside that environment, so you do not even need to activate it. If you prefer the classic workflow, uv venv and uv pip install requests work like their slower cousins.
Common mistakes
- Installing without activating. The package goes into your global Python, and your project's
pythonsaysModuleNotFoundError. Check for(.venv)in the prompt first. - PowerShell refuses to activate. If you see "running scripts is disabled on this system", run
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedonce in PowerShell, then try again. - Using
pipandpythonfrom different installations. If you have several Pythons, a barepipmay install into the wrong one.python -m pipalways matches thepythonyou run. - Putting your own files inside
.venv. That folder belongs to the tools and may be deleted and rebuilt at any time. Keep your code next to it, not in it.
Key points
- pip installs packages from PyPI; call it as
python -m pipso it uses the right Python. - A virtual environment gives each project its own private set of packages.
- Create one with
python -m venv .venv, activate it with the command for your system, and leave it withdeactivate. - Save dependencies with
pip freeze > requirements.txtand restore them withpip install -r requirements.txt. pyproject.tomlis the modern file that describes a project and its dependencies.- uv is a fast all-in-one alternative:
uv init,uv add,uv run.
Exercise
On your own computer, create a folder called quotes-app. Inside it, create and activate a virtual environment, install the requests package, and save the dependencies to requirements.txt. Then deactivate the environment, delete the .venv folder, and rebuild it from requirements.txt. Write down the commands you used.
Show solution
On Windows PowerShell:
mkdir quotes-app
cd quotes-app
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install requests
python -m pip freeze > requirements.txt
deactivate
Remove-Item -Recurse -Force .venv
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txtOn macOS or Linux, use source .venv/bin/activate to activate and rm -rf .venv to delete the folder. Rebuilding from requirements.txt gives you exactly the same package versions, which is the whole point of the file.
Quick Quiz
1. What is the main purpose of a virtual environment?
A venv keeps one project's packages apart from every other project and from the global Python.
2. Which command saves the installed packages and their versions to a file?
pip freeze prints name==version lines in the format that pip install -r reads back.
3. How do you activate a venv called .venv on macOS or Linux?
On macOS and Linux the activate script lives in .venv/bin and is loaded with source.
4. Why is python -m pip install x better than plain pip install x?
With several Pythons installed, python -m pip guarantees pip matches the interpreter you use.
Learn Python with a mentor
Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
