Python Basics

Python Modules and Imports: import, from and as

Use thousands of ready-made functions from Python's standard library, or split your own code into files.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • import module and module.function
  • from module import name
  • Aliases with as
Three ways to import
import
import math
  • math.sqrt(16)
  • clear where names come from
from ... import
from math import pi
  • pi
  • short, but watch for name clashes
as
import random as rnd
  • rnd.randint(1, 6)
  • a short alias

Borrow code from another file

The problem

Many tasks have already been solved well: square roots, random numbers, dates, counting items. Writing them yourself takes time and invites bugs. Python ships with a large standard library, and import lets you use it in one line.

Think of it like this

Importing a module is like borrowing a toolbox from a shared workshop. You bring the whole box (import math) and pick tools from it by name (math.sqrt), or take just one tool (from datetime import date). You can also put a shorter label on the box (as).

From a shared workshop to code
import math🧰 Borrow the whole toolbox
math.sqrt(16)🔧 Use one tool from the box
from datetime import date🪛 Take only the tool you need
Counter as Tally🏷️ Put your own label on it
Ready-made, tested tools instead of writing everything yourself.
In one sentence

A module is a file of Python code, and import makes its functions and classes available in your program.

Key points

  1. import math keeps the module name as a prefix, so you write math.sqrt(16) and math.pi.
  2. from datetime import date brings one name in directly, so you write date(...).
  3. from collections import Counter as Tally gives the imported name a different name.
  4. random.seed(1) makes the 'random' results repeat on every run, which helps testing.

When should you use it?

  • Maths, dates, random numbers, file paths and JSON: the standard library already has them.
  • Splitting your own project into several files and importing between them.
  • Using installed packages such as NumPy or pandas.

When not to use it

  • from module import *: it hides where names come from and can overwrite your own names.
  • Importing a large package for one tiny thing you can write in two lines.

Example code

Run code →
main.py
# import loads a module from Python's standard library.
import math
import random
from datetime import date
from collections import Counter as Tally

print(math.sqrt(16))
print(math.pi)
print(math.floor(3.7), math.ceil(3.2))

# A fixed seed makes the "random" numbers repeat on every run.
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))

print(date(2026, 1, 1).strftime("%A"))

# "as" gives an imported name a shorter or clearer alias.
print(Tally("banana").most_common(1))
Get a Run button for this example

Result of running the code

4.0
3.141592653589793
3 4
2
scissors
Thursday
[('a', 3)]

Code explanation

import math keeps the module name as a prefix, from datetime import date brings one name in directly, and as gives an import a shorter alias. random.seed makes the random results repeat.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Naming your file like a module

A file called random.py in your folder is imported instead of Python's random module.

Problem
# file name: random.py
import random
random.randint(1, 6)  # AttributeError
Fix
# rename your file to dice.py
import random
random.randint(1, 6)

Forgetting the module prefix

After import math, the function is math.sqrt, not sqrt.

Problem
import math
print(sqrt(16))
Fix
import math
print(math.sqrt(16))

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges