absolufy-imports

0.3.1 · deprecated · verified Tue Apr 14

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

Install

Quickstart

To use `absolufy-imports` via the command line, simply point it to the file(s) you wish to modify. It will perform the conversion in place.

# 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')

view raw JSON →