Skip to main content

removestar

Actions Status PyPI version Anaconda-Server Badge PyPI platforms Downloads Conda Downloads Ruff

Tool to automatically replace import * imports in Python files with explicit imports

Installation

Install removestar globally to use it through CLI using pypi -

pip install removestar
pip install "removestar[nb]"  # notebook support

or conda -

conda install -c conda-forge removestar

or add removestar in .pre-commit-config.yaml -

- repo: https://github.com/asmeurer/removestar
  rev: "1.5"
  hooks:
    - id: removestar
      args: [-i] # See docs for all args (-i edits file in-place)
      additional_dependencies: # The libraries or packages your code imports
        - ... # Add . if running inside a library (to install the library itself in the environment)
        - ... # Add nbformat and nbconvert for notebook support

Usage

pre-commit hook

Once removestar is added in .pre-commit-config.yaml, executing the following will always run it (and other pre-commits) before every commit -

pre-commit install

Optionally, the pre-commits (including removestar) can be manually triggered for all the files using -

pre-commit run --all-files

CLI

# scripts

$ removestar file.py # Shows diff but does not edit file.py

$ removestar -i file.py # Edits file.py in-place

$ removestar -i module/ # Modifies every Python file in module/ recursively

# notebooks (make sure nbformat and nbconvert are installed)

$ removestar file.ipynb # Shows diff but does not edit file.ipynb

$ removestar -i file.ipynb # Edits file.ipynb in-place

Why is import * so bad?

Doing from module import * is generally frowned upon in Python. It is considered acceptable when working interactively at a python prompt, or in __init__.py files (removestar skips __init__.py files by default).

Some reasons why import * is bad:

  • It hides which names are actually imported.
  • It is difficult both for human readers and static analyzers such as pyflakes to tell where a given name comes from when import * is used. For example, pyflakes cannot detect unused names (for instance, from typos) in the presence of import *.
  • If there are multiple import * statements, it may not be clear which names come from which module. In some cases, both modules may have a given name, but only the second import will end up being used. This can break people's intuition that the order of imports in a Python file generally does not matter.
  • import * often imports more names than you would expect. Unless the module you import defines __all__ or carefully dels unused names at the module level, import * will import every public (doesn't start with an underscore) name defined in the module file. This can often include things like standard library imports or loop variables defined at the top-level of the file. For imports from modules (from __init__.py), from module import * will include every submodule defined in that module. Using __all__ in modules and __init__.py files is also good practice, as these things are also often confusing even for interactive use where import * is acceptable.
  • In Python 3, import * is syntactically not allowed inside of a function.

Here are some official Python references stating not to use import * in files:

  • The official Python FAQ:

    In general, don’t use from modulename import *. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.

  • PEP 8 (the official Python style guide):

    Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

Unfortunately, if you come across a file in the wild that uses import *, it can be hard to fix it, because you need to find every name in the file that is imported from the *. Removestar makes this easy by finding which names come from * imports and replacing the import lines in the file automatically.

One exception where import * can be bneficial: At the early stages of code development, a definite set of required functions can be difficult to determine. In such a context, wildcard imports could be beneficial by avoiding constantly curating the set of required functions. For example, at the development stage usage of from os.path import * can save time from curating required functions. Post-development, the wld card imports could be determined using removestar. Having said that, because of the multiple said reasons, wildcard imports should be avoided.

Example

Suppose you have a module mymod like

mymod/
  | __init__.py
  | a.py
  | b.py

With

# mymod/a.py
from .b import *


def func(x):
    return x + y
# mymod/b.py
x = 1
y = 2

Then removestar works like:

$ removestar mymod/

--- original/mymod/a.py
+++ fixed/mymod/a.py
@@ -1,5 +1,5 @@
 # mymod/a.py
-from .b import *
+from .b import y

 def func(x):
     return x + y

This does not edit a.py by default. The -i flag causes it to edit a.py in-place:

$ removestar -i mymod/
$ cat mymod/a.py
# mymod/a.py
from .b import y

def func(x):
    return x + y

Command line options

$ removestar --help
usage: removestar [-h] [-i] [--version] [--no-skip-init]
                  [--no-dynamic-importing] [-v] [-q]
                  [--max-line-length MAX_LINE_LENGTH]
                  PATH [PATH ...]

Tool to automatically replace "import *" imports with explicit imports

Requires pyflakes.

Usage:

$ removestar file.py # Shows diff but does not edit file.py

$ removestar -i file.py # Edits file.py in-place

$ removestar -i module/ # Modifies every Python file in module/ recursively

positional arguments:
  PATH                  Files or directories to fix

optional arguments:
  -h, --help            show this help message and exit
  -i, --in-place        Edit the files in-place. (default: False)
  --version             Show removestar version number and exit.
  --no-skip-init        Don't skip __init__.py files (they are skipped by
                        default) (default: True)
  --no-dynamic-importing
                        Don't dynamically import modules to determine the list
                        of names. This is required for star imports from
                        external modules and modules in the standard library.
                        (default: True)
  -v, --verbose         Print information about every imported name that is
                        replaced. (default: False)
  -q, --quiet           Don't print any warning messages. (default: False)
  --max-line-length MAX_LINE_LENGTH
                        The maximum line length for replaced imports before
                        they are wrapped. Set to 0 to disable line wrapping.
                        (default: 100)

Whitelisting star imports

removestar does not replace star import lines that are marked with Flake8 noqa comments that permit star imports (F401 or F403).

For example, the star imports in this module would be kept:

from os import *  # noqa: F401
from .b import *  # noqa


def func(x):
    return x + y

Current limitations

  • Assumes only names in the current file are used by star imports (e.g., it won't work to replace star imports in __init__.py).

  • For files within the same module, removestar determines missing imported names statically. For external library imports, including imports of standard library modules, it dynamically imports the module to determine the names. This can be disabled with the --no-dynamic-importing flag.

Contributing

See the issue tracker. Pull requests are welcome.

Changelog

See the CHANGELOG file.

License

MIT

Metadata

Release files for removestar 1.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for removestar 1.5.3
File Size Uploaded Provenance
removestar-1.5.3.tar.gz 14.0 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for removestar 1.5.3
File Interpreter ABI Platform
removestar-1.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 30.2 kB

Release files / removestar-1.5.3.tar.gz

Download URL removestar-1.5.3.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c3a717fa5906a3a48ed410635d3375f3884da0c99e76ac83cbd613617903c038
BLAKE2b-256 checksum
How to use checksums
d9a84d8629e6e1296682038aacac71b902f898206427c188c9c611e8e81bb5ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Release files / removestar-1.5.3-py3-none-any.whl

Download URL removestar-1.5.3-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1404d0796e188ce4e1253d3ed86af616de31eb59da7a44a70bfce7d1a07e8056
BLAKE2b-256 checksum
How to use checksums
0ce58f40d61b243f2d60b115209a2b98f548693f5deadb547f1be3e66e7c9941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Security for removestar 1.5.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for removestar-1.5.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page