Skip to main content

Overview

mr.igor is an extension to pyflakes that will learn where you import things from, and then automatically fill in missing imports from the place they are most often imported.

But how does it know?

“It’th a knack.”

Usage: igor [–print | –reap] filename

This script will record all imports from filename in Igor’s database, and then add imports at the top of the file for any names that were not imported but were found in the database.

If the –print option is specified then the rewritten file will be written to stdout. (This allows the use of igor as a filter for editors.) Otherwise the file will be modified inplace.

If the –reap option is specified then imports will be added to the database from the specified file, but nothing will be written to stdout and the file will not be modified.

Only “from x import y” style imports are tracked and inserted. Aliases (“from x import y as z”) are not supported.

mr.igor stores its database in the user’s home directory. The base filename is ~/.mr.igor, but note that the Python shelve module may use a different physical filename, e.g. with a .db suffix.

Usage with TextMate

Go to the TextMate Bundle Editor and add a new command with the following settings:

Save

Current File

Command(s)
#!/bin/bash
igor --print $TM_FILEPATH
Input

None

Output

Replace Document

Activation

Key Equivalent: ⌘I

Scope Selector

source.python

Now you can save the current file and run it through Igor using the ⌘I keyboard shortcut.

Usage with Vim

The following key mapping will configure ⌘I to run the current file through Igor and then reload the current buffer:

nmap <D-i> :!igor %<CR> <bar> :e!<CR>

Usage with Emacs

Add the following to your .emacs. The igor function runs Igor over a temporary file copy of the current buffer. The current buffer is marked as modified only if Igor adds new imports.

(defun igor ()
  "Run the current buffer through mr.igor."
  (interactive)
  (let ((igor-exe (or (executable-find "igor")
                      (error "No command 'igor' found")))
        (tempfile (make-temp-file "igor"))
        (buffer (current-buffer))
        (lines-before (count-lines 1 (buffer-size))))
    (with-temp-file tempfile
      (insert-buffer-substring buffer))
    (with-temp-buffer
      (shell-command (concat igor-exe " --print " tempfile) t)
      (if (zerop (compare-buffer-substrings
                  (current-buffer) 1 (buffer-size)
                  buffer 1 (buffer-size buffer)))
          (message "igor: no new imports")
        (copy-to-buffer buffer 1 (buffer-size))
        (let ((lines-after (count-lines 1 (buffer-size))))
          (message "igor: added %d imports" (- lines-after lines-before))))
      (delete-file tempfile))))

(add-hook 'python-mode-hook '(lambda () (local-set-key (kbd "C-c C-i") 'igor)))

Changelog

2013/10/05

  • Released version 1.4. [davisagli]

  • ImportChecker’s __del__ isn’t called with pyflakes 0.7 so the database is never written. Switch to a with() statement, where the __exit__ method is guaranteed to be called. [danjacka]

2011/10/2

  • Released version 1.3. [davisagli]

  • Insert imports after initial set of comments, to fix issue when PEP0263 encoding is declared in python files. [naro]

  • Updated to be compatible with (and require) pyflakes 0.5. Tested with Python 2.6 and 2.7. [davisagli]

2010/1/7

  • Released version 1.2. [davisagli]

  • Added –reap option. [davisagli]

2009/12/14

  • Added usage info for integration with Vim. Thanks to Stephan Eletzhofer. [davisagli]

  • Released version 1.1. [davisagli]

  • Fix issue where a syntax error would cause the file to be clobbered when using print mode as a filter. [davisagli]

  • Fix issue where print mode would not print anything if no new imports were found. [davisagli]

  • Fix issue where multiple imports in the same line were not recorded. [davisagli]

  • Released version 1.0. [davisagli]

  • Added functional test. [davisagli]

  • Refactor to improve testability; avoid monkeypatching. [davisagli]

2009/12/13

  • Add help for console script. [davisagli]

  • Use ~/.mr.igor.db as default database location. [davisagli]

  • Added –print option. [davisagli]

  • Added igor console script. [davisagli]

2009/12/12

  • Initial proof-of-concept. [davisagli]

Metadata

Release files for mr.igor 1.4

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

Source distribution (sdist)

Source distribution for mr.igor 1.4
File Size Uploaded
mr.igor-1.4.zip 13.3 kB Details

Release files / mr.igor-1.4.zip

Download URL mr.igor-1.4.zip
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d40b46aee638a03879c9a7a61a50d2039e44bfd8244e385e4f73e16f2d855efb
BLAKE2b-256 checksum
How to use checksums
9dc8d10eb71ec7277d0bb6ef73a21a923931927e94d3dc7ad607eb7d282f729e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.4 This release

1 release file

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

0.1

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