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?
-
1stackoverflow.com/questions/3825073/…atomocopter– atomocopter2010-10-01 13:51:33 +00:00Commented Oct 1, 2010 at 13:51
-
2Maybe you should restrict your question to emacs of vi but not both, as answers will be very different.Jérôme Radix– Jérôme Radix2010-10-01 15:03:48 +00:00Commented Oct 1, 2010 at 15:03
-
1As far as I know, for emacs there's no such thing. However, if you use the same dependencies often, you could use emacs templates (emacs-template.sourceforge.net) to generate a "blank" python file containing your important dependencies.phimuemue– phimuemue2010-10-01 18:54:17 +00:00Commented Oct 1, 2010 at 18:54
-
1This seems like a half-duplicate of my own question about vim :/Daenyth– Daenyth2010-10-01 20:07:24 +00:00Commented Oct 1, 2010 at 20:07
-
Daenyth, it is nearly the same. I am sorry, I could not find it when I searched for it.Senthil Kumaran– Senthil Kumaran2010-10-02 11:22:56 +00:00Commented Oct 2, 2010 at 11:22
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.
8 Comments
M-x rope-auto-import can only create imports of the "from X import Y" variety.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 xxxstatements or thefrom …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.