Modules & Packages

Python Fundamentals — Modules & Packages
Python Fundamentals
Course 1 · Chapter 9 · Modules & Packages

📦 Modules & Packages

The final chapter of Python Fundamentals: how to use code beyond a single script — importing modules from the standard library, installing third-party packages with pip, and keeping each project's dependencies isolated with a virtual environment.

📥 Importing Modules

import math print(math.sqrt(16)) # 4.0 — access via math.name import math as m print(m.pi) # 3.14159... — a shorter alias from math import sqrt, pi print(sqrt(16)) # 4.0 — imported names used directly, no math. prefix from math import * # imports everything — see the warning below
⚠ Avoid from module import *

The star-import form dumps every public name from a module directly into your current namespace. It's convenient in a quick throwaway script, but in real code it makes it unclear where a given name actually came from, and it can silently overwrite (shadow) names you've already defined — including built-ins. Stick to import module or from module import specific_name instead.

📚 The Standard Library

Python ships with a large "batteries included" standard library — modules available immediately with no installation step:

import random print(random.randint(1, 10)) # a random integer from 1 to 10, inclusive import datetime print(datetime.date.today()) # today's date import os print(os.getcwd()) # the current working directory

📦 pip and Third-Party Packages

pip is Python's package installer, used for code outside the standard library:

# at the command line, not inside a .py file: pip install requests # then, in your script: import requests

A project's dependencies are typically tracked in a requirements.txt file — one package (and optionally a version) per line — so anyone else can recreate the exact same set with pip install -r requirements.txt.

🧪 Virtual Environments (venv)

Installing packages globally on your machine causes real problems the moment two projects need different, conflicting versions of the same package. A virtual environment gives each project its own isolated set of installed packages:

# create a virtual environment (once, per project) python -m venv venv # activate it — macOS/Linux source venv/bin/activate # activate it — Windows venv\Scripts\activate # now pip install only affects THIS project's environment pip install requests # leave the virtual environment deactivate
💡 Always Use a Virtual Environment

Creating a fresh venv for every new Python project is standard practice, not an optional extra — it's the direct equivalent of Node's per-project node_modules folder, keeping each project's dependencies self-contained and reproducible on another machine.

Dependency Management: Go vs Kotlin vs Python

LanguageApproach
Gogo.mod/go.sum declare and lock dependency versions per-project automatically — isolation is built into the tooling, no separate "activate an environment" step.
KotlinGradle (or Maven) resolves dependencies declared in build.gradle.kts, scoped per-project by default — also no manual activation step.
Pythonpip installs packages, but isolation isn't automatic — a venv has to be deliberately created and activated per project, or packages land in one shared global location.

This is a genuine rough edge coming from Go or Kotlin — both of those ecosystems bake project-level isolation into the build tool itself, while Python's isolation is a separate, opt-in step you have to remember to take.

import

import x, import x as y, from x import y — avoid from x import *.

Standard library

math, random, datetime, os, and many more — no install needed.

pip

Installs third-party packages; dependencies tracked in requirements.txt.

venv

An isolated, per-project set of installed packages — create and activate before installing anything.

💻 Coding Challenges

Challenge 1: Random Dice Roller

Using the random module, write a function roll_dice() that returns a random integer between 1 and 6 (inclusive), then call it and print the result.

Goal: Practice importing and using a standard library module.

→ Solution

Challenge 2: Days Until a Date

Using the datetime module, calculate and print how many days remain until January 1st of next year, starting from today's date.

Goal: Practice working with the datetime module and date arithmetic.

→ Solution

Challenge 3: Write a requirements.txt

Write out (as plain text, no code needed to run) a requirements.txt file listing three made-up third-party packages your project depends on, each pinned to a specific version using the == syntax.

Goal: Get familiar with the requirements.txt format itself, since you'll be reading and writing these in almost every real Python project.

→ Solution

🎓 Course Complete!

That's all 9 chapters of Python Fundamentals — installation, variables, control flow, loops, strings, lists/tuples/sets, dictionaries, functions, and modules/packages. Next up: Python Intermediate (py2), starting with Object-Oriented Programming.