5

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?

7
  • 1
    stackoverflow.com/questions/3825073/… Commented Oct 1, 2010 at 13:51
  • 2
    Maybe you should restrict your question to emacs of vi but not both, as answers will be very different. Commented Oct 1, 2010 at 15:03
  • 1
    As 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. Commented Oct 1, 2010 at 18:54
  • 1
    This seems like a half-duplicate of my own question about vim :/ Commented 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. Commented Oct 2, 2010 at 11:22

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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: Unfortunately, AFAIK, M-x rope-auto-import can only create imports of the "from X import Y" variety.
@unutbu is an anagram of Ubuntu. :)
Does jedi offer something similar as ropemacs?
@qed: Sorry, I don't know.
|
2

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.

4 Comments

Thanks, this works great and is much faster than using rope.
Cool ! :) btw, I created a git repo, so you can track changes or suggest improvements.
Does this work for the modules we are implemented? @Ehvince
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.