Skip to main content

Automatically convert python absolute imports to relative.

Project description



Automatically convert python absolute imports to relative.

Inspired and adapted from MarcoGorelli/absolufy-imports.


$ pip install --upgrade relativize-imports

Usage as a pre-commit hook

See pre-commit for instructions

Sample .pre-commit-config.yaml:

-   repo:
    rev: v0.0.6
    -   id: relativize-imports

Or with args:

-   repo:
    rev: v0.0.6
    -   id: relativize-imports
          - svc/src
          - automation/tests/source


relativize-imports [path/to/dir/or/ ...]

Provide zero or more paths to relativize.

Files are assumed to be relative to the cwd. Directories are assumed as relativization root and the tool will format all python files under them, in any depth.

If no args are provided, the tool will run in the cwd and address it as the relativization root.


The tool will read any config set in tox.ini in a root directory.

If you want to exclude any files from relativization (i.e entrypoints), you can add them to config:

ignore = src/,test

Command-line example

$ relativize-imports
$ relativize-imports src
$ relativize-imports mypackage/
- from mypackage import __version__
+ from . import __version__

Build and Publish

rm -rf dist
python bdist_wheel 
twine upload dist/*

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

relativize_imports-0.0.6-py2.py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page