Function: python-add-import

python-add-import is an autoloaded, interactive and byte-compiled function defined in python.el.gz.

Signature

(python-add-import NAME)

Documentation

Add an import statement to the current buffer.

Interactively, ask for an import statement using all imports found in the current project as suggestions. With a prefix argument, restrict the suggestions to imports defining the symbol at point. If there is only one such suggestion, act without asking.

If the buffer does not belong to a project, the import statement is searched under the buffer's default directory. For example, if the file is located directly under the home directory, all files under the home directory will be searched. Please note that this can take a long time and may appear to hang.

When calling from Lisp, use a non-nil NAME to restrict the suggestions to imports defining NAME.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
;;;###autoload
(defun python-add-import (name)
  "Add an import statement to the current buffer.

Interactively, ask for an import statement using all imports
found in the current project as suggestions.  With a prefix
argument, restrict the suggestions to imports defining the symbol
at point.  If there is only one such suggestion, act without
asking.

If the buffer does not belong to a project, the import statement is
searched under the buffer's default directory.  For example, if the file
is located directly under the home directory, all files under the home
directory will be searched.  Please note that this can take a long time
and may appear to hang.

When calling from Lisp, use a non-nil NAME to restrict the
suggestions to imports defining NAME."
  (interactive (list (when current-prefix-arg (thing-at-point 'symbol))))
  (when-let* ((statement (python--query-import name
                                               (python--import-sources)
                                               "Add import: ")))
    (if (python--do-isort "--add" statement)
        (message "Added `%s'" statement)
      (message "(No changes in Python imports needed)"))))