How Import Works in Python
import lets you use code from another Python file or from Python’s built-in and installed modules.
This is one of the most important Python basics. Once you understand how import works, it becomes much easier to read other people’s code and organize your own code into separate files.
Quick example
import math
print(math.sqrt(16))
from math import sqrt
print(sqrt(25))
Output:
4.0
5.0
Use import when you want to access code from another module.
import mathkeeps the module name, so you writemath.sqrt(...)from math import sqrtimports one specific name, so you writesqrt(...)
What import means
In Python, import brings code from another module into your program.
A few useful terms:
- A module is usually one
.pyfile - A package is a folder that contains Python modules
- An imported module is usually loaded once, then reused
For example, Python’s standard library includes modules like math, random, and json. You can also import your own files.
When Python imports a module, it:
- finds the module
- runs its top-level code once
- makes its functions, classes, and variables available
If you are new to this topic, you may also want to read Python modules explained.
Basic import forms
These are the main import patterns beginners see most often.
import module_name
import math
print(math.sqrt(9))
This form is usually the easiest to read.
Why:
- it shows where the function came from
- it avoids name conflicts
- it makes code clearer for beginners
from module_name import name
from math import sqrt
print(sqrt(9))
This is shorter.
It is useful when:
- you only need one or two names
- the imported name is very common in your code
The trade-off is that you no longer see the module name in each call.
import module_name as alias
import math as m
print(m.sqrt(9))
This gives the module a shorter name.
This is helpful when:
- the module name is long
- the alias is widely understood
from module_name import name as alias
from math import sqrt as square_root
print(square_root(9))
This is useful when:
- you want a clearer local name
- you need to avoid a naming conflict
Simple example with a standard library module
A good first example is the math module.
import math
number = 16
result = math.sqrt(number)
print(result)
Output:
4.0
Here:
mathis the modulesqrt()is a function inside that module- the dot in
math.sqrtis called dot notation
Dot notation helps show exactly where a function comes from. That makes code easier to understand.
For example, math.sqrt(9) is clearer than just sqrt(9) if the reader does not already know where sqrt came from.
Importing your own Python file
You can also import your own files.
Suppose you have this file structure:
helpers.py
main.py
helpers.py
def greet(name):
return f"Hello, {name}!"
main.py
import helpers
message = helpers.greet("Sam")
print(message)
Output:
Hello, Sam!
This works because:
helpers.pyis in the same folder asmain.py- the module name is the file name without
.py
So:
helpers.pybecomeshelperstools.pybecomestools
Use simple file names:
- lowercase is best
- avoid spaces
- avoid special characters
- do not start the file name with a number
If you want more step-by-step examples, see how to import a module in Python.
How Python finds modules
When you write an import statement, Python has to find that module.
At a beginner level, the main idea is simple. Python checks places such as:
- built-in modules
- installed packages
- folders in the module search path
- your current project folder
That search path is stored in sys.path.
You do not need to memorize the internals yet, but it helps to know that Python only imports modules it can find in one of those locations.
You can inspect the search path with this command:
python -c "import sys; print(sys.path)"
If a module cannot be found, you will often get a ModuleNotFoundError. For help with that, see ModuleNotFoundError: No module named X.
If you want to learn more about the search path itself, see sys.path explained and the Python sys module overview.
What happens when a module is imported
Importing a module does more than just “copy code.”
When Python imports a module:
- it runs the module’s top-level code
- it defines the functions and classes in that file
- it stores the loaded module so it can be reused
Example:
example_module.py
print("example_module is running")
def say_hello():
print("Hello")
main.py
import example_module
example_module.say_hello()
Output:
example_module is running
Hello
Notice what happened:
- the
print()at the top level ran during import - the function became available after the import
This can surprise beginners. If a file has code at the top level, that code runs when the file is imported.
Also, repeated imports in the same program usually do not rerun the whole file, because Python caches imported modules.
Why __name__ == '__main__' matters
This pattern helps prevent test code or demo code from running during import.
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("Sam"))
If you run the file directly:
python helpers.py
then the code inside this block runs.
But if another file imports helpers, this block does not run.
That makes it useful for:
- quick tests
- example usage
- demo output inside a module
Without this pattern, top-level test code can run every time the module is imported.
Common beginner import mistakes
Here are some of the most common problems beginners run into.
Naming your file like a standard library module
If you create a file called random.py, json.py, or sys.py, it can conflict with Python’s real module of the same name.
Example problem:
# random.py
import random
print(random.randint(1, 10))
This can break because your file may be imported instead of the standard library module.
Use unique file names for your own scripts.
Importing from the wrong folder
If your file is not in the expected location, Python may not find it.
Common causes:
- the file is in a different folder
- you are running the script from the wrong directory
- your project structure is not what you think it is
Using from module import *
Wildcard imports make code harder to read.
from math import *
This imports many names without showing where they came from.
That can cause confusion because:
- readers cannot easily tell where a name came from
- names can overwrite each other
- debugging becomes harder
Forgetting to install a package
Some modules are built in, but many are not.
For example, this may fail if the package is not installed:
import requests
If needed, install it with pip. See how to install a Python package with pip.
If you use virtual environments, also see how to create and use a virtual environment in Python.
Expecting import to copy code
import does not paste code into your file.
Instead, Python loads the module and lets you access its contents. That is why math.sqrt() still uses the module name, and why top-level code in the module can run during import.
Best practices for beginners
These habits will help you avoid many import problems.
- Prefer
import modulewhen possible - Use
from module import namewhen you only need one or two names - Avoid
from module import * - Keep module file names simple and lowercase
- Put test or demo code inside
if __name__ == "__main__":
A good beginner-friendly style looks like this:
import math
area = math.pi * 5 ** 2
print(area)
This is easy to read because it is clear that pi came from math.
Common causes of import problems
If your import is not working, check these first:
- the module file is not in the expected folder
- your file name conflicts with a standard library module or installed package
- the package is not installed
- the spelling or capitalization is wrong in the import statement
- top-level code in the imported file runs and causes unexpected behavior
Useful commands:
python --version
python your_script.py
python -c "import sys; print(sys.path)"
python -c "import math; print(math.__file__ if hasattr(math, '__file__') else 'built-in module')"
pip show requests
These help you check:
- which Python version you are using
- whether the script runs in the environment you expect
- where Python is looking for modules
- whether a package is installed
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 imports one name directly, so you write sqrt().
Why does Python say ModuleNotFoundError?
Usually Python cannot find the module file or package, or the package is not installed in the current environment.
See also ImportError: No module named X if you are troubleshooting an import problem.
Does Python run an imported file every time?
Normally no.
Python caches imported modules, so repeated imports in the same run usually reuse the loaded module.
Can I import my own Python file?
Yes.
If the file is in the right folder and has a valid name, Python can import it as a module.
Should beginners use from module import *?
Usually no.
It makes code harder to read because it hides where names came from.