CODE: Python Libraries and Virtual Environment

Libraries in Python extend the language with powerful prebuilt capabilities for almost any domain. Virtual environments isolate those libraries per project, ensuring stable dependencies and clean system configuration.

Introduction to Python Standard Library

The Python Standard Library (PSL) is a collection of modules and packages that come bundled with Python. It provides ready-to-use tools for everyday programming tasks, so you don’t need to “reinvent the wheel.” The PSL is one of Python’s greatest strengths—it makes the language powerful and versatile right out of the box.


What is the Standard Library?

  • A set of pre-installed modules that extend Python’s capabilities.
  • Covers file operations, math, text processing, networking, internet protocols, data serialization, system utilities, and much more.
  • Allows developers to write less code while solving complex problems efficiently.

Think of it as Python’s toolbox—you just need to import the right tool for the job.


Key Features

  • Cross-platform: Works consistently across Windows, macOS, and Linux.
  • Batteries included: Comes built-in—no need to install extra packages for most tasks.
  • Well-documented: Every module has official docs with examples.

Commonly Used Standard Library Modules

Here are some of the most frequently used modules in the PSL:

`math` – Mathematical Functions

Provides access to mathematical functions like square roots, trigonometry, and constants.

python
import math

print(math.sqrt(16))     # 4.0
print(math.pi)           # 3.141592653589793
print(math.factorial(5)) # 120


 `random` – Random Numbers

Useful for simulations, games, and generating test data.

python
import random

print(random.randint(1, 10))   # Random integer between 1 and 10
print(random.choice(["red", "blue", "green"]))  # Random choice


 `os` – Operating System Interface

Lets you interact with the operating system: files, directories, and environment variables.

python
import os

print(os.name)                     # 'posix' (Linux/Mac) or 'nt' (Windows)
print(os.getcwd())                 # Current working directory
os.mkdir("new_folder")             # Create new folder


 `datetime` – Dates and Times

Manages dates, times, and time intervals.

python
import datetime

now = datetime.datetime.now()
print(now)                  # Current date and time
print(now.strftime("%Y-%m-%d"))  # Format as 2025-09-29


 `json` – Working with JSON Data

Enables reading and writing JSON files (common in APIs).

python
import json

data = {"name": "Alice", "age": 25}
json_str = json.dumps(data)   # Convert dict to JSON string
print(json_str)

parsed = json.loads(json_str) # Convert back to dict
print(parsed["name"])


  `urllib` – Working with URLs

Fetches data from the internet.

python
from urllib import request

response = request.urlopen("https://example.com")
print(response.read()[:50])  # First 50 bytes of page


 `re` – Regular Expressions

Helps with pattern matching in strings.

python
import re

text = "My phone number is 123-456-7890"
match = re.search(r"\d{3}-\d{3}-\d{4}", text)
print(match.group())  # 123-456-7890


Why Learn the Standard Library?

  • Saves time and effort (don’t reinvent wheels).
  • Makes your code shorter, cleaner, and more readable.
  • Improves performance with optimized C-implemented modules.
  • Widely used in interviews, real-world projects, and production systems.

Installing & Managing Packages with `pip`

pip is Python’s package manager. You use it to installupgraderemove, and inspect third-party packages from the Python Package Index (PyPI) or private registries. Always prefer running it as a module (e.g., python -m pip) to be sure you’re using the pip that matches your Python interpreter.

Use these launchers:

  • macOS/Linux: python3 -m pip ...
  • Windows: py -m pip ...

Check your setup

bash
python3 -m pip --version          # pip version + path
python3 -m pip list               # installed packages
python3 -m pip show requests      # details about a package
python3 -m pip check              # report dependency conflicts

Upgrade pip (per interpreter):

python3 -m pip install --upgrade pip


Install packages

Basic install

python3 -m pip install requests

Specific versions & ranges (PEP 440)

bash
python3 -m pip install "Django==5.1.1"     # exact
python3 -m pip install "Django>=5.1,<5.2"   # compatible range
python3 -m pip install "pydantic~=2.8"      # compatible release (>=2.8,<3.0)

Extras (optional features)

python3 -m pip install "uvicorn[standard]"

From local files / URLs / VCS

bash
python3 -m pip install ./pkgdir/                # local path (has pyproject.toml or setup.py)
python3 -m pip install ./dist/pkg-1.0.0.whl     # wheel file
python3 -m pip install "git+https://github.com/pallets/flask.git@main#egg=flask"

Editable installs (for local development)

python3 -m pip install -e ./your_package


Upgrade / remove

bash
python3 -m pip install --upgrade requests
python3 -m pip uninstall requests

To see available versions:

python3 -m pip index versions requests


Reproducible environments with `requirements.txt`

Freeze currently installed packages

python3 -m pip freeze > requirements.txt

Install exactly those versions

python3 -m pip install -r requirements.txt

Constraints files (pin transitive deps without forcing top-level)

  • requirements.txt (what you want)
  • constraints.txt (version ceilings/floors for subdeps)

bash
# requirements.txt
fastapi

