TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 20 of 22 · By Ethan Cole · Updated November 21, 2026

Python Modules: import, from and the Standard Library

A module is simply a .py file whose functions and variables you can reuse in other files. Python ships with a large standard library of modules, so many everyday tasks, such as maths, dates, random numbers, JSON or file paths, need no installation at all.

Three ways to import

  • import math loads the module and gives you one name, math. You then write math.sqrt(81). This keeps it obvious where each function comes from.
  • from collections import Counter copies just the names you list into your file, so you write Counter(...) directly.
  • import random as rnd gives a module a shorter alias. Some aliases are community conventions, like import numpy as np.

Avoid from module import *. It dumps every public name into your file and makes it unclear where a name came from.

Python 3 · editable

Expected output

9.0 3.141592653589793
2 3
[('i', 4), ('s', 4)]
Saturday
True

A quick tour of the example

  • math offers sqrt, floor, ceil, constants like pi, and much more. Note that sqrt returns a float.
  • Counter counts how often each item appears; most_common(2) lists the top two. In "mississippi", i and s tie at four, and ties keep the order in which they were first seen.
  • datetime.date represents calendar dates; strftime("%A") formats the weekday name.
  • random produces pseudo-random numbers. Calling seed() makes the sequence repeatable, which is useful in tests. For passwords or tokens use the secrets module instead.

Your own modules

Save functions in helpers.py, and another file in the same folder can import helpers. When Python imports a file, it runs the file's top-level code once. That is why scripts often end with if __name__ == "__main__": followed by the code that should run only when the file is executed directly, not when it is imported.

Third-party packages

Packages from the Python Package Index are installed with pip, for example python3 -m pip install requests, ideally inside a virtual environment created with python3 -m venv .venv. The import syntax is exactly the same as for standard-library modules.

Common mistakes

  • Naming your own file math.py or random.py, which shadows the standard module.
  • Importing inside a loop; imports belong at the top of the file.
  • Forgetting the module prefix after import math: sqrt(4) alone raises NameError.

Summary

  • import m, from m import name and import m as alias load code from modules.
  • The standard library covers maths, dates, counting, randomness and much more.
  • Any .py file can be imported; guard script-only code with if __name__ == "__main__":.

Quiz: Modules and imports

1. After import math, how do you call the square root function?

import math gives you the module name; functions are reached with a dot.

2. What does from collections import Counter let you write?

from ... import brings the listed name straight into your file.

3. Why should you not name your script random.py?

Python finds your file first, so import random loads your script instead of the library.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.