🇬🇧 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?