{"id":13501410,"url":"https://github.com/asottile/reorder-python-imports","last_synced_at":"2025-12-17T05:59:49.136Z","repository":{"id":25268357,"uuid":"28693782","full_name":"asottile/reorder-python-imports","owner":"asottile","description":"Rewrites source to reorder python imports","archived":false,"fork":false,"pushed_at":"2025-11-25T15:49:41.000Z","size":552,"stargazers_count":772,"open_issues_count":1,"forks_count":58,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-11-28T21:34:38.178Z","etag":null,"topics":["linter","pre-commit","python","refactoring"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asottile.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"asottile"}},"created_at":"2015-01-01T19:30:10.000Z","updated_at":"2025-11-25T15:49:44.000Z","dependencies_parsed_at":"2023-01-14T02:26:24.572Z","dependency_job_id":"8f723249-f908-4745-9d41-258352143342","html_url":"https://github.com/asottile/reorder-python-imports","commit_stats":{"total_commits":389,"total_committers":18,"mean_commits":21.61111111111111,"dds":"0.41645244215938304","last_synced_commit":"9a3503852aac11aa95fdf5f0f9c69ce91df640e8"},"previous_names":["asottile/reorder_python_imports"],"tags_count":65,"template":false,"template_full_name":null,"purl":"pkg:github/asottile/reorder-python-imports","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Freorder-python-imports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Freorder-python-imports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Freorder-python-imports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Freorder-python-imports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asottile","download_url":"https://codeload.github.com/asottile/reorder-python-imports/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Freorder-python-imports/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27546866,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-06T02:00:06.463Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linter","pre-commit","python","refactoring"],"created_at":"2024-07-31T22:01:36.496Z","updated_at":"2025-12-17T05:59:49.130Z","avatar_url":"https://github.com/asottile.png","language":"Python","readme":"[![build status](https://github.com/asottile/reorder-python-imports/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/reorder-python-imports/actions/workflows/main.yml)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/reorder-python-imports/main.svg)](https://results.pre-commit.ci/latest/github/asottile/reorder-python-imports/main)\n\nreorder-python-imports\n======================\n\nTool for automatically reordering python imports.  Similar to `isort` but\nuses static analysis more.\n\n\n## Installation\n\n```bash\npip install reorder-python-imports\n```\n\n\n## Console scripts\n\nConsult `reorder-python-imports --help` for the full set of options.\n\n`reorder-python-imports` takes filenames as positional arguments\n\nCommon options:\n\n- `--py##-plus`: [see below](#removing-obsolete-__future__-imports).\n- `--add-import` / `--remove-import`: [see below](#adding--removing-imports).\n- `--replace-import`: [see below](#replacing-imports).\n- `--application-directories`: by default, `reorder-python-imports` assumes\n  your project is rooted at `.`.  If this isn't true, tell it where your\n  import roots live.  For example, when using the popular `./src` layout you'd\n  use `--application-directories=.:src` (note: multiple paths are separated\n  using a `:`).\n- `--unclassifiable-application-module`: (may be specified multiple times)\n  modules names that are considered application modules.  this setting is\n  intended to be used for things like C modules which may not always appear on\n  the filesystem.\n\n## As a pre-commit hook\n\nSee [pre-commit](https://github.com/pre-commit/pre-commit) for instructions\n\nSample `.pre-commit-config.yaml`\n\n```yaml\n-   repo: https://github.com/asottile/reorder-python-imports\n    rev: v3.16.0\n    hooks:\n    -   id: reorder-python-imports\n```\n\n## What does it do?\n\n### Separates imports into three sections\n\n```python\nimport sys\nimport pyramid\nimport reorder_python_imports\n```\n\nbecomes (stdlib, third party, first party)\n\n```python\nimport sys\n\nimport pyramid\n\nimport reorder_python_imports\n```\n\n### `import` imports before `from` imports\n\n```python\nfrom os import path\nimport sys\n```\n\nbecomes\n\n```python\nimport sys\nfrom os import path\n```\n\n### Splits `from` imports\n\n```python\nfrom os.path import abspath, exists\n```\n\nbecomes\n\n```python\nfrom os.path import abspath\nfrom os.path import exists\n```\n\n### Removes duplicate imports\n\n```python\nimport os\nimport os.path\nimport sys\nimport sys\n```\n\nbecomes\n\n```python\nimport os.path\nimport sys\n```\n\n## Using `# noreorder`\n\nLines containing and after lines which contain a `# noreorder` comment will\nbe ignored.  Additionally any imports that appear after non-whitespace\nnon-comment lines will be ignored.\n\nFor instance, these will not be changed:\n\n```python\nimport sys\n\ntry:  # not import, not whitespace\n    import foo\nexcept ImportError:\n    pass\n```\n\n```python\nfrom typing import TYPE_CHECKING\n\nif TYPE_CHECKING:\n    # all these imports are after non-whitspace non-comment lines\n    # and will be ignored (i.e. they will remain out of order)\n    from collections.abc import Sequence\n    from collections.abc import Callable\n```\n\n```python\nimport sys\n\nimport reorder_python_imports\n\nimport matplotlib  # noreorder\nmatplotlib.use('Agg')\nimport matplotlib.pyplot as plt\n```\n\n```python\n# noreorder\nimport sys\nimport pyramid\nimport reorder_python_imports\n```\n\n## why this style?\n\nThe style chosen by `reorder-python-imports` has a single aim: reduce merge\nconflicts.\n\nBy having a single import per line, multiple contributors can\nadd / remove imports from a single module without resulting in a conflict.\n\nConsider the following example which causes a merge conflict:\n\n```diff\n# developer 1\n-from typing import Dict, List\n+from typing import Any, Dict, List\n```\n\n```diff\n# developer 2\n-from typing import Dict, List\n+from typing import Dict, List, Tuple\n```\n\nno conflict with the style enforced by `reorder-python-imports`:\n\n```diff\n+from typing import Any\n from typing import Dict\n from typing import List\n+from typing import Tuple\n```\n\n## Adding / Removing Imports\n\nLet's say I want to enforce `absolute_import` across my codebase.  I can use:\n`--add-import 'from __future__ import absolute_import'`.\n\n```console\n$ cat test.py\nprint('Hello world')\n$ reorder-python-imports --add-import 'from __future__ import absolute_import' test.py\nReordering imports in test.py\n$ cat test.py\nfrom __future__ import absolute_import\nprint('Hello world')\n```\n\nLet's say I no longer care about supporting Python 2.5, I can remove\n`from __future__ import with_statement` with\n`--remove-import 'from __future__ import with_statement'`\n\n```console\n$ cat test.py\nfrom __future__ import with_statement\nwith open('foo.txt', 'w') as foo_f:\n    foo_f.write('hello world')\n$ reorder-python-imports --remove-import 'from __future__ import with_statement' test.py\nReordering imports in test.py\n$ cat test.py\nwith open('foo.txt', 'w') as foo_f:\n    foo_f.write('hello world')\n```\n\n## Replacing imports\n\nImports can be replaced with others automatically (if they provide the same\nnames).  This can be useful for factoring out compatibility libraries such\nas `six` (see below for automated `six` rewriting).\n\nThis rewrite avoids `NameError`s as such it only occurs when:\n\n- the imported symbol is the same before and after\n- the import is a `from` import\n\nThe argument is specified as `orig.mod=new.mod` or with an optional\nchecked attribute `orig.mod=new.mod:attr`.  The checked attribute is useful\nfor renaming some imports from a module instead of a full module.\n\nFor example:\n\n```bash\n# full module move\n--replace-import six.moves.queue=queue\n# specific attribute move\n--replace-import six.moves=io:StringIO\n```\n\n## Removing obsolete `__future__` imports\n\nThe cli provides a few options to help \"burn the bridges\" with old python\nversions by removing `__future__` imports automatically.  Each option implies\nall older versions.\n\n- `--py22-plus`: `nested_scopes`\n- `--py23-plus`: `generators`\n- `--py26-plus`: `with_statement`\n- `--py3-plus`: `division`, `absolute_import`, `print_function`,\n  `unicode_literals`\n- `--py37-plus`: `generator_stop`\n\n## Removing / rewriting obsolete `six` imports\n\nWith `--py3-plus`, `reorder-python-imports` will also remove / rewrite imports\nfrom `six`.  Rewrites follow the same rules as\n[replacing imports](#replacing-imports) above.\n\nFor example:\n\n```diff\n+import queue\n+from io import StringIO\n+from urllib.parse import quote_plus\n+\n import six.moves.urllib.parse\n-from six.moves import queue\n-from six.moves import range\n-from six.moves import StringIO\n-from six.moves.urllib.parse import quote_plus\n```\n\n## Rewriting mock imports\n\nWith `--py3-plus`, `reorder-python-imports` will also rewrite various `mock` imports:\n\n```diff\n-from mock import patch\n+from unittest.mock import patch\n```\n\n## Rewriting `mypy_extensions` and `typing_extension` imports\n\nWith `--py36-plus` and higher, `reorder-python-imports` will also rewrite\n`mypy_extensions` and `typing_extensions` imports ported to `typing`.\n\n```diff\n-from mypy_extensions import TypedDict\n+from typing import TypedDict\n```\n\n## Rewriting pep 585 typing imports\n\nWith `--py39-plus` and higher, `reorder-python-imports` will replace imports\nwhich were moved out of the typing module in [pep 585].\n\n```diff\n-from typing import Sequence\n+from collections.abc import Sequence\n```\n\n[pep 585]: https://www.python.org/dev/peps/pep-0585/\n","funding_links":["https://github.com/sponsors/asottile"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasottile%2Freorder-python-imports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasottile%2Freorder-python-imports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasottile%2Freorder-python-imports/lists"}