ℹ️ 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.7 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.naukri.com/code360/library/relative-vs-non-relative-imports |
| Last Crawled | 2026-03-19 14:52:20 (20 days ago) |
| First Indexed | 2024-03-30 13:29:33 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Relative vs Non Relative Imports - Naukri Code 360 |
| Meta Description | In this blog, we will learn about Relative vs Non Relative Imports. We will also explore the advantages and disadvantages. |
| Meta Canonical | null |
| Boilerpipe Text | Introduction
If you've ever worked on a Python project, you know that managing imports can sometimes feel like you're navigating a labyrinth. You might wonder, "Should I use a relative import here or go for a non-relative one?" Your decision can profoundly impact your code's readability, maintainability, and portability.
This article aims to serve as your compass in the bewildering world of Python imports by dissecting relative and non-relative imports in an easily digestible manner.
Setting the Stage: What Exactly Are Imports?
In Python, 'import' is essentially a keyword that allows you to bring external code into your current Python script. This external code could be a built-in Python module, a third-party library, or even another Python file you've written. The idea is to reuse code efficiently to solve new problems without reinventing the wheel.
The Close Relatives: Relative Imports
Relative imports are like your close family; you know exactly where they live in relation to your home. In technical terms, you use relative imports when the code you want to bring in is part of the same directory structure.
The Syntax
The syntax often involves one or more dots (.) before the module name, indicating its position relative to the current file.
For instance, consider a directory structure like so:
my_project/
├── main.py
└── utilities/
├── __init__.py
└── math_operations.py
In main.py, you could import math_operations as follows:
from .utilities import math_operations
The Good and the Bad
Advantages
Project Portability: Since the path is relative, you can easily move the entire project folder to a new location without breaking any imports.
Easy Identification: Relative imports make it clear that the imported module is part of the same project, aiding readability.
Disadvantages
Limited Flexibility: You're restricted to importing files that exist within the same project.
Potential Confusion: If overused, especially in large projects, relative imports can make it difficult to track file locations.
The Distant Cousins: Non-Relative (Absolute) Imports
Non-relative imports don't rely on a file's location within a project. They're like distant relatives; you need the full address to reach them.
The Syntax
Unlike relative imports, the syntax here doesn't involve dots. You either specify the full path to the module or just use its name if it's globally accessible.
For example, importing Python's math library would simply be:
import math
The Good and the Bad
Advantages
Clarity: You immediately know you're working with an external library, which can be useful for future debugging or code reviews.
Greater Reach: You can import modules or libraries from anywhere on your system, not just your current directory.
Disadvantages
Dependency Management: You'll need to ensure that all external libraries are installed wherever your code runs.
Verbose: Sometimes the paths can be long, making your code harder to read.
Decision Time: When to Use Which?
Choosing between relative and non-relative imports isn't arbitrary; it's a matter of project requirements and clarity.
Project-Specific Code: If the code you're importing is a part of the same project, relative imports make more sense. They enhance portability and readability.
External Libraries or Modules: For these, non-relative imports are the way to go. They clarify that the code is not part of the project and may need to be installed separately.
Frequently Asked Questions
Is it a good practice to mix relative and non-relative imports?
While Python allows it, mixing the two can create confusion. Stick to one type for consistency and better code structure.
Do relative and non-relative imports affect performance?
The difference in performance is negligible. The choice is primarily about code organization and readability.
How can I resolve the 'Attempted relative import in non-package' error?
This error often occurs when trying to use a relative import in a Python script that's executed as the main program. To fix it, ensure your script is part of a Python package or switch to a non-relative import.
Conclusion
The world of Python imports doesn't have to be a maze. With a clear understanding of what relative and non-relative imports are, their pros and cons, and when to use each, you can write Python code that is not only functional but also clean, organized, and easy to manage. It's all about making the right choices for the right situations. Happy coding!
For more information, refer to our
Guided Path
on Coding Ninjas Studio to upskill yourself in
Python
,
Data Structures and Algorithms
,
Competitive Programming
,
System Design
,
and many more!
Head over to our practice platform,
CodeStudio
, to practice top problems, attempt
mock tests
,
read
interview experiences and interview
bundles, follow
guided paths
for placement preparations, and much more!
|
| Markdown |  
[]()
- [Learn]()
- [Contests & Events]()
- [Interview prep]()
- [Practice]()
- [Resources]()
[]()

Become AI ready
Login

