๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

Modules and import

๐Ÿ“š What are Modules? A module is a file of Python code you can reuse. Python comes with a huge standard library of modules covering maths, dates, randomness, file handling, networking, and much more. Rather than writing everything from scratch you simply import what you need.

8 min 10 XP Lesson 14 of 21
Modules and import
๐ŸŒ

Appy Saysโ€ฆ

You don't have to build everything from scratch. Python has thousands of modules โ€” pre-written code you can drop straight into your program. Need to generate a random number? Pick a date? Do maths? There's a module for that. Import it and go.

๐Ÿ“–

What are Modules and Imports?

A module is a Python file containing functions, classes, and variables you can reuse. Importing a module makes its contents available in your script. Python ships with a huge standard library of modules built in.

  • โ€ขimport math โ€” import the whole module; use math.sqrt(16)
  • โ€ขfrom math import sqrt โ€” import just one function; use sqrt(16)
  • โ€ขfrom math import * โ€” import everything (avoid โ€” pollutes your namespace)
  • โ€ขimport random as rnd โ€” give it an alias for shorter names
  • โ€ขUseful standard library modules: math, random, datetime, os, json, time
  • โ€ขThird-party modules (installed with pip): requests, numpy, flask
๐ŸŽฎ

Think of it like Roblox Studio plugins

In Roblox Studio, you can install plugins to add new tools โ€” a terrain generator, a rig builder. Modules are Python's plugins. Instead of writing a square-root function yourself, you import math and use the one that's already been tested by millions of developers.

โš™๏ธ

How It Works

  • โ€ข1. Python searches for the module name in: your folder โ†’ standard library โ†’ installed packages
  • โ€ข2. import math loads the whole module as an object called math
  • โ€ข3. Access its contents with dot notation: math.pi, math.sqrt()
  • โ€ข4. from random import choice imports just that one function directly
  • โ€ข5. Your own .py files are also modules โ€” import my_utils works if it's in the same folder
  • โ€ข6. pip install requests in the terminal installs third-party packages from PyPI
๐ŸŒ

Real-World Examples

  • โ€ขA quiz app uses from random import shuffle to randomise question order
  • โ€ขA countdown timer uses import time and time.sleep(1) to wait one second
  • โ€ขA weather app uses import requests to fetch live weather data from an API
  • โ€ขA data dashboard uses import json to parse JSON responses from a server
  • โ€ขA game leaderboard uses from datetime import datetime to timestamp each score
๐Ÿ’ก

Key Facts

  • โ€ขPython's standard library has over 200 modules โ€” covering everything from email to HTML parsing
  • โ€ขPyPI (Python Package Index) hosts over 500,000 third-party packages you can install with pip
  • โ€ขModules are only loaded once per program run โ€” Python caches them for efficiency
  • โ€ขYou can write your own module by saving any .py file and importing it by name
โš ๏ธ

Watch Out!

Never name your own file the same as a built-in module โ€” a file called random.py will shadow Python's random module and cause confusing errors. Use descriptive names like my_random_utils.py.

๐Ÿ“Œ

Remember

Prefer from module import function for clean, readable code. Use import module when you need multiple things from it. Avoid import *.

โœ…

What You Learned

  • โ€ขModules are reusable Python files; import them with import name or from name import x
  • โ€ขPython's standard library has 200+ modules; PyPI has 500,000+ packages installable via pip
  • โ€ขUnlocks: using random, maths, dates, file paths, JSON, and thousands of third-party tools

Key Facts

  • โ†’Python's standard library has over 200 modules โ€” covering everything from email to HTML parsing
  • โ†’PyPI (Python Package Index) hosts over 500,000 third-party packages you can install with pip
  • โ†’Modules are only loaded once per program run โ€” Python caches them for efficiency
  • โ†’You can write your own module by saving any .py file and importing it by name

Real-World Examples

โ€ข A quiz app uses <code>from random import shuffle</code> to randomise question order โ€ข A countdown timer uses <code>import time</code> and <code>time.sleep(1)</code> to wait one second โ€ข A weather app uses <code>import requests</code> to fetch live weather data from an API โ€ข A data dashboard uses <code>import json</code> to parse JSON responses from a server โ€ข A game leaderboard uses <code>from datetime import datetime</code> to timestamp each score

Remember

Prefer from module import function for clean, readable code. Use import module when you need multiple things from it. Avoid import *.

Quick Quiz

1 / 2

import math gives?