Skip to content

An Emacs library for inserting Python import statements.

License

Notifications You must be signed in to change notification settings

KarimAziev/pyimp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

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.

Table of Contents

Requirements

NameVersion
Emacs with treesit support29.1
pyvenv1.21

Installation

With use-package and straight.el

(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))

Manual installation

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)

Usage

Invoke the command pyimp-import (for example via M-x or a bound key) to insert an import statement. The process works as follows:

  1. You will be prompted for a Python module. pyimp searches your project, installed libraries, and built-in modules.
  2. 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).
  3. 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).

Configuration and Customization

pyimp provides several customizable variables. For instance:

  • pyimp-files-sorting-threshold Sets the threshold (in number of files) that determines whether project files should be sorted. (Set to nil to always sort.)
  • pyimp-allow-minibuffer-marking If enabled, lets you mark multiple symbols in the minibuffer for import. If enabled, you may want to customize pyimp-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-functions A hook variable that specifies functions used to extract exported symbols from a module.

About

An Emacs library for inserting Python import statements.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published