[Naukri Code 360](https://www.naukri.com/code360)

[Library](https://www.naukri.com/code360/library)

Relative vs Non Relative Imports
Browse Categories
Table of contents
1\.
Introduction
2\.
Setting the Stage: What Exactly Are Imports?
2\.1.
The Close Relatives: Relative Imports
2\.2.
The Syntax
3\.
The Good and the Bad
3\.1.
Advantages
3\.2.
Disadvantages
4\.
The Distant Cousins: Non-Relative (Absolute) Imports
4\.1.
The Syntax
5\.
The Good and the Bad
5\.1.
Advantages
5\.2.
Disadvantages
6\.
Decision Time: When to Use Which?
7\.
Frequently Asked Questions
7\.1.
Is it a good practice to mix relative and non-relative imports?
7\.2.
Do relative and non-relative imports affect performance?
7\.3.
How can I resolve the 'Attempted relative import in non-package' error?
8\.
Conclusion
Last Updated: Feb 5, 2025
Easy
# Relative vs Non Relative Imports
[Author Lekhika](https://www.naukri.com/code360/profile/25be33fc-d7d2-4f9a-9a65-a59b72ce93f6)
 Share
0 upvote

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Yes
No
## Introduction
If you've ever worked on a Python project, you know that managing imports can sometimes feel like you're navigating a labyrinth. You might wonder, "Should I use a relative import here or go for a non-relative one?" Your decision can profoundly impact your code's readability, maintainability, and portability.

This article aims to serve as your compass in the bewildering world of Python imports by dissecting relative and non-relative imports in an easily digestible manner.
## Setting the Stage: What Exactly Are Imports?
In Python, 'import' is essentially a keyword that allows you to bring external code into your current Python script. This external code could be a built-in Python module, a third-party library, or even another Python file you've written. The idea is to reuse code efficiently to solve new problems without reinventing the wheel.
### The Close Relatives: Relative Imports
Relative imports are like your close family; you know exactly where they live in relation to your home. In technical terms, you use relative imports when the code you want to bring in is part of the same directory structure.
### The Syntax
The syntax often involves one or more dots (.) before the module name, indicating its position relative to the current file.
For instance, consider a directory structure like so:
```
my_project/
├── main.py
└── utilities/
├── __init__.py
└── math_operations.py
```
In main.py, you could import math\_operations as follows:
```
from .utilities import math_operations
```
## The Good and the Bad
### Advantages
Project Portability: Since the path is relative, you can easily move the entire project folder to a new location without breaking any imports.
Easy Identification: Relative imports make it clear that the imported module is part of the same project, aiding readability.
### Disadvantages
Limited Flexibility: You're restricted to importing files that exist within the same project.
Potential Confusion: If overused, especially in large projects, relative imports can make it difficult to track file locations.
## The Distant Cousins: Non-Relative (Absolute) Imports
Non-relative imports don't rely on a file's location within a project. They're like distant relatives; you need the full address to reach them.
### The Syntax
Unlike relative imports, the syntax here doesn't involve dots. You either specify the full path to the module or just use its name if it's globally accessible.
For example, importing Python's math library would simply be:
```
import math
```
## The Good and the Bad
### Advantages
Clarity: You immediately know you're working with an external library, which can be useful for future debugging or code reviews.
Greater Reach: You can import modules or libraries from anywhere on your system, not just your current directory.
### Disadvantages
Dependency Management: You'll need to ensure that all external libraries are installed wherever your code runs.
Verbose: Sometimes the paths can be long, making your code harder to read.
## Decision Time: When to Use Which?
Choosing between relative and non-relative imports isn't arbitrary; it's a matter of project requirements and clarity.
Project-Specific Code: If the code you're importing is a part of the same project, relative imports make more sense. They enhance portability and readability.
External Libraries or Modules: For these, non-relative imports are the way to go. They clarify that the code is not part of the project and may need to be installed separately.
## Frequently Asked Questions
### Is it a good practice to mix relative and non-relative imports?
While Python allows it, mixing the two can create confusion. Stick to one type for consistency and better code structure.
### Do relative and non-relative imports affect performance?
The difference in performance is negligible. The choice is primarily about code organization and readability.
### How can I resolve the 'Attempted relative import in non-package' error?
This error often occurs when trying to use a relative import in a Python script that's executed as the main program. To fix it, ensure your script is part of a Python package or switch to a non-relative import.
## Conclusion
The world of Python imports doesn't have to be a maze. With a clear understanding of what relative and non-relative imports are, their pros and cons, and when to use each, you can write Python code that is not only functional but also clean, organized, and easy to manage. It's all about making the right choices for the right situations. Happy coding\!
For more information, refer to our [Guided Path](https://www.naukri.com/code360/guided-paths) on Coding Ninjas Studio to upskill yourself in [Python](https://www.naukri.com/code360/guided-paths/basics-of-python), [Data Structures and Algorithms](https://www.naukri.com/code360/guided-paths/data-structures-algorithms), [Competitive Programming](https://www.naukri.com/code360/guided-paths/competitive-programming), [System Design](https://www.naukri.com/code360/guided-paths/system-design), and many more\!
Head over to our practice platform, [CodeStudio](https://www.naukri.com/code360), to practice top problems, attempt [mock tests](https://www.naukri.com/code360/test-series), read [interview experiences and interview](https://www.naukri.com/code360/interview-bundle) bundles, follow [guided paths](https://www.naukri.com/code360/guided-paths) for placement preparations, and much more\!
Live masterclass

Zomato Data Analysis Case Study: Ace 25L+ Roles in FoodTech
by Abhishek Soni
16 Mar, 2026
01:30 PM
 39+ registered
Register now

Data Analysis for 20L+ CTC@Flipkart: End-Season Sales dataset
by Sumit Shukla
15 Mar, 2026
06:30 AM
 267+ registered
Register now

Beginner to GenAI Engineer Roadmap for 30L+ CTC at Amazon
by Shantanu Shubham
15 Mar, 2026
08:30 AM
 55+ registered
Register now

Multi-Agent AI Systems: Live Workshop for 25L+ CTC at Google
by Saurav Prateek
16 Mar, 2026
03:00 PM
 8+ registered
Register now

Zomato Data Analysis Case Study: Ace 25L+ Roles in FoodTech
by Abhishek Soni
16 Mar, 2026
01:30 PM
 39+ registered
Register now

Data Analysis for 20L+ CTC@Flipkart: End-Season Sales dataset
by Sumit Shukla
15 Mar, 2026
06:30 AM
 267+ registered
Register now
[View more events](https://www.naukri.com/code360/events)
Library:
[Java](https://www.naukri.com/code360/library/introduction-to-java)
[Python](https://www.naukri.com/code360/library/python-introduction)
[C Programming Language](https://www.naukri.com/code360/library/introduction-to-c-programming)
[C++ Programming Language](https://www.naukri.com/code360/library/introduction-to-cpp)
[Cloud Computing](https://www.naukri.com/code360/library/introduction-to-cloud-computing)
[Node JS](https://www.naukri.com/code360/library/introduction-to-node-js)
[Machine Learning](https://www.naukri.com/code360/library/what-is-machine-learning)
[Deep Learning](https://www.naukri.com/code360/library/introduction-to-deep-learning)
[Big Data](https://www.naukri.com/code360/library/what-is-big-data)
[Operating System](https://www.naukri.com/code360/library/introduction-to-operating-system)
[Go Language](https://www.naukri.com/code360/library/introduction-to-go)
[C\#](https://www.naukri.com/code360/library/introduction-to-csharp)
[Ruby](https://www.naukri.com/code360/library/try-ruby)
[Amazon Web Services](https://www.naukri.com/code360/library/introduction-to-aws)
[Microsoft Azure](https://www.naukri.com/code360/library/introduction-to-microsoft-azure)
[Google Cloud Platform](https://www.naukri.com/code360/library/introduction-to-google-cloud-platform)
[Data Warehousing](https://www.naukri.com/code360/library/what-is-data-warehousing)
[Internet of Things](https://www.naukri.com/code360/library/what-is-iot)
Get the tech career you deserve faster with Coding Ninjas courses
User rating 4.7/5
1:1 doubt support
95% placement record
Request a callback
[](https://www.codingninjas.com/)
[About us](https://www.codingninjas.com/about)
[Success stories](https://www.codingninjas.com/review)
[Privacy policy](https://www.codingninjas.com/policy/privacy.pdf)
[Terms & conditions](https://www.codingninjas.com/policy/tnc.pdf)
Our courses
[](https://www.codingninjas.com/programs/job-bootcamp-web-development)
Follow us on
[](https://www.instagram.com/coding.ninjas/)
[](https://twitter.com/CodingNinjasOff)
[](https://www.facebook.com/codingninjas)
[](https://www.youtube.com/c/CodingNinjasIndia)
[](https://www.linkedin.com/company/coding-ninjas-india/)
Contact us
[1800-123-3598](tel:1800-123-3598)
[code360@codingninjas.com](mailto:code360@codingninjas.com)
[](https://www.naukri.com/)
[Privacy policy](https://www.naukri.com/privacypolicy)
[Terms & conditions](https://www.naukri.com/termsconditions)
Download the naukri app
[](https://play.google.com/store/apps/details?id=naukriApp.appModules.login&pcampaignid=web_share)[](https://apps.apple.com/in/app/naukri-com-job-search/id482877505) |
| Readable Markdown | ## Introduction
If you've ever worked on a Python project, you know that managing imports can sometimes feel like you're navigating a labyrinth. You might wonder, "Should I use a relative import here or go for a non-relative one?" Your decision can profoundly impact your code's readability, maintainability, and portability.

This article aims to serve as your compass in the bewildering world of Python imports by dissecting relative and non-relative imports in an easily digestible manner.
## Setting the Stage: What Exactly Are Imports?
In Python, 'import' is essentially a keyword that allows you to bring external code into your current Python script. This external code could be a built-in Python module, a third-party library, or even another Python file you've written. The idea is to reuse code efficiently to solve new problems without reinventing the wheel.
### The Close Relatives: Relative Imports
Relative imports are like your close family; you know exactly where they live in relation to your home. In technical terms, you use relative imports when the code you want to bring in is part of the same directory structure.
### The Syntax
The syntax often involves one or more dots (.) before the module name, indicating its position relative to the current file.
For instance, consider a directory structure like so:
```
my_project/
├── main.py
└── utilities/
├── __init__.py
└── math_operations.py
```
In main.py, you could import math\_operations as follows:
```
from .utilities import math_operations
```
## The Good and the Bad
### Advantages
Project Portability: Since the path is relative, you can easily move the entire project folder to a new location without breaking any imports.
Easy Identification: Relative imports make it clear that the imported module is part of the same project, aiding readability.
### Disadvantages
Limited Flexibility: You're restricted to importing files that exist within the same project.
Potential Confusion: If overused, especially in large projects, relative imports can make it difficult to track file locations.
## The Distant Cousins: Non-Relative (Absolute) Imports
Non-relative imports don't rely on a file's location within a project. They're like distant relatives; you need the full address to reach them.
### The Syntax
Unlike relative imports, the syntax here doesn't involve dots. You either specify the full path to the module or just use its name if it's globally accessible.
For example, importing Python's math library would simply be:
```
import math
```
## The Good and the Bad
### Advantages
Clarity: You immediately know you're working with an external library, which can be useful for future debugging or code reviews.
Greater Reach: You can import modules or libraries from anywhere on your system, not just your current directory.
### Disadvantages
Dependency Management: You'll need to ensure that all external libraries are installed wherever your code runs.
Verbose: Sometimes the paths can be long, making your code harder to read.
## Decision Time: When to Use Which?
Choosing between relative and non-relative imports isn't arbitrary; it's a matter of project requirements and clarity.
Project-Specific Code: If the code you're importing is a part of the same project, relative imports make more sense. They enhance portability and readability.
External Libraries or Modules: For these, non-relative imports are the way to go. They clarify that the code is not part of the project and may need to be installed separately.
## Frequently Asked Questions
### Is it a good practice to mix relative and non-relative imports?
While Python allows it, mixing the two can create confusion. Stick to one type for consistency and better code structure.
### Do relative and non-relative imports affect performance?
The difference in performance is negligible. The choice is primarily about code organization and readability.
### How can I resolve the 'Attempted relative import in non-package' error?
This error often occurs when trying to use a relative import in a Python script that's executed as the main program. To fix it, ensure your script is part of a Python package or switch to a non-relative import.
## Conclusion
The world of Python imports doesn't have to be a maze. With a clear understanding of what relative and non-relative imports are, their pros and cons, and when to use each, you can write Python code that is not only functional but also clean, organized, and easy to manage. It's all about making the right choices for the right situations. Happy coding\!
For more information, refer to our [Guided Path](https://www.naukri.com/code360/guided-paths) on Coding Ninjas Studio to upskill yourself in [Python](https://www.naukri.com/code360/guided-paths/basics-of-python), [Data Structures and Algorithms](https://www.naukri.com/code360/guided-paths/data-structures-algorithms), [Competitive Programming](https://www.naukri.com/code360/guided-paths/competitive-programming), [System Design](https://www.naukri.com/code360/guided-paths/system-design), and many more\!
Head over to our practice platform, [CodeStudio](https://www.naukri.com/code360), to practice top problems, attempt [mock tests](https://www.naukri.com/code360/test-series), read [interview experiences and interview](https://www.naukri.com/code360/interview-bundle) bundles, follow [guided paths](https://www.naukri.com/code360/guided-paths) for placement preparations, and much more\! |
| Shard | 162 (laksa) |
| Root Hash | 12824966294148467362 |
| Unparsed URL | com,naukri!www,/code360/library/relative-vs-non-relative-imports s443 |