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?
-
1http://stackoverflow.com/questions/3825073/is-there-a-plugin-for-vim-to-auto-import-python-libraries – atomocopter Oct 01 '10 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 Oct 01 '10 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 (http://emacs-template.sourceforge.net/) to generate a "blank" python file containing your important dependencies. – phimuemue Oct 01 '10 at 18:54
-
1This seems like a half-duplicate of my own question [about vim](http://stackoverflow.com/questions/3825073/is-there-a-plugin-for-vim-to-auto-import-python-libraries) :/ – Daenyth Oct 01 '10 at 20:07
-
Daenyth, it is nearly the same. I am sorry, I could not find it when I searched for it. – Senthil Kumaran Oct 02 '10 at 11:22
-
Jérôme, either is fine with me. I can use it alternatively. I was hoping if eclipse is not exclusive with this feature. Looks like it is. – Senthil Kumaran Oct 02 '10 at 11:25
-
Senthil, sorry if I misunderstood your question. Does Eclipse's `Ctrl+Shift+o` do something different than emac's `M-x ropemacs-auto-import`? – unutbu Oct 02 '10 at 13:40
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.
-
unutbu, that's an useful pointer. BTW, can't ropemacs do a non-relative import. As in, I use shutil.rmtree and when I do M-x ropemacs-auto-import, it does import shutil ? – Senthil Kumaran Oct 05 '10 at 04:40
-
@Senthil: Unfortunately, AFAIK, `M-x rope-auto-import` can only create imports of the "from X import Y" variety. – unutbu Oct 05 '10 at 10:39
-
-
-
-
-
@qed: To tell you the truth, I don't use jedi or ropemacs. I collect runnable code snippets, and copy/paste/edit. I also use IPython from within emacs, which gives me autocompletion of a sort, though I use it more for introspection/exploration than for autocompletion-while-programming. – unutbu Jun 10 '14 at 17:10
-
I tried ropemacs but it keeps saying `Global name rmtree not found!` @unutbu – alper Jul 23 '20 at 12:30
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 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.

- 17,274
- 7
- 58
- 79
-
-
Cool ! :) btw, I created [a git repo](https://gitlab.com/emacs-stuff/python-import-add), so you can track changes or suggest improvements. – Ehvince Mar 07 '16 at 22:27
-
-
1@alper if you ask if it works for our own local modules, yes, because the tool is dumb, it only automates something you would write manually. It is not an automatic import, just a helper to write them. – Ehvince Apr 18 '20 at 10:06