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
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Relative vs Non Relative Imports

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 PythonData Structures and AlgorithmsCompetitive ProgrammingSystem 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!
 

Live masterclass