absolufy-imports
A Python tool and pre-commit hook designed to automatically convert relative imports to absolute imports within a codebase. While functional, its author has indicated it is superseded by the more comprehensive `reorder-python-imports` library. Its current version is `0.3.1`, last released in January 2022.
Warnings
- deprecated The author of `absolufy-imports` states that it is superseded by `reorder-python-imports` (also by the same author) and recommends using the latter instead. `absolufy-imports` is functional but is not actively maintained for new features.
- gotcha When using `absolufy-imports` (especially with a pre-commit hook), ensure you configure `--application-directories` correctly if your project uses non-standard layouts (e.g., a `src` directory). This helps the tool correctly identify the root of your package for absolute import resolution.
- gotcha This tool modifies files in-place. Always ensure you have appropriate version control or backups before running it across your codebase to prevent unintended changes.
Install
-
pip install absolufy-imports
Quickstart
# Example file: mypackage/myfile.py
# from . import __version__
# from .submodule import some_function
# To convert relative imports in a file:
# Create a dummy file for demonstration
with open('mypackage/myfile.py', 'w') as f:
f.write('from . import __version__\n')
f.write('from .submodule import some_function\n')
import os
import subprocess
# Run absolufy-imports on the file
print('Before conversion:')
with open('mypackage/myfile.py', 'r') as f:
print(f.read())
# Assuming 'mypackage' is at the root for absolute imports
# The tool modifies the file in place
subprocess.run(['absolufy-imports', 'mypackage/myfile.py'], check=True)
print('\nAfter conversion:')
with open('mypackage/myfile.py', 'r') as f:
print(f.read())
# Clean up dummy file and directory
os.remove('mypackage/myfile.py')
os.rmdir('mypackage')