# constraints.txt
pydantic==2.8.2

python3 -m pip install -r requirements.txt -c constraints.txt

Verifying integrity with hashes (supply one per file)

bash
# requirements.txt
requests==2.32.3 \
  --hash=sha256:... \
  --hash=sha256:...

python3 -m pip install --require-hashes -r requirements.txt


Using mirrors, private indexes, and proxies

Alternate index (private registry)

bash
python3 -m pip install --index-url https://pypi.org/simple pkg
python3 -m pip install --extra-index-url https://your.company/simple your-internal-pkg

Behind a proxy

bash
python3 -m pip install --proxy http://user:pass@proxy:8080 requests

Trusted hosts (if SSL inspection causes issues)

bash
python3 -m pip install --trusted-host your.company --index-url http://your.company/simple your-internal-pkg


Speed & offline installs

Prefer wheels (faster, no build step)

python3 -m pip install --only-binary=:all: <pkg> # fail if wheel not available

Build a local wheelhouse (for CI/offline)

bash
mkdir wheels
python3 -m pip download --only-binary=:all: -d wheels -r requirements.txt
# later / offline
python3 -m pip install --no-index -f wheels -r requirements.txt

Caching

pip uses a cache by default. To inspect/clear:

bash
python3 -m pip cache dir
python3 -m pip cache info
python3 -m pip cache purge


Building from source (PEP 517/518)

Some packages ship only as source (sdist) and need build tools:

  • A C/C++ toolchain (e.g., build-essential, Xcode CLT, or MSVC)
  • Python build backend defines in pyproject.toml (e.g., setuptoolshatchlingmaturin)

Install build helpers when needed:

python3 -m pip install build setuptools wheel


Environment markers (per-platform deps)

In requirements.txt:

uvloop==0.20.0 ; platform_system == "Linux"


Common issues & fixes

  • Permission errors / need sudo → Use virtual environments or --user (see below).
  • SSL or corporate MITM → Use --trusted-host, proper certs, or your company index.
  • Version conflicts → python3 -m pip check, pin versions, or add a constraints file.
  • Multiple Python versions → Always use python -m pip for the _right_ interpreter.
  • “Legacy” pip search → It’s removed; use pip index versions <pkg> or browse PyPI.

Per-user installs (outside venv)

python
python3 -m pip install --user httpie
# User-site bin path (ensure on PATH)
python3 -m site --user-base


(Preview) Virtual environments (best practice)

You’ll normally combine pip with virtual environments to isolate dependencies per project:

python
python3 -m venv .venv
source ./.venv/bin/activate     # Windows: .\.venv\Scripts\activate
python -m pip install -r requirements.txt


Quick “cheat sheet”

bash
# Inspect
python -m pip --version
python -m pip list
python -m pip show PACKAGE
python -m pip check

# Install / Update / Remove
python -m pip install PACKAGE
python -m pip install "PACKAGE>=X,<Y"
python -m pip install --upgrade PACKAGE
python -m pip uninstall PACKAGE

# Reproducibility
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
python -m pip install -r requirements.txt -c constraints.txt
python -m pip install --require-hashes -r requirements.txt

# Indexes & proxies
python -m pip install --index-url URL --extra-index-url URL PACKAGE
python -m pip install --proxy http://user:pass@host:port PACKAGE

# Wheels & offline
python -m pip download --only-binary=:all: -d wheels -r requirements.txt
python -m pip install --no-index -f wheels -r requirements.txt


Virtual Environments in Python (venv)

When working on multiple Python projects, each may require different package versions. Installing everything globally leads to conflicts and dependency chaos. That’s why we use virtual environments—isolated Python “sandboxes” where each project has its own interpreter and dependencies.


What is a Virtual Environment?

  • self-contained directory that holds:
  • Its own Python interpreter
  • Its own site-packages (where dependencies live)
  • Keeps dependencies isolated per project.
  • Prevents version conflicts between projects.
  • Easy to create, activate, and delete.

Think of it as creating a mini Python installation for every project.


Why Use Virtual Environments?

  • Avoids _“works on my machine”_ issues.
  • Keeps projects independent.
  • Allows reproducible builds.
  • Encouraged by Python best practices (PEP 405).

Using `venv` (Standard Library)

Since Python 3.3, venv is built in—no extra install needed.

Create a venv

python3 -m venv .venv

  • .venv is the folder name (common convention).

Activate

  • Linux/macOS:
  • source .venv/bin/activate

  • Windows (cmd):
  • .venv\Scripts\activate

  • Windows (PowerShell):
  • .\.venv\Scripts\Activate.ps1

You’ll see (.venv) prefix in your shell → means environment is active.

Deactivate

deactivate

Install packages inside

pip install requests

Installs into .venv/lib/..., not globally.

Quick Workflow Example

bash
# 1) Create & activate
python3 -m venv .venv
source .venv/bin/activate

# 2) Install dependencies
pip install fastapi uvicorn

# 3) Save versions
pip freeze > requirements.txt

# 4) Reproduce later
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt