pyimp is an Emacs library that helps you insert Python import statements.
Why
While many LSP servers assume you’re typing a symbol and then rely on auto-import completions, they may fall short in scenarios where:
- The desired symbol isn’t immediately suggested.
- You’re pasting code or otherwise not triggering completions.
- You remember the module but not the symbol name.
pyimp fills this gap by giving you precise control over import statements. This tool can work alongside your LSP server or serve as a standalone solution for handling Python imports in Emacs.
| Name | Version |
|---|---|
Emacs with treesit support | 29.1 |
| pyvenv | 1.21 |
(use-package pyimp
:straight (pyimp
:repo "KarimAziev/pyimp"
:type git
:host github
:flavor nil)
:bind ((:map pyimp-modules-minibuffer-map
("C-j" . pyimp-describe-module)
("C-c C-o" . pyimp-abort-minibuffer-and-describe-module)))
:commands (pyimp-import))Download the source code and put it wherever you like, e.g. into ~/.emacs.d/pyimp/
git clone https://github.com/KarimAziev/pyimp.git ~/.emacs.d/pyimp/Add the downloaded directory to the load path:
(add-to-list 'load-path "~/.emacs.d/pyimp/")
(require 'pyimp)Invoke the command pyimp-import (for example via M-x or a bound key) to insert an import statement. The process works as follows:
- You will be prompted for a Python module. pyimp searches your project, installed libraries, and built-in modules.
- After selecting a module, pyimp presents a list of exported symbols from the module. You can choose a single symbol or mark multiple symbols (if you enable minibuffer marking).
- Once selected, pyimp automatically inserts or updates the appropriate import statement in your Python file.
If you ever need to read module documentation or review what’s been imported, you can use the provided module description functionality (bound under C-j by default).
pyimp provides several customizable variables. For instance:
pyimp-files-sorting-thresholdSets the threshold (in number of files) that determines whether project files should be sorted. (Set to nil to always sort.)pyimp-allow-minibuffer-markingIf enabled, lets you mark multiple symbols in the minibuffer for import. If enabled, you may want to customizepyimp-multiple-symbols-minibuffer-map:
(use-package pyimp
:straight (pyimp
:repo "KarimAziev/pyimp"
:type git
:host github
:flavor nil)
:bind ((:map pyimp-multiple-symbols-minibuffer-map
("RET" . pyimp-minibuffer-done)
("C-SPC" . pyimp-minibuffer-mark)
("C-<return>" . pyimp-minibuffer-done)
("C-M-j" . pyimp-minibuffer-done)))
:commands (pyimp-import))pyimp-extract-module-export-functionsA hook variable that specifies functions used to extract exported symbols from a module.