Skip to main content

A tool to automatically replace 'import *' imports with explicit imports in files

Project description

removestar

Build Status

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

Requires pyflakes.

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.

See the issue tracker. Pull requests are welcome.

Installation

pip install removestar

or if you use conda

conda install -c conda-forge removestar

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

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.

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)

Changelog

See the CHANGELOG file.

License

MIT

Project details


Download files

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

Source Distribution

removestar-1.2.4.tar.gz (34.8 kB view details)

Uploaded Source

Built Distribution

removestar-1.2.4-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file removestar-1.2.4.tar.gz.

File metadata

  • Download URL: removestar-1.2.4.tar.gz
  • Upload date:
  • Size: 34.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.6.0.post20210108 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.6

File hashes

Hashes for removestar-1.2.4.tar.gz
Algorithm Hash digest
SHA256 d6ae5db4aaa2197c623a5a9e4dd9ebbf08f64cae5f6c6217e63940dc5ae40287
MD5 a7455c07f7035e6b4a712ac6c0743a11
BLAKE2b-256 5d4f6620afd7e39aca131466dacaaa47d39e4e99cbfe754d31bdf3c651a0949f

See more details on using hashes here.

File details

Details for the file removestar-1.2.4-py3-none-any.whl.

File metadata

  • Download URL: removestar-1.2.4-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.6.0.post20210108 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.6

File hashes

Hashes for removestar-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 18f263cd551dec42099f8c03ba2e856349f63d8ec4b9abc5cf84954a8881f08f
MD5 48029a8354b15915ba17ce666d640406
BLAKE2b-256 dca768acf4743e7d44a40a36054b8927330e4e49973930eb7ebbe45fd049383d

See more details on using hashes here.

Supported by

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