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.