Convert relative imports in a Python package to absolute imports.
Project description
relative2absolute
relative2absolute is a Python tool that converts relative import statements into absolute ones based on a specified root directory. It is especially useful for refactoring Python projects to follow absolute import best practices.
Features
- Converts relative imports (e.g.,
from ..core import database) to absolute imports (e.g.,from package.app.core import database) - Allows specifying the root path where absolute imports should start
- Supports nested directories
- Will not affect any existing absolute imports
- Command-line interface with pip installation support
Description
This tool converts relative imports to absolute imports based on a root package specified via the --root argument. It recursively parses every module and subpackage within the root package and modifies any relative import it finds to an absolute import that begins from the root package.
If a module contains a relative import with a level (i.e., number of .) that exceeds the number of package levels between the root and that module, an error will be raised.
Installation
Install the tool using pip:
pip install relative2absolute
Usage
Run the tool from the command line by specifying the path to the directory or file to convert, along with the --root argument:
relative2absolute --root path/to/root/package
The --root argument determines the starting point for absolute imports. The imports will start from the last element in the path you provide for the --root argument. For example, if the root path is:
path/to/root/package
Then this relative import:
from ..core import database
Will be converted to:
from package.app.core import database
Example
Example directory:
example/
│ ├── core/
│ │ ├── database.py
│ │ └── dependencies.py # conains: from ..utils import send_email
│ ├── main.py # contains: from .core.database import get_db
│ └── utils.py
Command:
relative2absolute --root path/to/example
Result:
The import in main.py will be updated to:
from example.core.database import get_db
The import in dependencies.py will be updated to:
from example.utils import upload_file
Requirements
- Python 3.6+
- No third-party dependencies
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you'd like to change.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file relative2absolute-0.1.3.tar.gz.
File metadata
- Download URL: relative2absolute-0.1.3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b63e2a12e66c682e3bb7c0812304665a92d30d3d0c62a17c3c78e10e64da138
|
|
| MD5 |
a4011a413f5088c1ca865e0080a77bc0
|
|
| BLAKE2b-256 |
6b90a601c6d9fd45ff9f6e33846742579a9e3a5ebec06cae6177a77bf2885c6c
|
File details
Details for the file relative2absolute-0.1.3-py3-none-any.whl.
File metadata
- Download URL: relative2absolute-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d514cf6a727d6b59c515802479f9628638750b6100380f2af8114d2627343eb
|
|
| MD5 |
f45093481410c45f44c5c04ba2460164
|
|
| BLAKE2b-256 |
7bbc0bec2f917587205c3d7c6356424159612ec4c6ef0015ccec8d95c890dc7b
|