PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.45k stars 574 forks source link

Feature Request: Automatic Conversion of Relative Imports to Absolute Imports #2253

Open nh916 opened 5 months ago

nh916 commented 5 months ago

Description

I would like to request a new feature for isort that automatically converts relative imports to absolute imports within a Python project. This feature would enhance code readability and maintainability, especially in large projects with complex directory structures.

Problem Statement

In large and complex Python projects, relative imports can make the codebase harder to navigate and understand, especially for newcomers. Enforcing absolute imports contributes to cleaner and more understandable code. Currently, developers have to manually adjust imports or rely on code review practices to enforce the use of absolute imports, which can be time-consuming and error-prone.

Proposed Solution

I propose adding an option to isort that, when enabled, converts all relative imports in a project to absolute imports based on the project's root directory. This could be implemented as a command-line flag (e.g., --absolute-imports) that can be used in conjunction with the existing isort command to also maybe check on the CI level.

Use Case

This feature would be particularly useful in projects that prioritize code readability and maintainability, making it easier to enforce a consistent import style across the entire codebase. It would also be beneficial for projects looking to migrate from relative to absolute imports as part of refactoring efforts.

Thank you for considering this feature request. I believe it would make a valuable addition to isort and greatly benefit the Python community.