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.

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; usemath.sqrt(16) - โข
from math import sqrtโ import just one function; usesqrt(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 mathloads the whole module as an object calledmath - โข3. Access its contents with dot notation:
math.pi,math.sqrt() - โข4.
from random import choiceimports just that one function directly - โข5. Your own
.pyfiles are also modules โimport my_utilsworks if it's in the same folder - โข6.
pip install requestsin the terminal installs third-party packages from PyPI
Real-World Examples
- โขA quiz app uses
from random import shuffleto randomise question order - โขA countdown timer uses
import timeandtime.sleep(1)to wait one second - โขA weather app uses
import requeststo fetch live weather data from an API - โขA data dashboard uses
import jsonto parse JSON responses from a server - โขA game leaderboard uses
from datetime import datetimeto 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
.pyfile 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 nameorfrom 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
.pyfile and importing it by name
Real-World Examples
Remember
Prefer from module import function for clean, readable code. Use import module when you need multiple things from it. Avoid import *.
Quick Quiz
import math gives?