qed
qed

Reputation: 23134

Auto import modules with emacs-jedi

With ropemacs you can do something like this:

M-x rope-auto-import

This analyze the code (I presume) and imports missing modules, for example if I write:

datetime.now()

it should do the import for me by add this line:

from datetime import datetime

(it always uses the from ... import ... form)

Is there a similar function in emacs-jedi?

Upvotes: 8

Views: 2021

Answers (3)

rutger
rutger

Reputation: 2355

For future reference, M-x python-add-import has been added to Emacs 29.

This works without jedi, rope, or lsp.

With a prefix argument, it will try to find the import for symbol at point.

Upvotes: 0

Att Righ
Att Righ

Reputation: 1799

If we ignore the jedi part and pay attention to the emacs part you could use the follow command after installing autoimport.

(defun my-python-autoimports ()
  (interactive)
  (save-buffer)
  (shell-command (s-concat "autoimport " (shell-quote-argument (buffer-file-name))))
  (revert-buffer t t))

Upvotes: 1

Dave Halter
Dave Halter

Reputation: 16335

Jedi doesn't support auto imports, yet. (And therefore obviously emacs-jedi does neither)

There are discussions ongoing to implement refactorings as well (which includes auto imports). However, it will probably take another year or so.

Upvotes: 3

Related Questions