How to import modules in Python
Once you understand variables, functions, error handling, and file operations, the next major step in beginner Python is learning how to use code that already exists outside your current script. Up to this point, most examples in the course have been built only from Python syntax and logic written directly in one file. That is useful for learning the basics, but real Python programming becomes much more powerful when you start importing modules.
A module is a Python file that contains reusable code such as functions, variables, or classes. Instead of writing everything from scratch, you can import a module and use the tools it provides. Python includes many built-in modules, and later you can also install third-party packages. For beginners, modules are important because they show how Python scales beyond simple one-file examples and becomes a much richer programming environment.
This lesson explains how to import modules in Python in a clear, beginner-friendly way. You will learn what a module is, why imports matter, how the import statement works, how to import specific items, how aliases work, what common beginner mistakes look like, and how modules fit into real Python programming.
What is a module in Python
A module is a Python file that contains code you can reuse.
That code may include:
- functions
- variables
- constants
- classes
- helper logic
For example, Python has a built-in module called math that contains mathematical functions and constants. Instead of writing your own square root function from scratch, you can import math and use the one Python already provides.
This is one of the main ideas behind modules:
reusable code stored in separate files
A module helps organize logic and makes it easier to share useful functionality between scripts.
Why modules matter
Without modules, every program would need to contain all of its logic directly in one place. That would quickly become messy and repetitive.
Modules matter because they let you:
- reuse code instead of rewriting it
- keep programs more organized
- separate different parts of a project
- access built-in Python tools
- work with specialized functionality such as math, randomness, dates, files, or system operations
- scale from simple scripts to larger programs
For beginners, importing a module is one of the first clear signs that Python is more than just a set of language rules. It is also an ecosystem of reusable tools.
The basic import statement
The simplest form of import is:
This tells Python to load the math module.
Once it is imported, you can use items from that module with dot notation.
Example:
print(math.sqrt(25))
Output:
Here:
mathis the module namesqrtis a function inside that module
This is the basic beginner pattern for module usage.
Why dot notation is used
When you import a full module, Python usually expects you to access its contents through the module name.
Example:
print(math.pi)
print(math.ceil(4.2))
This is useful because it makes the source of the function or value clear.
math.pimeans thepivalue from themathmodulemath.ceilmeans theceil()function from themathmodule
This clarity becomes especially important in larger programs, where multiple modules may contain similarly named items.
A beginner example with the math module
The math module is one of the best beginner examples because its purpose is easy to understand.
Example:
number = 16
result = math.sqrt(number)
print(result)
Output:
This shows the full beginner pattern:
- import a module
- use something inside it
- store the result
- print the output
The math module also contains many other useful tools, such as:
sqrt()for square rootceil()for rounding upfloor()for rounding downpifor the mathematical constant pi
Importing more than one module
You can import multiple modules in one script.
Example:
import random
print(math.sqrt(9))
print(random.randint(1, 10))
This works because each module provides a different set of tools.
mathprovides mathematical functionsrandomprovides random-number-related functions
This is common in real Python code. A program often uses several modules together.
The random module
Another very useful beginner module is random.
Example:
print(random.randint(1, 10))
This prints a random integer between 1 and 10.
Each time you run it, the result may be different.
The random module is often used in beginner projects such as:
- guessing games
- random choices
- simulations
- shuffled content
- basic game logic
This makes it one of the most practical early modules to learn.
Importing a specific function from a module
Sometimes you do not want to import the whole module name into your code. Instead, you may want to import just one function or value.
Example:
print(sqrt(25))
Output:
This works without writing math.sqrt() because sqrt was imported directly.
This is shorter, but it also makes the origin slightly less explicit. That is why beginners should understand both styles.
Importing multiple specific items
You can import more than one specific item from the same module.
Example:
print(sqrt(16))
print(pi)
This is useful when you only need a few things from a module and want shorter code.
Still, in larger programs, importing the full module name can sometimes improve readability.
Using an alias with as
Python allows aliases with the as keyword. This means you can give a module or imported item a shorter or alternative name.
Example:
print(m.sqrt(36))
print(m.pi)
Output:
3.141592653589793
Here, m is an alias for math.
This can make code shorter, especially for modules with longer names.
Why aliases are useful
Aliases are useful when:
- the module name is long
- you want shorter code
- the short name is widely recognized
- you want to avoid naming conflicts
For beginners, the most common reason is simply convenience.
Another example:
print(rnd.randint(1, 5))
This works the same way as importing random normally.
Aliasing specific items
You can also alias a directly imported function or value.
Example:
print(square_root(49))
This is less common in very basic code, but it is valid and useful in some cases.
The difference between import module and from module import item
Beginners should clearly understand these two patterns.
Pattern 1: import module
print(math.sqrt(25))
Here, you must use the module name.
Pattern 2: from module import item
print(sqrt(25))
Here, you use the imported item directly.
Both are correct. The difference is mostly about style, readability, and scope.
A simple beginner rule is:
- use
import modulewhen you want clarity - use
from module import itemwhen you want shorter access to a few specific tools
Importing all items with *
Python also allows this form:
This imports many names from the module directly.
Technically it works, but beginners should usually avoid it.
Why?
Because it can:
- make the code less clear
- create name conflicts
- make it harder to see where something came from
Cleaner alternatives are usually better.
For beginners, from module import * is best treated as something to recognize, not something to use often.
Built-in modules versus third-party packages
At this stage, it helps to understand that not all modules are the same.
Built-in modules
These come with Python itself.
Examples:
mathrandomostimedatetime
You can usually import these directly without installing anything extra.
Third-party packages
These must be installed separately, usually with pip.
Examples later in a Python journey may include:
requestspandasnumpy
For this beginner lesson, the focus is mainly on built-in modules, because they are immediately available and easier to start with.
The os module
A common built-in module is os, which helps with operating system-related tasks.
Beginner example:
print(os.getcwd())
This prints the current working directory.
The os module becomes useful later for:
- file paths
- folders
- environment information
- automation tasks
At beginner level, it is enough to know that Python includes modules for many system-related tasks too.
The time module
The time module is another useful built-in example.
Example:
print(time.time())
This prints the current Unix timestamp.
A beginner-friendly use is pausing execution:
print(“Start”)
time.sleep(2)
print(“End”)
This causes Python to wait for 2 seconds between the two messages.
That makes time useful in simple timing-based examples or demonstrations.
The datetime module
The datetime module is used for working with dates and times.
Example:
now = datetime.datetime.now()
print(now)
This is more advanced than the earliest beginner topics, but it is useful to know that Python has built-in tools for date and time handling.
Modules can also be your own Python files
A module does not have to be built into Python. It can also be a Python file that you create yourself.
Example: imagine you have a file called helpers.py containing:
return “Hello, “ + name
Then in another file, you can write:
print(helpers.greet(“Alice”))
This works because helpers.py is acting as a module.
This is a very important concept because it shows that modules are not only something other people make. They are also a way for you to organize your own code across multiple files.
Why custom modules are useful
As programs grow, putting everything into one file becomes messy.
Custom modules help you:
- separate logic into smaller files
- keep related functions together
- reuse your own tools in different scripts
- make larger programs easier to manage
For beginners, this is a valuable idea even before building multi-file projects regularly.
What happens when Python imports a module
At a beginner level, the practical explanation is simple:
- Python looks for the module
- loads it
- makes its contents available according to the import style you used
If Python cannot find the module, you get an error such as:
ModuleNotFoundError
Example:
This fails because the module does not exist or is not available to Python.
ModuleNotFoundError explained
One common beginner problem is trying to import something Python cannot find.
Example:
If mymodule.py is not in the right place, or if the package is not installed, Python raises ModuleNotFoundError.
This is one reason beginners should:
- use correct module names
- check spelling carefully
- make sure third-party packages are installed when needed
- place their own module files where Python can find them
Common beginner mistakes with imports
Several problems appear often when beginners start using modules.
Forgetting to import before use
Incorrect:
This fails because math was never imported.
Correct:
print(math.sqrt(25))
Using the wrong name after importing
Example:
print(sqrt(25))
This fails because only the module was imported, not sqrt directly.
Correct:
or:
print(sqrt(25))
Misspelling the module name
Example:
This fails because the correct built-in module name is math, not maths.
Overusing from module import *
Example:
This works, but beginners often lose track of where names came from. Clearer import styles are usually better.
Forgetting aliases after using as
Example:
print(math.sqrt(16))
This fails because after aliasing, you must use the alias:
Naming conflicts
If you create your own file called random.py, it may conflict with Python’s real random module. Beginners should be careful not to name their own files after common built-in modules.
Practical beginner examples
Example 1: Use the math module
print(math.sqrt(64))
print(math.pi)
Example 2: Use the random module
print(random.randint(1, 10))
Example 3: Import a specific function
print(ceil(4.2))
Example 4: Import with an alias
print(m.floor(4.9))
Example 5: Use your own module
helpers.py
return “Hello, “ + name
Main file:
print(helpers.greet(“Emma”))
These examples cover the most important beginner import patterns.
Why modules are essential in real Python programming
Modules are one of the reasons Python is so productive. They allow you to build programs using existing tools instead of writing everything from zero.
In real Python work, modules are used for:
- math
- randomness
- date and time
- system operations
- file handling helpers
- web requests
- data analysis
- automation
- reusable project structure
This is why learning imports is such an important step. It connects beginner Python to the wider language ecosystem.
Good beginner habits with imports
A few habits make import-based code cleaner and easier to maintain.
Prefer clear import styles
For example:
is often very clear and readable.
Use aliases only when they help
Aliases are useful, but do not shorten names so much that the code becomes confusing.
Avoid wildcard imports
They make code harder to read and can cause conflicts.
Keep module names and function names distinct
Do not create your own file names that shadow built-in modules.
Import only what you need
This helps keep the code more understandable, especially while learning.
What you should understand before moving on
Before continuing to the next lesson, you should be comfortable with the following:
- a module is a Python file containing reusable code
import moduleloads a modulemodule.nameaccesses items inside the imported modulefrom module import itemimports a specific function or valueascreates an alias- built-in modules come with Python
- your own
.pyfiles can also act as modules ModuleNotFoundErrorhappens when Python cannot find the module- import style affects readability and clarity
If these ideas are clear, the next lesson on easy Python practice projects will be much easier, because many useful beginner projects combine everything learned so far, including modules.
Importing modules in Python allows you to use reusable code from built-in modules, third-party packages, or your own Python files. For beginners, this is an important step because it shows how Python programs can grow beyond one file and take advantage of a much larger ecosystem of tools.
Once you understand how import, from ... import ..., and aliases work, you can write cleaner and more powerful Python code without reinventing common functionality. This makes modules one of the most practical and essential concepts in real Python programming.
Image(s) used in this article are either AI-generated or sourced from royalty-free platforms like Pixabay or Pexels.
This article may contain affiliate links. If you purchase through these links, we may earn a commission at no extra cost to you.
Get the weekly RF & IT briefing
Radio guides, RF calculators, AI, Windows, Linux and satellite communication explainers. One useful email per week. No spam.
