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 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

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. 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]
                  paths [paths ...]

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:
  paths                 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.

Files for removestar, version 1.2.1
Filename, size File type Python version Upload date Hashes
Filename, size removestar-1.2.1-py3-none-any.whl (16.1 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size removestar-1.2.1.tar.gz (30.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page