How to Import a Module in Python
Importing a module lets you use code that already exists instead of writing everything yourself.
In Python, modules can be:
- built-in modules that come with Python
- installed packages from outside Python
- your own
.pyfiles
This guide shows you how to import modules, use functions from them, and avoid common beginner mistakes.
Quick answer
import math
print(math.sqrt(16))
from random import randint
print(randint(1, 10))
Use import module_name to load a module.
Use from module_name import something when you only need specific names.
What importing a module means
A module is a Python file that contains reusable code.
When you import a module, Python makes that code available in your program.
Many useful modules come with Python, including:
mathfor math functionsrandomfor random valuessysfor Python system information
If you are new to this topic, see Python modules explained.
Use import module_name
Write import followed by the module name.
Then use the module's functions with dot notation.
import math
print(math.sqrt(25))
print(math.ceil(4.2))
Expected output:
5.0
5
Why this style is useful
- It is clear where a function came from.
- It reduces name conflicts.
- It is common in real Python code.
For example, math.sqrt() clearly shows that sqrt comes from the math module.
Use from module_name import name
You can import only the function, class, or variable you need.
from math import sqrt
print(sqrt(36))
Expected output:
6.0
With this style, you call sqrt() directly instead of writing math.sqrt().
When to use this
This can be useful in short scripts and simple examples.
But in larger files, it can be less clear because you do not see the module name before the function.
If you want to understand this more deeply, read how import works in Python.
Rename a module with as
You can give a module a shorter name with as.
import math as m
print(m.sqrt(49))
print(m.pi)
Expected output:
7.0
3.141592653589793
This is helpful when:
- the module name is long
- you want shorter code
- a short alias is commonly used
For example, import math as m works, though many real-world aliases are even more common with other libraries.
Import your own Python file
You can also import your own file.
If you have a file named helpers.py in the same folder as your script, you can import it like this:
helpers.py
def greet(name):
return f"Hello, {name}!"
main.py
import helpers
print(helpers.greet("Sam"))
Expected output:
Hello, Sam!
Important points:
- Put both files in the same folder.
- Use the filename without
.py. helpers.pybecomesimport helpers.
Do not write this:
import helpers.py
That is invalid syntax for imports.
How to check that the import worked
The simplest way is to run your script and use something from the module.
For example:
import math
print(math.sqrt(64))
If the import works, Python runs the code normally.
If Python cannot find the module, you may get a ModuleNotFoundError. See how to fix ModuleNotFoundError: No module named x.
Useful commands for testing imports:
python your_script.py
python -c "import math; print(math.sqrt(25))"
python -c "import sys; print(sys.path)"
pip show requests
python -m pip install requests
What these do:
python your_script.pyruns your filepython -c "import math; print(math.sqrt(25))"tests an import quicklypython -c "import sys; print(sys.path)"shows where Python looks for modulespip show requestschecks whether a package is installedpython -m pip install requestsinstalls a package
If you need help with installation, read how to install a Python package with pip.
When beginners get stuck
These are common reasons imports fail:
- Misspelled module name
- Using
import myfile.pyinstead ofimport myfile - The module file is not in the same folder or Python path
- The package is not installed
- Your file name conflicts with a real module name
Common beginner mistakes
Using the .py extension
Wrong:
import helpers.py
Right:
import helpers
Naming your file the same as a standard module
If you name your file random.py, then this can cause problems:
import random
Python may import your file instead of the standard library module.
Try to avoid names like:
random.pymath.pysys.py
Importing a package that is not installed
This will fail if the package is missing:
import requests
If needed, install it:
python -m pip install requests
FAQ
What is the difference between import math and from math import sqrt?
import math keeps the module name, so you write math.sqrt().
from math import sqrt lets you write sqrt() directly.
Can I import my own Python file?
Yes. If your file is in the same folder and is named helpers.py, you can usually write import helpers.
Why do I get ModuleNotFoundError?
Python cannot find the module. Check the spelling, file location, and whether the package is installed.
Should I use from module import *?
Usually no. It imports many names at once and can make code harder to read.
See also
- Python modules explained
- How import works in Python
- How to fix
ModuleNotFoundError: No module named x - How to install a Python package with pip
- Python
dir()function explained
Next, learn how Python finds modules and how to fix import errors when a module cannot be found.