🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 103 (from laksa026)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
2 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://www.geeksforgeeks.org/python/absolute-and-relative-imports-in-python/
Last Crawled2026-04-09 14:18:13 (2 days ago)
First Indexed2025-06-15 21:02:48 (9 months ago)
HTTP Status Code200
Meta TitleAbsolute and Relative Imports in Python - GeeksforGeeks
Meta DescriptionYour All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more., Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Meta Canonicalnull
Boilerpipe Text
In this article, we are going to see that absolute and relative imports in Python . Working of import in Python Import in Python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. Import statement consists of the import keyword along with the name of the module. The import statement involves two operations, it searches for a module and it binds the result of the search to a name in the local scope. When a module is imported, Python runs all of the code in the module file and made available to the importer file. When a module is imported the interpreter first searches it in sys.modules, which is the cache of all modules which have been previously imported. If it is not found then it searches in all built-in modules with that name, if it is found then the interpreter runs all of the code and is made available to the file. If the module is not found then it searches for a file with the same name in the list of directories given by the variable sys.path. sys.path is a variable containing a list of paths that contains python libraries, packages, and a directory containing the input script. For example, a module named math is imported then the interpreter searches it in built-in modules, if it is not found then it searches for a file named math.py in list of directories given by sys.path.  Output: 3.141592653589793 Syntax of import statements :   Users can import both packages and modules. (Note that importing a package essentially imports the package’s __init__.py file as a module.) Users can also import specific objects from a package or module. There are generally two types of import syntax. When you use the first one, you import the resource directly. import gfg gfg can be a package or a module. When a user uses the second syntax, then the user imports the resource from another package or module. from gfg import geek geek can be a module, subpackage, or object, such as a class or function.    Styling of import statements PEP8 , the official style guide for python, has a set of rules for how to formulate the python code to maximize its readability. For writing import statements there are some points to follow: Imports should always be written at the top of the file, just after any module comments and docstrings. Imports should usually be separated by a blank space. Imports should be grouped in the following order. Standard library imports (Python’s built-in modules) Related third party imports. Local application/library-specific imports It is also good to order import statements alphabetically within each import group.  # Python program showing # how to style import statements import math import os # Third party imports from flask import Flask from flask_restful import Api from flask_sqlalchemy import SQLAlchemy # Local application imports from local_module import local_class from local_package import local_function Absolute vs Relative Imports in Python Absolute imports in Python Absolute import involves a full path i.e., from the project's root folder to the desired module. An absolute import state that the resource is to be imported using its full path from the project’s root folder. Syntax and Practical Examples: Let’s see we have the following directory structure:     Here a directory named project, under which two subdirectories namely pkg1, pkg2. pkg1 has two modules, module1 and module2. pkg2 contains three modules, module3, module4, __init__.py, and one subpackage name subpkg1 which contains module5.py. Let's assume the following: pkg1 / module1.py contain a function, fun1 pkg2 / module3.py contain a function, fun2 pkg2 / subpkg1 / module5.py contain a function fun3 # Python program showing # practical example of # absolute imports # importing a fun1 from pkg1/module1 from pkg1.import module1 import fun1 from pkg1 import module2 # importing a fun2 from pkg2/module3 from pkg2 import module3 import fun2 # importing a fun3 from pkg2/subpkg1/module5 from pkg2.subpkg1.module5 import fun3 In this example, we are importing the modules by writing the full path from its root folder.    Pros and Cons of Absolute imports : Pros: Absolute imports are very useful because they are clear and straight to the point. Absolute import is easy to tell exactly from where the imported resource is, just by looking at the statement. Absolute import remains valid even if the current location of the import statement changes. Cons:   If the directory structure is very big then usage of absolute imports is not meaningful. In such a case using relative imports works well. from pkg1.subpkg2.subpkg3.subpkg4.module5 import fun6 Relative imports in Python Relative import specifies an object or module imported from its current location, that is the location where import statement resides. There two types of relative imports : Implicit relative imports - Implicit relative import have been disapproved in Python(3.x). Explicit relative imports - Explicit relative import have been approved in Python(3.x). Syntax and Practical Examples :  The syntax of relative import depends on the current location as well as the location of the module or object to be imported. Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on. Let’s see we have the following directory structure:     Let's assume the following: pkg1 / module1.py contain a function, fun1 pkg2 / module3.py contain a function, fun2 pkg2 / subpkg1 / module5.py contain a function fun3 # Python program showing # practical example of # relative imports # importing fun1 into pkg1/module1.py from .module1 import fun1 # importing fun2 and fun3 into pkg2/module3.py from .module3 import fun2 from .subpackage1.module5 import fun3 Pros and Cons of Relative imports :   Pros: Working with relative imports is concise and clear. Based on the current location it reduces the complexity of an import statement. Cons: Relative imports is not so readable as absolute ones. Using relative imports it is not easy because it is very hard to tell the location of a module.
Markdown
[![geeksforgeeks](https://media.geeksforgeeks.org/gfg-gg-logo.svg)](https://www.geeksforgeeks.org/) ![search icon](https://media.geeksforgeeks.org/auth-dashboard-uploads/Property=Light---Default.svg) - Sign In - [Courses]() - [Tutorials]() - [Interview Prep]() - [Python Tutorial](https://www.geeksforgeeks.org/python/python-programming-language-tutorial/) - [Data Types](https://www.geeksforgeeks.org/python/python-data-types/) - [Interview Questions](https://www.geeksforgeeks.org/python/python-interview-questions/) - [Examples](https://www.geeksforgeeks.org/python/python-programming-examples/) - [Quizzes](https://www.geeksforgeeks.org/python/python-quizzes/) - [DSA Python](https://www.geeksforgeeks.org/dsa/python-data-structures-and-algorithms/) - [Data Science](https://www.geeksforgeeks.org/data-science/data-science-with-python-tutorial/) - [NumPy](https://www.geeksforgeeks.org/python/numpy-tutorial/) - [Pandas](https://www.geeksforgeeks.org/pandas/pandas-tutorial/) - [Practice](https://www.geeksforgeeks.org/dsa/geeksforgeeks-practice-best-online-coding-platform/) # Absolute and Relative Imports in Python Last Updated : 11 Jul, 2025 In this article, we are going to see that **absolute and relative imports in Python**. ### **Working of import in Python** Import in Python is similar to \#include header\_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. Import statement consists of the import keyword along with the name of the module. The import statement involves two operations, it searches for a module and it binds the result of the search to a name in the local scope. When a module is imported, Python runs all of the code in the module file and made available to the importer file. When a module is imported the interpreter first searches it in sys.modules, which is the cache of all modules which have been previously imported. If it is not found then it searches in all built-in modules with that name, if it is found then the interpreter runs all of the code and is made available to the file. If the module is not found then it searches for a file with the same name in the list of directories given by the variable sys.path. sys.path is a variable containing a list of paths that contains python libraries, packages, and a directory containing the input script. For example, a module named math is imported then the interpreter searches it in built-in modules, if it is not found then it searches for a file named math.py in list of directories given by sys.path. Python3 `` ``` # Python program importing ``` ``` # math module ``` ``` ​ ``` ``` import math ``` ``` print(math.pi) ``` **Output:** ``` 3.141592653589793 ``` ### **Syntax of import statements :** Users can import both packages and modules. (Note that importing a package essentially imports the package’s \_\_init\_\_.py file as a module.) Users can also import specific objects from a package or module. There are generally two types of import syntax. When you use the first one, you import the resource directly. ``` import gfg ``` gfg can be a package or a module. When a user uses the second syntax, then the user imports the resource from another package or module. ``` from gfg import geek ``` *geek* can be a module, subpackage, or object, such as a class or function. ### **Styling of import statements** [PEP8](https://peps.python.org/pep-0008/#imports), the official style guide for python, has a set of rules for how to formulate the python code to maximize its readability. For writing import statements there are some points to follow: 1. Imports should always be written at the top of the file, just after any module comments and docstrings. 2. Imports should usually be separated by a blank space. 3. Imports should be grouped in the following order. - Standard library imports (Python’s built-in modules) - Related third party imports. - Local application/library-specific imports It is also good to order import statements alphabetically within each import group. Python3 `` ## Absolute vs Relative Imports in Python ## Absolute imports in Python Absolute import involves a full path i.e., from the project's root folder to the desired module. An absolute import state that the resource is to be imported using its full path from the project’s root folder. ### **Syntax and Practical Examples:** Let’s see we have the following directory structure: ![](https://media.geeksforgeeks.org/wp-content/uploads/absolute-import-updated.jpg) Here a directory named project, under which two subdirectories namely pkg1, pkg2. pkg1 has two modules, module1 and module2. pkg2 contains three modules, module3, module4, \_\_init\_\_.py, and one subpackage name subpkg1 which contains module5.py. Let's assume the following: - pkg1 / module1.py contain a function, fun1 - pkg2 / module3.py contain a function, fun2 - pkg2 / subpkg1 / module5.py contain a function fun3 Python3 `` In this example, we are importing the modules by writing the full path from its root folder. ### **Pros and Cons of Absolute imports :** **Pros:** - Absolute imports are very useful because they are clear and straight to the point. - Absolute import is easy to tell exactly from where the imported resource is, just by looking at the statement. - Absolute import remains valid even if the current location of the import statement changes. **Cons:** If the directory structure is very big then usage of absolute imports is not meaningful. In such a case using relative imports works well. ``` from pkg1.subpkg2.subpkg3.subpkg4.module5 import fun6 ``` ## Relative imports in Python Relative import specifies an object or module imported from its current location, that is the location where import statement resides. There two types of relative imports : - **Implicit relative imports** - Implicit relative import have been disapproved in Python(3.x). - **Explicit relative imports -** Explicit relative import have been approved in Python(3.x). **Syntax and Practical Examples :** The syntax of relative import depends on the current location as well as the location of the module or object to be imported. Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on. Let’s see we have the following directory structure: ![](https://media.geeksforgeeks.org/wp-content/uploads/absolute-import-updated.jpg) Let's assume the following: - pkg1 / module1.py contain a function, fun1 - pkg2 / module3.py contain a function, fun2 - pkg2 / subpkg1 / module5.py contain a function fun3 Python3 `` ### **Pros and Cons of Relative imports :** **Pros:** - Working with relative imports is concise and clear. - Based on the current location it reduces the complexity of an import statement. **Cons:** - Relative imports is not so readable as absolute ones. - Using relative imports it is not easy because it is very hard to tell the location of a module. Comment [B](https://www.geeksforgeeks.org/user/bestharadhakrishna/) [bestharadhakrishna](https://www.geeksforgeeks.org/user/bestharadhakrishna/) 5 Article Tags: Article Tags: [Technical Scripter](https://www.geeksforgeeks.org/category/technical-scripter/) [Python](https://www.geeksforgeeks.org/category/programming-language/python/) [Technical Scripter 2018](https://www.geeksforgeeks.org/tag/technical-scripter-2018/) [python-modules](https://www.geeksforgeeks.org/tag/python-modules/) ### Explore [![GeeksforGeeks](https://media.geeksforgeeks.org/auth-dashboard-uploads/gfgFooterLogo.png)](https://www.geeksforgeeks.org/) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Corporate & Communications Address: A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305) ![location](https://media.geeksforgeeks.org/img-practice/Location-1685004904.svg) Registered Address: K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305 [![GFG App on Play Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/googleplay-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app)[![GFG App on App Store](https://media.geeksforgeeks.org/auth-dashboard-uploads/appstore-%281%29.png)](https://geeksforgeeksapp.page.link/gfg-app) - Company - [About Us](https://www.geeksforgeeks.org/about/) - [Legal](https://www.geeksforgeeks.org/legal/) - [Privacy Policy](https://www.geeksforgeeks.org/legal/privacy-policy/) - [Contact Us](https://www.geeksforgeeks.org/about/contact-us/) - [Advertise with us](https://www.geeksforgeeks.org/advertise-with-us/) - [GFG Corporate Solution](https://www.geeksforgeeks.org/gfg-corporate-solution/) - [Campus Training Program](https://www.geeksforgeeks.org/campus-training-program/) - Explore - [POTD](https://www.geeksforgeeks.org/problem-of-the-day) - [Job-A-Thon](https://practice.geeksforgeeks.org/events/rec/job-a-thon/) - [Blogs](https://www.geeksforgeeks.org/category/blogs/?type=recent) - [Nation Skill Up](https://www.geeksforgeeks.org/nation-skill-up/) - Tutorials - [Programming Languages](https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/) - [DSA](https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/) - [Web Technology](https://www.geeksforgeeks.org/web-tech/web-technology/) - [AI, ML & Data Science](https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/) - [DevOps](https://www.geeksforgeeks.org/devops/devops-tutorial/) - [CS Core Subjects](https://www.geeksforgeeks.org/gate/gate-exam-tutorial/) - [Interview Preparation](https://www.geeksforgeeks.org/aptitude/interview-corner/) - [Software and Tools](https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/) - Courses - [ML and Data Science](https://www.geeksforgeeks.org/courses/category/machine-learning-data-science) - [DSA and Placements](https://www.geeksforgeeks.org/courses/category/dsa-placements) - [Web Development](https://www.geeksforgeeks.org/courses/category/development-testing) - [Programming Languages](https://www.geeksforgeeks.org/courses/category/programming-languages) - [DevOps & Cloud](https://www.geeksforgeeks.org/courses/category/cloud-devops) - [GATE](https://www.geeksforgeeks.org/courses/category/gate) - [Trending Technologies](https://www.geeksforgeeks.org/courses/category/trending-technologies/) - Videos - [DSA](https://www.geeksforgeeks.org/videos/category/sde-sheet/) - [Python](https://www.geeksforgeeks.org/videos/category/python/) - [Java](https://www.geeksforgeeks.org/videos/category/java-w6y5f4/) - [C++](https://www.geeksforgeeks.org/videos/category/c/) - [Web Development](https://www.geeksforgeeks.org/videos/category/web-development/) - [Data Science](https://www.geeksforgeeks.org/videos/category/data-science/) - [CS Subjects](https://www.geeksforgeeks.org/videos/category/cs-subjects/) - Preparation Corner - [Interview Corner](https://www.geeksforgeeks.org/interview-prep/interview-corner/) - [Aptitude](https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/) - [Puzzles](https://www.geeksforgeeks.org/aptitude/puzzles/) - [GfG 160](https://www.geeksforgeeks.org/courses/gfg-160-series) - [System Design](https://www.geeksforgeeks.org/system-design/system-design-tutorial/) [@GeeksforGeeks, Sanchhaya Education Private Limited](https://www.geeksforgeeks.org/), [All rights reserved](https://www.geeksforgeeks.org/copyright-information/) ![]()
Readable Markdown
In this article, we are going to see that **absolute and relative imports in Python**. ### **Working of import in Python** Import in Python is similar to \#include header\_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. Import statement consists of the import keyword along with the name of the module. The import statement involves two operations, it searches for a module and it binds the result of the search to a name in the local scope. When a module is imported, Python runs all of the code in the module file and made available to the importer file. When a module is imported the interpreter first searches it in sys.modules, which is the cache of all modules which have been previously imported. If it is not found then it searches in all built-in modules with that name, if it is found then the interpreter runs all of the code and is made available to the file. If the module is not found then it searches for a file with the same name in the list of directories given by the variable sys.path. sys.path is a variable containing a list of paths that contains python libraries, packages, and a directory containing the input script. For example, a module named math is imported then the interpreter searches it in built-in modules, if it is not found then it searches for a file named math.py in list of directories given by sys.path. **Output:** ``` 3.141592653589793 ``` ### **Syntax of import statements :** Users can import both packages and modules. (Note that importing a package essentially imports the package’s \_\_init\_\_.py file as a module.) Users can also import specific objects from a package or module. There are generally two types of import syntax. When you use the first one, you import the resource directly. ``` import gfg ``` gfg can be a package or a module. When a user uses the second syntax, then the user imports the resource from another package or module. ``` from gfg import geek ``` *geek* can be a module, subpackage, or object, such as a class or function. ### **Styling of import statements** [PEP8](https://peps.python.org/pep-0008/#imports), the official style guide for python, has a set of rules for how to formulate the python code to maximize its readability. For writing import statements there are some points to follow: 1. Imports should always be written at the top of the file, just after any module comments and docstrings. 2. Imports should usually be separated by a blank space. 3. Imports should be grouped in the following order. - Standard library imports (Python’s built-in modules) - Related third party imports. - Local application/library-specific imports It is also good to order import statements alphabetically within each import group. `` ## Absolute vs Relative Imports in Python ## Absolute imports in Python Absolute import involves a full path i.e., from the project's root folder to the desired module. An absolute import state that the resource is to be imported using its full path from the project’s root folder. ### **Syntax and Practical Examples:** Let’s see we have the following directory structure: ![](https://media.geeksforgeeks.org/wp-content/uploads/absolute-import-updated.jpg) Here a directory named project, under which two subdirectories namely pkg1, pkg2. pkg1 has two modules, module1 and module2. pkg2 contains three modules, module3, module4, \_\_init\_\_.py, and one subpackage name subpkg1 which contains module5.py. Let's assume the following: - pkg1 / module1.py contain a function, fun1 - pkg2 / module3.py contain a function, fun2 - pkg2 / subpkg1 / module5.py contain a function fun3 `` In this example, we are importing the modules by writing the full path from its root folder. ### **Pros and Cons of Absolute imports :** **Pros:** - Absolute imports are very useful because they are clear and straight to the point. - Absolute import is easy to tell exactly from where the imported resource is, just by looking at the statement. - Absolute import remains valid even if the current location of the import statement changes. **Cons:** If the directory structure is very big then usage of absolute imports is not meaningful. In such a case using relative imports works well. ``` from pkg1.subpkg2.subpkg3.subpkg4.module5 import fun6 ``` ## Relative imports in Python Relative import specifies an object or module imported from its current location, that is the location where import statement resides. There two types of relative imports : - **Implicit relative imports** - Implicit relative import have been disapproved in Python(3.x). - **Explicit relative imports -** Explicit relative import have been approved in Python(3.x). **Syntax and Practical Examples :** The syntax of relative import depends on the current location as well as the location of the module or object to be imported. Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on. Let’s see we have the following directory structure: ![](https://media.geeksforgeeks.org/wp-content/uploads/absolute-import-updated.jpg) Let's assume the following: - pkg1 / module1.py contain a function, fun1 - pkg2 / module3.py contain a function, fun2 - pkg2 / subpkg1 / module5.py contain a function fun3 `` ### **Pros and Cons of Relative imports :** **Pros:** - Working with relative imports is concise and clear. - Based on the current location it reduces the complexity of an import statement. **Cons:** - Relative imports is not so readable as absolute ones. - Using relative imports it is not easy because it is very hard to tell the location of a module.
Shard103 (laksa)
Root Hash12046344915360636903
Unparsed URLorg,geeksforgeeks!www,/python/absolute-and-relative-imports-in-python/ s443