absolufy-imports
raw JSON → 0.3.1 verified Tue Apr 14 auth: no python deprecated
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.
pip install absolufy-imports Common errors
error ModuleNotFoundError: No module named 'absolufy_imports' ↓
cause This error occurs when the 'absolufy-imports' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install absolufy-imports'.
error absolufy-imports: command not found ↓
cause This error occurs when the 'absolufy-imports' command-line tool is not available, possibly due to the package not being installed or the installation path not being in the system's PATH.
fix
Ensure the package is installed and the installation path is included in the system's PATH.
error ImportError: cannot import name 'absolufy_imports' from 'absolufy_imports' ↓
cause This error occurs when attempting to import 'absolufy_imports' incorrectly, possibly due to a circular import or incorrect module structure.
fix
Ensure that the import statement is correct and there are no circular imports in the codebase.
error absolufy-imports not converting imports ↓
cause The tool might not be correctly configured to identify your application's source directories, or the files it's processing don't match the expected package structure.
fix
Specify your application directories using the
--application-directories flag (e.g., absolufy-imports . --application-directories .:src), ensuring it includes all relevant package roots. Also, verify that the files are part of a valid Python package (i.e., contain __init__.py). error pre-commit hook absolufy-imports failed ↓
cause This generic pre-commit error indicates that the `absolufy-imports` hook exited with a non-zero status. This can be due to syntax errors in the Python files, an unsupported file structure, or an incorrect `rev` in your `.pre-commit-config.yaml`.
fix
First, fix any underlying Python syntax errors in your staged files. Then, check your
.pre-commit-config.yaml to ensure the rev for absolufy-imports is correct (e.g., v0.3.1) and that the id is properly set. Run pre-commit autoupdate to get the latest working revision. 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. ↓
fix Consider migrating to `reorder-python-imports` for broader import management capabilities.
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. ↓
fix For a project with a `src` directory, use `absolufy-imports --application-directories .:src` or configure your pre-commit hook accordingly.
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. ↓
fix Commit your changes before running `absolufy-imports` or verify changes using a diff tool afterward. Using it as a pre-commit hook helps manage this within your workflow.
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')