Skip to content
beginner

Importing Modules in Python

An import statement is not a magic incantation. It is a name-resolution request: you tell the running Python program, "find this module and make its names…

Published 2026-05-11Updated 2026-09-157 min read
Explore summer relaxation with a teal swimsuit covered in sand on a sunny beach.
Explore summer relaxation with a teal swimsuit covered in sand on a sunny beach. Photo by https://kaboompics.com/ on Pexels.

An import statement is not a magic incantation. It is a name-resolution request: you tell the running Python program, "find this module and make its names available to me." Understand that one mechanism and you can predict why an import succeeds, why it fails, and what to do about it—without memorizing a list of commands.

What a Module Is

A module is a file of Python code—functions, classes, or variables—that you can reuse. Think of it as a mini-program, written by someone else or by you earlier, that you plug into your current project instead of rewriting it.

Modules exist for a simple reason: they let you stop repeating yourself. When a task is common, someone has usually already solved it. Your job is to pull that solution in.

The useful way to sort the modules you will meet is not by their name but by where their code comes from and whether you must install it first:

  • Standard library modules ship with Python and are ready to use immediately. math, random, and datetime are examples.
  • Third-party packages are written by others and installed into your environment before you can import them. requests and pandas are examples.
  • Local modules are files in your own project that you write and then import from other files.

All three use the same import syntax. What differs is where Python finds them. A third-party package must be installed before it is reachable; a local module must sit where the running script can see it; a standard library module is already there.

Knowledge check

Check your understanding

Answer this question before you continue.

Which statement correctly describes a standard library module?
Single Choice

Focus: Distinguish standard library modules from third-party packages based on whether they ship with Python.

How Python Finds a Module

Flowchart showing a running Python interpreter receiving an import request, searching standard library, installed packages, and project folders, then reaching either the imported module or a ModuleNotFoundError.
An import works when the running interpreter can reach the requested module; otherwise Python reports that it cannot find it.

When you write import math, the running interpreter searches the locations it can reach—its own standard library, any installed packages, and the directories on its import path. If it finds the module, it loads it and makes its names available. If it does not, you get an error.

That is the mental model worth keeping: an import succeeds only if the module is reachable from the interpreter that is running your code. This is why "it works on my machine" problems happen. The script and the pip you ran in the terminal can point at different environments, so the module you installed may not be visible to the script you are running.

Knowledge check

Check your understanding

Answer this question before you continue.

A package was installed from one terminal, but a script still raises ModuleNotFoundError. What is the most likely explanation taught in the article?
Misconception Check

Focus: Explain that an import succeeds only when the running interpreter can reach the requested module.

Your First Import

Let's make that concrete. Open a Python file and import the math module:

import math

result = math.sqrt(16)
print(result)
4.0

The pattern is always the same: module_name.function_name. The dot is how Python says "look inside this module." Because you imported the whole module, math is now a name in your program, and sqrt lives inside it.

Common mistake

Forgetting the module name in front of the function is the classic beginner slip. sqrt(16) fails with a NameError because sqrt only exists inside math. You have to write math.sqrt(16).

Knowledge check

Check your understanding

Answer this question before you continue.

What does this code print? import math result = math.sqrt(16) print(result)
Output Prediction

Focus: Predict the result of importing a whole module and accessing one of its functions with dot notation.

Importing Specific Parts of a Module

Sometimes you only need one or two things from a module. The from ... import ... statement brings in just what you need:

from math import sqrt

print(sqrt(25))
5.0

Now you can call sqrt(25) directly without typing math. every time. That is convenient when you use a function many times.

You can also import several names at once:

from random import randint, choice

When to use which style

Import styleUse this whenAvoid when
import mathYou use many functions from the module, or you want it obvious where each name came fromYou only need one small function and want shorter code
from math import sqrtYou use one or two functions a lot and want cleaner linesYou import so many names that you lose track of which module they came from

A good beginner rule: start with import module and the full module.name form. It keeps your code self-documenting—anyone reading it can see exactly where each name came from. Switch to from module import name when the full name makes your lines noisy and you are confident about what you are pulling in.

