Auto Include python import statements in vim /emacs?
Asked Answered
C

2

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?

Cullen answered 1/10, 2010 at 10:57 Comment(7)
#3825573Snipes
Maybe you should restrict your question to emacs of vi but not both, as answers will be very different.Saprophagous
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.Stocky
This seems like a half-duplicate of my own question about vim :/Phylogeny
Daenyth, it is nearly the same. I am sorry, I could not find it when I searched for it.Cullen
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.Cullen
Senthil, sorry if I misunderstood your question. Does Eclipse's Ctrl+Shift+o do something different than emac's M-x ropemacs-auto-import?Doughy
D
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.

Doughy answered 1/10, 2010 at 19:43 Comment(8)
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 ?Cullen
@Senthil: Unfortunately, AFAIK, M-x rope-auto-import can only create imports of the "from X import Y" variety.Doughy
@Doughy is an anagram of Ubuntu. :)Charlsiecharlton
Does jedi offer something similar as ropemacs?Charlsiecharlton
@qed: Sorry, I don't know.Doughy
Do you think it's a good idea to install both emacs-jedi and ropemacs?Charlsiecharlton
@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.Doughy
I tried ropemacs but it keeps saying Global name rmtree not found! @DoughyFigueroa
D
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.

Derk answered 6/5, 2014 at 17:12 Comment(4)
Thanks, this works great and is much faster than using rope.Laural
Cool ! :) btw, I created a git repo, so you can track changes or suggest improvements.Derk
Does this work for the modules we are implemented? @DerkFigueroa
@Figueroa 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.Derk

© 2022 - 2024 — McMap. All rights reserved.