Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Include python import statements in vim /emacs?

Tags:

python

vim

emacs

In eclipse, there is this handy shorthand CTRL+SHIFT+o which will auto include the import (include) statements which are needed based on the Class or module being used. Have you found any such plugin for vim or emacs?

like image 513
Senthil Kumaran Avatar asked Oct 01 '10 10:10

Senthil Kumaran


2 Answers

Ropemacs has rope-auto-import, so that if you write

rmtree

and execute M-x rope-auto-import,

from shutil import rmtree

is inserted at the top of the file. You might want to avoid this though, because importing functions from modules may not be a very wise idea.

See this SO post for information on setting up ropemacs. The README (e.g. /usr/share/doc/python-ropemacs/README.txt.gz) that comes with ropemacs also has useful information for setting up the cache used by the ropemacs-auto-import command.

like image 119
unutbu Avatar answered Sep 26 '22 15:09

unutbu


So I'm happy with this simple function that already saves me repeated movements. When I call my-python-import-add:

  • I am asked what modules to import (I can give a comma-separated list like os, sys, from foo import bar, re)
  • it inserts either an import xxx statements or the from … you typed.
  • the imports are sorted.
  • the point didn't move (of course).

So for example: M-x my-python-import-add RET os, sys, from foo import bar, requests gives me:

import os
import sys

import requests

from foo import bar

ps: found one caveat: you need to already have an import statement.


We rely on the isort python package:

pip install isort

and on the py-isort package (in Melpa),

so read their helps to check how to customize the sorting feature. I myself want a single import by line so I setted

(setq py-isort-options '("--force_single_line_imports"))

The function:

edit: I put it in a gitlab repo

update: now the prompt suggests the word at point by default

(require 's)  ;; melpa
(require 'dash)  ;; melpa

(require 'py-isort)  ;; melpa

(defun my-insert-import (to_insert)
  (newline)
  (if (s-starts-with? "from" (s-trim to_insert)) (insert (s-trim to_insert))
    (insert "import " to_insert))
  )

(setq py-isort-options '("--force_single_line_imports"))


(defun python-import-add (to_import)
  "Adds an import statement for every given module. Can give a comma-separated list of modules, like:
M-x my-python-import-add RET os, sys, from foo import bar, re RET"
  (interactive "swhat to import ? ")
  (beginning-of-buffer)
  (save-excursion
    (search-forward-regexp "\\(^from \\)?.*import [a-z]+")  ;; if not found ?
    (end-of-line)
    ;; split the arguments by spaces:
    (setq to_insert (s-split "," to_import))
    ;; insert each elt of the list:
    (-map 'my-insert-import to_insert)
    ;; sort the imports: (set your py-isort options. See isort -h)
    (py-isort)
    )
  )

What I will maybe do now is grab the thing-at-point (like sys.argv) and suggest it as a default. =>done

I'm confident we can have a useful and complete auto-import feature in emacs.

like image 29
Ehvince Avatar answered Sep 25 '22 15:09

Ehvince