Common mistake

Avoid from math import *. The star imports every public name from the module into your program, which can silently overwrite your own variables and make bugs hard to trace. It is a shortcut that costs you clarity.

Knowledge check

Check your understanding

Answer this question before you continue.

Which change fixes this code? from math import sqrt print(math.sqrt(16))
Debugging

Focus: Correct a mismatch between a from-import statement and the name used to call the imported function.

When an Import Fails

Import errors are normal when you are learning, and they are useful: each one is evidence about what Python could and could not find. When one appears, work through the same reachability question in order.

1. Check the spelling first.

Python is picky about spelling. improt math or import maths both fail. Check the exact name before you do anything else.

2. Ask whether the module is installed and reachable.

If you import a module that is not in the standard library and you have not installed it, Python raises ModuleNotFoundError:

import requests  # fails unless requests is installed
ModuleNotFoundError: No module named 'requests'

To fix it, install the module with pip in your terminal:

pip install requests

Then run your script again with the same Python that owns that pip. If the error persists, the script is probably running under a different interpreter than the one you installed into.

Here is the observable check that makes that diagnosis concrete. Ask the interpreter you are running your script with which Python it is:

python -c "import sys; print(sys.executable)"

That prints the full path to the interpreter. Then install the package through that same interpreter:

python -m pip install requests

Using python -m pip instead of a bare pip ties the install to the exact Python you are running. If the path printed above and the interpreter running your script are the same, the module you install will be reachable.

3. Match your from import to how you call it.

If you write from math import sqrt and then call math.sqrt(16), you will get a NameError because math itself was never imported—only sqrt was. Pick one style and stay consistent.

Frequently Asked Questions

Q: Can I import more than one module in a single line?

Yes: import math, random works. But most developers prefer one import per line because it is easier to read and to remove later.

Q: What happens if I import the same module twice?

Python loads each module only once per program run. Importing it again just reuses the already-loaded version, so it does not hurt anything.

Q: How do I see what is inside a module?

Use the dir() function: dir(math) prints a list of the names the module provides.

Next Step: Run One Import

The fastest way to make this stick is to run it. Open a Python file and try this single task: use random to pick a random item from a list.

import random

colors = ["red", "green", "blue"]
print(random.choice(colors))

Run it a few times. Each run prints one of the three colors, and the output changes because choice is random—so do not expect a fixed result. That is the point: you imported a module, called one of its functions, and observed the consequence.

Now make one small change that tests the model you just built. Replace random with a misspelled name, such as import radnom, and run the file again. You should get a ModuleNotFoundError. That error is not a failure of your understanding—it is confirmation that Python really does search for the exact name you gave it and stops when it cannot find it.

When you are ready to reuse your own code the same way you reuse Python's, write a small module yourself. Keep the same rule in mind: choose an explicit import style, run it with the intended interpreter, and read every error as evidence about what Python can find.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

Which command does the article recommend for installing a package through the Python interpreter you are running?
Question 1 of 2Single Choice

Focus: Choose the installation command that ties pip to the Python interpreter being used.

According to the article's guidance, which style is a good starting choice when you use many functions from a module?
Question 2 of 2Misconception Check

Focus: Select an import style that keeps the source module clear when using several functions from it.

References

  1. Importing Modules — Python 3.14.7 - dokumentacjadocs.python.org
  2. Python import: Advanced Techniques and Tips – Real Pythonrealpython.com
Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python for Artificial Intelligence Starter Bundle

Build a Python foundation you can actually use. The Python for Artificial Intelligence Starter Pack brings together a guided path through setup, core programming concepts, data structures, files, JSON, APIs, debugging, and practical projects—so you can move quickly from running your first program to understanding and building useful software.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

A breathtaking sunrise over a vast mountainous landscape with clear skies.
beginner
6 min read

Defining Functions in Python

A function turns a block of code into a named tool you can call by name. Write the steps once, give them a name, and reuse them across your program instead…

Read tutorial