ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.techgeekbuzz.com/blog/absolute-vs-relative-imports-in-python/ |
| Last Crawled | 2026-04-13 12:51:34 (3 days ago) |
| First Indexed | 2022-06-03 17:41:21 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Absolute vs Relative Imports in Python | TechGeekBuzz.com |
| Meta Description | Absolute vs relative imports in Python - There are two ways to import modules and packages in Python, namely absolute and relative. |
| Meta Canonical | null |
| Boilerpipe Text | When you work on a big Python project, you will be dividing the Python code into multiple Python files. This splitting of code into multiple files gives a modular look to the project, and it is one of the best ways to write clean code.
Mostly, all the
top Python libraries
are written using multiple Python files known as Python modules and reside in multiple directories or folders known as
Python packages
. The Python import statement makes it easy for Pythonistas to use the code of one Python file in another.
However, still many Python beginner and intermediate learners find the import statement confusing and do not know the difference between relative and absolute imports in Python (absolute vs relative imports).
Here in this Python tutorial, we will walk you through the Python import statement and discuss the difference between relative and absolute imports.
What is Import in Python?
import
is a
Python keyword
that is used for importing code in the current Python script. The Python import statement is generally used when we want to access another Python file, Python module, or Python package in the current script.
For instance, suppose you want to use the Python
math
module to
round off a floating-point number to a whole number
. First, you need to import the math module in your current script before using the floor() and ceil() methods.
Example
#error
>>> num = 39.7
>>> math.floor(num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
If you do not import the module you wish to use in your Python script, then
Python will throw a NameError
.
Example
>>> import math
>>> num = 39.7
>>> math.floor(num)
39
The import statement will add the module object to the current scope of your program. So if you want to use the imported module throughout any section of the program, you need to mention the import statement at the top of your Python program or script.
How Does The Import Statement Work?
The import statement goes through a lot behind the scenes. First, the import statement looks for the module or package in the
sys.modules
. The
sys.module
section stores all the previous imported code, and it is like cached storage for already imported modules.
If the import does not find the module or package in the
sys.module
section, it starts searching through the Python Standard Libraries. Still, if the import does not find the module in the
Python Standard Libraries
, it starts searching the same in the installed libraries section or
system.path
.
Even if the import statement is not able to find the module or package, it throws a ModuleNotFoundError. The search for import search starts from your current directory, and then it goes to the Python built-in Standard Libraries, and at last look for the same in the installed libraries.
Import Statement Syntax
Generally, we mention the import statement at the top of our Python program or script, so we can access the module from any section of the program. Names of modules and packages are case-sensitive, and thus, make sure you are writing the correct name.
import module_name
or
import package_name
The import statement imports the complete module or packages to your current Python program or script. However, Python also provides the
from
statement that works along with
import
. This makes the import statement sophisticated.
from package_name import module
or
from module_name import module_function
Example
#import module
>>> import math
>>> num = 34.88
>>> math.floor(num)
34
#from module import module function
>>> from math import floor
>>> num =35.9
>>> floor(num)
35
#from module import all functions or submodule
>>> from math import *
>>> num =48.37
>>> ceil(num)
49
Types of Python Imports
You can use two different types of import statements to import a module or package to your current Python program. These are:
Absolute Imports
Relative Imports
1. Python Absolute Imports
In the Python absolute import, we include the absolute path of the module from its root directory or folder. To separate every subdirectory or module, we use the period symbol (.). Absolute imports are generally used when we only want to import specific functions or sub-modules from modules or packages.
Syntax
??? project
| ??? package_A
| ? ??? __init__.py
| ? ??? module_A.py
| ? ??? module_B.py
| ??? package_B
| ??? __init__.py
| ??? module_C.py
| ??? module_D.py
| ??? subpackage_A
| ??? module_E.py
For instance, if you want to access a method
function_E
that is written inside the
module_E,
you need to specify the absolute path to
module_E.py
and grab the
function_E
method. For example:
from project.pakage_B.subpackage_A.module_E import function_E.
Using the same syntax, you can access any module and package of the project.
Pros of Absolute Import
Absolute imports are clean and give a brief idea about the method or module you want to use.
It also becomes easy for the interpreter to directly look for the specified module.
Cons of Absolute Import
In some cases, the absolute import becomes quite long because of subfolders or modules. And this could expand the import statement into two lines.
In absolute importing, you have to be careful while mentioning the root folder. If you are in the subfolder, then the above folder will be your root folder.
2. Python Relative Imports
In relative imports, we mention the import statement relative to the location of the current Python program or where we are using the import statement.
Syntax
??? project1
| ??? package_A
| ? ??? __init__.py
| ? ??? module_A.py
| ? ??? module_B.py
| ? ??? CurrentProgram.py
| ??? package_B
| ??? __init__.py
| ??? module_C.py
| ??? module_D.py
| ??? subpackage_B
| ??? module_E.py
|------project2
Suppose you are at
CurrentProgram.py
, and you want to access the
function_B
method from
module_B.py
. So, instead of using the absolute import, it would be a great practice to use the relative import because
CurrentProgram.py
and
module_B.py
are in the same location. In relative import, we use the (.)
dot notation
to represent the current directory.
#CurrentProgram.py
from .module_B import function_B
Access the
module_E
method
function_E
from
module_C
.
#package_B/module_C
.subpackage_B.module_E import function_E
Pros of Relative Import
For small projects where you are not using many sub-folders, relative import provides a cleaner way to import
Python modules
.
The relative import rarely gets too long.
Cons of Relative Import
If you are working on a project with multiple subfolders, their relative import could get messy.
For shared projects, relative imports are hard to read.
Absolute vs Relative Imports in Python: A Head-to-Head Comparison
Python Absolute
Python Relative
In absolute import, we specify the complete path of the module location from its root directory or package.
In relative impart, we specify the path of the module location from the current or working Python program or script.
Absolute imports are generally long.
Relative imports are generally short.
Absolute imports are more readable and clearer.
Relative imports are less clear.
Conclusion
That was all about absolute vs relative imports in Python. Generally, Python developers prefer absolute import when they are working on shared or big projects. Because there, they want to make sure that other developers could also get the full path of the import module.
Relative imports are helpful when you are working alone on a Python project, or the module is in the same directory where you are importing the module.
While importing the module, be careful with the (.) dot notation. In absolute imports, it is used to separate the modules and sub-packages, and in relative imports, it is used to represent the current directory and separate modules and sub-packages.
People are also reading:
Online Python Compiler
Extract YouTube Comments in Python
Face Detection in Python
Edge Detection in Python
Detect Contours in Images in Python
HOG Feature Extraction in Python
Starting Python Coding on a MacBook
Python Counter in Collections
Extract all stored Chrome Password with Python
Install Python package using Jupyter Notebook |
| Markdown | [ TechGeekBuzz Techies World for Tech Geeks](https://www.techgeekbuzz.com/)
#####
- [Tutorials](https://www.techgeekbuzz.com/blog/absolute-vs-relative-imports-in-python/)
- [Python](https://www.techgeekbuzz.com/tutorial/python/)
***
- [C++](https://www.techgeekbuzz.com/tutorial/c++/)
***
- [SQL](https://www.techgeekbuzz.com/tutorial/sql/)
***
- [JavaScript](https://www.techgeekbuzz.com/tutorial/javascript/)
***
- [CSS](https://www.techgeekbuzz.com/tutorial/css/)
***
- [HTML](https://www.techgeekbuzz.com/tutorial/html/)
***
- [PHP](https://www.techgeekbuzz.com/tutorial/php/)
***
- [Data Structure](https://www.techgeekbuzz.com/tutorial/data-structure/)
***
- [Docker](https://www.techgeekbuzz.com/tutorial/docker/)
***
- [Java](https://www.techgeekbuzz.com/tutorial/java/)
***
- [Angular](https://www.techgeekbuzz.com/tutorial/angular/)
***
- [Scala](https://www.techgeekbuzz.com/tutorial/scala/)
***
- [React](https://www.techgeekbuzz.com/tutorial/react/)
***
- [Blog](https://www.techgeekbuzz.com/blog/)
- [About Us](https://www.techgeekbuzz.com/about)
- [Contact Us](https://www.techgeekbuzz.com/about#contact-us)
- [Tools](https://www.techgeekbuzz.com/tools)
- [Roadmaps](https://www.techgeekbuzz.com/roadmap/)
- [Tutorials](https://www.techgeekbuzz.com/blog/absolute-vs-relative-imports-in-python/)
- [ Python](https://www.techgeekbuzz.com/tutorial/python/)
***
- [ C++](https://www.techgeekbuzz.com/tutorial/c++/)
***
- [ SQL](https://www.techgeekbuzz.com/tutorial/sql/)
***
- [ JavaScript](https://www.techgeekbuzz.com/tutorial/javascript/)
***
- [ CSS](https://www.techgeekbuzz.com/tutorial/css/)
***
- [ HTML](https://www.techgeekbuzz.com/tutorial/html/)
***
- [ PHP](https://www.techgeekbuzz.com/tutorial/php/)
***
- [ Data Structure](https://www.techgeekbuzz.com/tutorial/data-structure/)
***
- [ Docker](https://www.techgeekbuzz.com/tutorial/docker/)
***
- [ Java](https://www.techgeekbuzz.com/tutorial/java/)
***
- [ Angular](https://www.techgeekbuzz.com/tutorial/angular/)
***
- [ Scala](https://www.techgeekbuzz.com/tutorial/scala/)
***
- [React](https://www.techgeekbuzz.com/tutorial/react/)
***
- [Blog](https://www.techgeekbuzz.com/blog/)
- [Tools](https://www.techgeekbuzz.com/tools)
- [Roadmaps](https://www.techgeekbuzz.com/roadmap/)
# Absolute vs Relative Imports in Python
Posted in [DIFFERENCE](https://www.techgeekbuzz.com/blog/category/difference/) / [PROGRAMMING LANGUAGE](https://www.techgeekbuzz.com/blog/category/programming-language/) / [PYTHON](https://www.techgeekbuzz.com/blog/category/python/)


[Vinay Khatri](https://www.techgeekbuzz.com/blog/author/vinaykhatri/)
Last updated on September 26, 2025
Share on:
## Table of Content
When you work on a big Python project, you will be dividing the Python code into multiple Python files. This splitting of code into multiple files gives a modular look to the project, and it is one of the best ways to write clean code.
Mostly, all the [top Python libraries](https://www.techgeekbuzz.com/blog/best-python-libraries/) are written using multiple Python files known as Python modules and reside in multiple directories or folders known as [Python packages](https://www.techgeekbuzz.com/blog/install-python-package-using-jupyter-notebook/) . The Python import statement makes it easy for Pythonistas to use the code of one Python file in another.
However, still many Python beginner and intermediate learners find the import statement confusing and do not know the difference between relative and absolute imports in Python (absolute vs relative imports).
Here in this Python tutorial, we will walk you through the Python import statement and discuss the difference between relative and absolute imports.
## **What is Import in Python?**
is a [Python keyword](https://www.techgeekbuzz.com/tutorial/python/python-keywords-identifiers/) that is used for importing code in the current Python script. The Python import statement is generally used when we want to access another Python file, Python module, or Python package in the current script.
For instance, suppose you want to use the Python module to [round off a floating-point number to a whole number](https://www.techgeekbuzz.com/blog/how-to-round-to-whole-numbers-in-python/) . First, you need to import the math module in your current script before using the floor() and ceil() methods.
**Example** **\#error**
```
```
If you do not import the module you wish to use in your Python script, then [Python will throw a NameError](https://www.techgeekbuzz.com/blog/python-nameerror-name-is-not-defined-solution/) . **Example**
```
```
The import statement will add the module object to the current scope of your program. So if you want to use the imported module throughout any section of the program, you need to mention the import statement at the top of your Python program or script.
### **How Does The Import Statement Work?**
The import statement goes through a lot behind the scenes. First, the import statement looks for the module or package in the . The section stores all the previous imported code, and it is like cached storage for already imported modules.
If the import does not find the module or package in the section, it starts searching through the Python Standard Libraries. Still, if the import does not find the module in the ***Python Standard Libraries*** , it starts searching the same in the installed libraries section or .
Even if the import statement is not able to find the module or package, it throws a ModuleNotFoundError. The search for import search starts from your current directory, and then it goes to the Python built-in Standard Libraries, and at last look for the same in the installed libraries.
### **Import Statement Syntax**
Generally, we mention the import statement at the top of our Python program or script, so we can access the module from any section of the program. Names of modules and packages are case-sensitive, and thus, make sure you are writing the correct name.
```
import module_name
```
**or**
```
import package_name
```
The import statement imports the complete module or packages to your current Python program or script. However, Python also provides the statement that works along with . This makes the import statement sophisticated.
```
```
**Example**
```
```
## **Types of Python Imports**
You can use two different types of import statements to import a module or package to your current Python program. These are:
1. Absolute Imports
2. Relative Imports
### **1\. Python Absolute Imports**
In the Python absolute import, we include the absolute path of the module from its root directory or folder. To separate every subdirectory or module, we use the period symbol (.). Absolute imports are generally used when we only want to import specific functions or sub-modules from modules or packages.
**Syntax**
```
```
For instance, if you want to access a method that is written inside the you need to specify the absolute path to and grab the method. For example:
```
from project.pakage_B.subpackage_A.module_E import function_E.
```
Using the same syntax, you can access any module and package of the project.
#### **Pros of Absolute Import**
- Absolute imports are clean and give a brief idea about the method or module you want to use.
- It also becomes easy for the interpreter to directly look for the specified module.
#### **Cons of Absolute Import**
- In some cases, the absolute import becomes quite long because of subfolders or modules. And this could expand the import statement into two lines.
- In absolute importing, you have to be careful while mentioning the root folder. If you are in the subfolder, then the above folder will be your root folder.
### **2\. Python Relative Imports**
In relative imports, we mention the import statement relative to the location of the current Python program or where we are using the import statement.
**Syntax**
```
```
Suppose you are at , and you want to access the method from . So, instead of using the absolute import, it would be a great practice to use the relative import because and are in the same location. In relative import, we use the (.) [dot notation](https://www.askpython.com/python/built-in-methods/dot-notation) to represent the current directory. **\#CurrentProgram.py**
```
from .module_B import function_B
```
Access the method from . **\#package\_B/module\_C**
```
.subpackage_B.module_E import function_E
```
#### **Pros of Relative Import**
- For small projects where you are not using many sub-folders, relative import provides a cleaner way to import [Python modules](https://www.techgeekbuzz.com/tutorial/python/python-module/) .
- The relative import rarely gets too long.
#### **Cons of Relative Import**
- If you are working on a project with multiple subfolders, their relative import could get messy.
- For shared projects, relative imports are hard to read.
### **Absolute vs Relative Imports in Python: A Head-to-Head Comparison**
| | |
|---|---|
| **Python Absolute** | **Python Relative** |
| In absolute import, we specify the complete path of the module location from its root directory or package. | In relative impart, we specify the path of the module location from the current or working Python program or script. |
| Absolute imports are generally long. | Relative imports are generally short. |
| Absolute imports are more readable and clearer. | Relative imports are less clear. |
### **Conclusion**
That was all about absolute vs relative imports in Python. Generally, Python developers prefer absolute import when they are working on shared or big projects. Because there, they want to make sure that other developers could also get the full path of the import module.
Relative imports are helpful when you are working alone on a Python project, or the module is in the same directory where you are importing the module.
While importing the module, be careful with the (.) dot notation. In absolute imports, it is used to separate the modules and sub-packages, and in relative imports, it is used to represent the current directory and separate modules and sub-packages.
**People are also reading:**
- [Online Python Compiler](https://www.techgeekbuzz.com/blog/online-python-compiler/)
- [Extract YouTube Comments in Python](https://www.techgeekbuzz.com/blog/how-to-extract-youtube-comments-in-python-using-youtube-api/)
- [Face Detection in Python](https://www.techgeekbuzz.com/blog/face-detection-using-opencv-in-python/)
- [Edge Detection in Python](https://www.techgeekbuzz.com/blog/how-to-perform-edge-detection-in-python-using-opencv/)
- [Detect Contours in Images in Python](https://www.techgeekbuzz.com/blog/how-to-detect-contours-in-images-using-opencv-in-python/)
- [HOG Feature Extraction in Python](https://www.techgeekbuzz.com/blog/how-to-apply-hog-feature-extraction-in-python/)
- [Starting Python Coding on a MacBook](https://www.techgeekbuzz.com/blog/your-guide-for-starting-python-coding-on-a-macbook/)
- [Python Counter in Collections](https://www.techgeekbuzz.com/blog/python-counter-in-collections-with-example/)
- [Extract all stored Chrome Password with Python](https://www.techgeekbuzz.com/blog/how-to-extract-all-stored-chrome-password-with-python/)
- [Install Python package using Jupyter Notebook](https://www.techgeekbuzz.com/blog/install-python-package-using-jupyter-notebook/)

[Vinay Khatri](https://www.techgeekbuzz.com/blog/author/vinaykhatri/)
I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.
## FAQs
***
#### How do absolute import and relative import differ?
While absolute import entails the path from a project's root folder to the desired module, relative import entails the path starting from the path of the current module to the desired module.
#### What are the types of relative imports?
The two types of relative imports are implicit relative imports and explicit relative imports.
#### Which type of relative import does Python 3.x support?
Python 3.x supports explicit relative imports.
#### What are the types of modules in Python?
Built-in and user-defined modules are the two types of modules in Python.
#### When should you use relative import?
You should use relative import when you are alone working on a Python project or when the module is in the same directory where you are importing the module.
### Related Blogs
***
[](https://www.techgeekbuzz.com/blog/how-to-start-coding/)
[PROGRAMMING LANGUAGE](https://www.techgeekbuzz.com/blog/category/programming-language/)
### [How to Start Coding: A Complete Guide](https://www.techgeekbuzz.com/blog/how-to-start-coding/)
Coding has become one of the most valuable skills in the modern digital world. Almost every industr…
19 March, 2026
[](https://www.techgeekbuzz.com/blog/coding-platforms/)
[PROGRAMMING LANGUAGE](https://www.techgeekbuzz.com/blog/category/programming-language/)
### [Best Coding Platforms for Beginners in 2026](https://www.techgeekbuzz.com/blog/coding-platforms/)
Picking up coding has become a highly useful skill lately. By 2026, you don’t need to be an e…
2 March, 2026
[](https://www.techgeekbuzz.com/blog/history-of-programming-languages/)
[PROGRAMMING LANGUAGE](https://www.techgeekbuzz.com/blog/category/programming-language/)
### [History of Programming Languages](https://www.techgeekbuzz.com/blog/history-of-programming-languages/)
Programming languages are the foundations of any kind of project or website you visit or app you ha…
7 November, 2025
### Tutorials
- [Python Tutorial](https://www.techgeekbuzz.com/tutorial/python/)
- [C++ Tutorial](https://www.techgeekbuzz.com/tutorial/c++/)
- [SQL Tutorial](https://www.techgeekbuzz.com/tutorial/sql/)
- [JavaScript Tutorial](https://www.techgeekbuzz.com/tutorial/javascript/)
- [CSS Tutorial](https://www.techgeekbuzz.com/tutorial/css/)
- [HTML Tutorial](https://www.techgeekbuzz.com/tutorial/html/)
- [PHP Tutorial](https://www.techgeekbuzz.com/tutorial/php/)
- [Data Structure Tutorial](https://www.techgeekbuzz.com/tutorial/data-structure/)
- [Docker Tutorial](https://www.techgeekbuzz.com/tutorial/docker/)
- [Java Tutorial](https://www.techgeekbuzz.com/tutorial/java/)
- [Angular Tutorial](https://www.techgeekbuzz.com/tutorial/angular/)
- [Scala Tutorial](https://www.techgeekbuzz.com/tutorial/scala/)
- [React Tutorial](https://www.techgeekbuzz.com/tutorial/react/)
### Top Books
- [Python Books](https://www.techgeekbuzz.com/blog/best-python-books/)
- [C++ Books](https://www.techgeekbuzz.com/blog/best-cpp-books/)
- [C Books](https://www.techgeekbuzz.com/blog/best-c-books/)
- [Java Books](https://www.techgeekbuzz.com/blog/best-java-books/)
- [PHP Books](https://www.techgeekbuzz.com/blog/best-php-books-for-programmers/)
- [Data Science Books](https://www.techgeekbuzz.com/blog/data-science-books/)
- [R Books](https://www.techgeekbuzz.com/blog/best-r-books/)
### Top Interview Questions
- [Python Interview Questions](https://www.techgeekbuzz.com/blog/python-interview-questions/)
- [C++ Interview Questions](https://www.techgeekbuzz.com/blog/top-cpp-interview-questions/)
- [C Interview Questions](https://www.techgeekbuzz.com/blog/c-interview-questions/)
- [Java Interview Questions](https://www.techgeekbuzz.com/blog/core-java-interview-questions/)
- [PHP Interview Questions](https://www.techgeekbuzz.com/blog/php-interview-questions/)
- [jquery Interview Questions](https://www.techgeekbuzz.com/blog/top-jquery-interview-questions/)
- [Data Science Interview Questions](https://www.techgeekbuzz.com/blog/data-science-interview-questions/)
- [Programming Interview Questions](https://www.techgeekbuzz.com/blog/programming-interview-questions/)
### Top Projects
- [Python Projects](https://www.techgeekbuzz.com/blog/python-projects/)
- [C++ Projects](https://www.techgeekbuzz.com/blog/cpp-projects/)
- [HTML Projects](https://www.techgeekbuzz.com/blog/html-projects/)
- [Java Projects](https://www.techgeekbuzz.com/blog/java-projects/)
- [PHP Projects](https://www.techgeekbuzz.com/blog/php-projects/)
- [Android Projects](https://www.techgeekbuzz.com/blog/android-projects/)
- [JavaScript Projects](https://www.techgeekbuzz.com/blog/best-javascript-projects/)
- [Data Science Projects](https://www.techgeekbuzz.com/blog/top-data-science-project-ideas/)
- [AI Projects](https://www.techgeekbuzz.com/blog/artificial-intelligence-projects/)
[](https://www.techgeekbuzz.com/)
### [TechGeekBuzz](https://www.techgeekbuzz.com/)
[Techies World for Tech Geeks](https://www.techgeekbuzz.com/)
## Get the latest tutorials and updates
[About Us](https://www.techgeekbuzz.com/about) [Advertising](https://www.techgeekbuzz.com/advertise-with-us) [Privacy Policy](https://www.techgeekbuzz.com/privacy) [Term and Condition](https://www.techgeekbuzz.com/term-and-condition) [Blog](https://www.techgeekbuzz.com/blog/) [Write for us](https://www.techgeekbuzz.com/write-for-us) [Roadmaps](https://www.techgeekbuzz.com/roadmap/) [Web Stories](https://www.techgeekbuzz.com/webstories/)
© TechGeekbuzz All rights reserved. |
| Readable Markdown | When you work on a big Python project, you will be dividing the Python code into multiple Python files. This splitting of code into multiple files gives a modular look to the project, and it is one of the best ways to write clean code.
Mostly, all the [top Python libraries](https://www.techgeekbuzz.com/blog/best-python-libraries/) are written using multiple Python files known as Python modules and reside in multiple directories or folders known as [Python packages](https://www.techgeekbuzz.com/blog/install-python-package-using-jupyter-notebook/) . The Python import statement makes it easy for Pythonistas to use the code of one Python file in another.
However, still many Python beginner and intermediate learners find the import statement confusing and do not know the difference between relative and absolute imports in Python (absolute vs relative imports).
Here in this Python tutorial, we will walk you through the Python import statement and discuss the difference between relative and absolute imports.
## **What is Import in Python?**
is a [Python keyword](https://www.techgeekbuzz.com/tutorial/python/python-keywords-identifiers/) that is used for importing code in the current Python script. The Python import statement is generally used when we want to access another Python file, Python module, or Python package in the current script.
For instance, suppose you want to use the Python module to [round off a floating-point number to a whole number](https://www.techgeekbuzz.com/blog/how-to-round-to-whole-numbers-in-python/) . First, you need to import the math module in your current script before using the floor() and ceil() methods.
**Example** **\#error**
```
```
If you do not import the module you wish to use in your Python script, then [Python will throw a NameError](https://www.techgeekbuzz.com/blog/python-nameerror-name-is-not-defined-solution/) . **Example**
```
```
The import statement will add the module object to the current scope of your program. So if you want to use the imported module throughout any section of the program, you need to mention the import statement at the top of your Python program or script.
### **How Does The Import Statement Work?**
The import statement goes through a lot behind the scenes. First, the import statement looks for the module or package in the . The section stores all the previous imported code, and it is like cached storage for already imported modules.
If the import does not find the module or package in the section, it starts searching through the Python Standard Libraries. Still, if the import does not find the module in the ***Python Standard Libraries*** , it starts searching the same in the installed libraries section or .
Even if the import statement is not able to find the module or package, it throws a ModuleNotFoundError. The search for import search starts from your current directory, and then it goes to the Python built-in Standard Libraries, and at last look for the same in the installed libraries.
### **Import Statement Syntax**
Generally, we mention the import statement at the top of our Python program or script, so we can access the module from any section of the program. Names of modules and packages are case-sensitive, and thus, make sure you are writing the correct name.
```
import module_name
```
**or**
```
import package_name
```
The import statement imports the complete module or packages to your current Python program or script. However, Python also provides the statement that works along with . This makes the import statement sophisticated.
```
```
**Example**
```
```
## **Types of Python Imports**
You can use two different types of import statements to import a module or package to your current Python program. These are:
1. Absolute Imports
2. Relative Imports
### **1\. Python Absolute Imports**
In the Python absolute import, we include the absolute path of the module from its root directory or folder. To separate every subdirectory or module, we use the period symbol (.). Absolute imports are generally used when we only want to import specific functions or sub-modules from modules or packages.
**Syntax**
```
```
For instance, if you want to access a method that is written inside the you need to specify the absolute path to and grab the method. For example:
```
from project.pakage_B.subpackage_A.module_E import function_E.
```
Using the same syntax, you can access any module and package of the project.
#### **Pros of Absolute Import**
- Absolute imports are clean and give a brief idea about the method or module you want to use.
- It also becomes easy for the interpreter to directly look for the specified module.
#### **Cons of Absolute Import**
- In some cases, the absolute import becomes quite long because of subfolders or modules. And this could expand the import statement into two lines.
- In absolute importing, you have to be careful while mentioning the root folder. If you are in the subfolder, then the above folder will be your root folder.
### **2\. Python Relative Imports**
In relative imports, we mention the import statement relative to the location of the current Python program or where we are using the import statement.
**Syntax**
```
```
Suppose you are at , and you want to access the method from . So, instead of using the absolute import, it would be a great practice to use the relative import because and are in the same location. In relative import, we use the (.) [dot notation](https://www.askpython.com/python/built-in-methods/dot-notation) to represent the current directory. **\#CurrentProgram.py**
```
from .module_B import function_B
```
Access the method from . **\#package\_B/module\_C**
```
.subpackage_B.module_E import function_E
```
#### **Pros of Relative Import**
- For small projects where you are not using many sub-folders, relative import provides a cleaner way to import [Python modules](https://www.techgeekbuzz.com/tutorial/python/python-module/) .
- The relative import rarely gets too long.
#### **Cons of Relative Import**
- If you are working on a project with multiple subfolders, their relative import could get messy.
- For shared projects, relative imports are hard to read.
### **Absolute vs Relative Imports in Python: A Head-to-Head Comparison**
| | |
|---|---|
| **Python Absolute** | **Python Relative** |
| In absolute import, we specify the complete path of the module location from its root directory or package. | In relative impart, we specify the path of the module location from the current or working Python program or script. |
| Absolute imports are generally long. | Relative imports are generally short. |
| Absolute imports are more readable and clearer. | Relative imports are less clear. |
### **Conclusion**
That was all about absolute vs relative imports in Python. Generally, Python developers prefer absolute import when they are working on shared or big projects. Because there, they want to make sure that other developers could also get the full path of the import module.
Relative imports are helpful when you are working alone on a Python project, or the module is in the same directory where you are importing the module.
While importing the module, be careful with the (.) dot notation. In absolute imports, it is used to separate the modules and sub-packages, and in relative imports, it is used to represent the current directory and separate modules and sub-packages.
**People are also reading:**
- [Online Python Compiler](https://www.techgeekbuzz.com/blog/online-python-compiler/)
- [Extract YouTube Comments in Python](https://www.techgeekbuzz.com/blog/how-to-extract-youtube-comments-in-python-using-youtube-api/)
- [Face Detection in Python](https://www.techgeekbuzz.com/blog/face-detection-using-opencv-in-python/)
- [Edge Detection in Python](https://www.techgeekbuzz.com/blog/how-to-perform-edge-detection-in-python-using-opencv/)
- [Detect Contours in Images in Python](https://www.techgeekbuzz.com/blog/how-to-detect-contours-in-images-using-opencv-in-python/)
- [HOG Feature Extraction in Python](https://www.techgeekbuzz.com/blog/how-to-apply-hog-feature-extraction-in-python/)
- [Starting Python Coding on a MacBook](https://www.techgeekbuzz.com/blog/your-guide-for-starting-python-coding-on-a-macbook/)
- [Python Counter in Collections](https://www.techgeekbuzz.com/blog/python-counter-in-collections-with-example/)
- [Extract all stored Chrome Password with Python](https://www.techgeekbuzz.com/blog/how-to-extract-all-stored-chrome-password-with-python/)
- [Install Python package using Jupyter Notebook](https://www.techgeekbuzz.com/blog/install-python-package-using-jupyter-notebook/) |
| Shard | 169 (laksa) |
| Root Hash | 4769853076903061169 |
| Unparsed URL | com,techgeekbuzz!www,/blog/absolute-vs-relative-imports-in-python/ s443 |