7. Modules, Standard Library & Packages

Virtual Environments (venv) & Installing Packages (pip)

Keep Python projects organized with virtual environments (venv), install free community libraries using pip, and share dependencies cleanly via requirements.txt.

3 min readUpdated 2026-11-29
In This Lesson
  • Isolate project dependencies using python3 -m venv .venv
  • Install open-source libraries with pip and keep .venv out of Git
  • Export and share exact project dependencies via requirements.txt

Python's built-in tools can do a lot, but the real reason developers love Python is that you can install free community packages for almost anything—sending web requests (requests), testing code (pytest), working with spreadsheets (pandas), or building APIs (fastapi).

Before installing packages, there is one simple habit that prevents 90% of setup headaches: virtual environments (venv).

Why Use a Virtual Environment?

Suppose Project A works with version 1.0 of a library, and six months later Project B needs version 2.0 of that same library. If you install everything into one global folder on your computer, upgrading for Project B could break Project A!

A virtual environment creates a private .venv/ folder right inside your project folder—like giving each project its own clean toolbox.

Create & Turn On .venv (3 Commands)

Open your terminal inside your project folder and run:

bash
# 1. Create a private .venv folder (only needed once per project)
python3 -m venv .venv

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

# 2. Turn it ON (Windows PowerShell)
.venv\Scripts\Activate.ps1

How do you know it worked? Your terminal prompt will change to show (.venv) at the start! (Type deactivate anytime to turn it off.)

Keep .venv Out of Git

Never push .venv/ to GitHub—it contains thousands of files. Add .venv/ to your .gitignore file and share requirements.txt instead!

Installing Packages with pip

Once (.venv) is active, pip installs packages right into that project's .venv folder (using python -m pip guarantees you always invoke the active environment's pip):

bash
python -m pip install requests

Pinning Package Versions (== vs >=) & pyproject.toml

In requirements.txt, pinning exact versions (requests==2.32.3) guarantees teammates get the exact same tested release, whereas requests>=2.31,<3.0 allows safe minor security patches without breaking major upgrades. Modern Python projects also list dependencies inside pyproject.toml alongside project metadata.

Now your Python script can import and use requests to fetch live data from a website or API:

python
import requests

response = requests.get("https://api.github.com/repos/python/cpython", timeout=10)
data = response.json()
print("Repo:", data["full_name"], "| Stars:", data["stargazers_count"])

Sharing Your Package List (requirements.txt)

To save a list of everything installed in .venv:

bash
pip freeze > requirements.txt

If someone else downloads your project on their computer, they simply create their own .venv and run:

bash
pip install -r requirements.txt

Try It Yourself: Semantic Version Pinning Check

Practice checking whether an installed package version satisfies a requirements.txt constraint right in the browser:

Python 3Try It Live: Check Package Version Constraints
Output

Click “Run Code” to execute this snippet right in your browser.

Try venv & pip on Your Machine

0/4 done

Quick Checkpoints

Check Your Understanding

Why do Python developers create a .venv inside each project?