HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Modules (import)

Modules (import)

Using ready-made libraries with import: math for mathematics, random for random numbers.

Simple enough for a 5-year-old to understand

import: bring in ready-made tools

In Python many ready-made functions come inside a "module". To use them you first import them.

The math module
1
import math
Import the math module.
ImportImport the math module.
2
print(math.sqrt(144))
Square root: 12.0
Output / InputSquare root: 12.0
3
print(math.floor(4.7))
Round down: 4
Output / InputRound down: 4
4
print(round(math.pi, 2))
The number Pi: 3.14
Output / InputThe number Pi: 3.14
ImportOutput / Input
💡 Hint: If you forget to write import math, you get a "math is not defined" error.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.What must you do before using the math module?
2.Print the square root of 81 using the math module.

🎯 Expected output: 9.0

Loading editor…

About this topic

A module is a Python file. As your code grows, keeping everything in a single file becomes unsustainable; you split functions into separate files and call them using import where needed. This way, each file is responsible for a single task.

Python's greatest strength is its ready-made module ecosystem. The standard library comes with hundreds of modules like math, date, file system, JSON, and random numbers without requiring installation. Beyond that, there are over three hundred thousand packages that can be installed with pip.

What you will learn

  • Calling a module with import
  • Importing a specific function directly
  • Importing with an alias
  • Using math, random, datetime, and json from the standard library
  • Writing your own module and calling it from another file
  • What main program control is used for

When to use?

When a file exceeds two hundred lines or starts containing independent tasks, it is time to split it into modules. Grouping related functions in the same file makes the code both findable and testable.

When it comes to built-in modules, the rule is simple: do not reinvent the wheel. For tasks like date calculation, JSON parsing, or random selection, the standard library already provides tested solutions.

Common mistakes

Wrong

import math
print(sqrt(16))

When you import a module as a whole, you access its functions using the module name. Writing sqrt directly gives "NameError: name 'sqrt' is not defined".

Correct

import math
print(math.sqrt(16))

Wrong

from math import *
print(pi)

Importing everything with an asterisk dumps all names in the module into the global namespace and can conflict with your own variables. It also becomes unclear where which name comes from; it's a habit that makes debugging difficult.

Correct

from math import pi
print(pi)

Wrong

# file name: random.py
import random
print(random.randint(1, 6))

Naming your own file after a standard module creates a conflict: Python imports your file instead of its own random module and throws the "AttributeError: module 'random' has no attribute 'randint'" error. Choose file names different from standard module names.

Correct

# file name: dice.py
import random
print(random.randint(1, 6))

Frequently asked questions

What is the difference between import and from import?

import gets the module as a whole, and you access functions with the module name. from import only gets the specified name, and you use it directly. The former is preferred in large projects because it clearly shows where the name comes from.

What is main program control used for?

A file can both be run directly and imported from another file. This structure, which checks the module name, prevents test codes or sample calls from running when the file is imported. It only activates when run directly.

Why can't I find the package I installed with pip?

The most common reason is that you installed the package into another Python environment: there may be multiple Python versions on the system. Using a virtual environment eliminates this confusion; each project keeps its own dependencies in its own folder.

Summary

A module is a Python file; you split code into pieces with import and benefit from ready-made libraries. Avoiding wildcard imports and not giving your files standard module names are two fundamental rules.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code