Skip to content

Instantly share code, notes, and snippets.

View con-cat's full-sized avatar
🐈

Caitlin White con-cat

🐈
  • Melbourne, Australia
View GitHub Profile
@con-cat
con-cat / get-python-import-statement.el
Last active December 31, 2023 04:54
Emacs lisp function to return an import statement for a Python file or object
(defun con-cat/get-python-import-statement (root-in-project)
"Return an import statement for a Python file or object.
root-in-project is the optional path to a directory inside the project that the path should be relative to,
e.g. 'src' if your project is in ~/myproject and your Python files are in ~/myproject/src"
(unless (and (projectile-project-root) buffer-file-name (string= "py" (file-name-extension buffer-file-name)))
(user-error "Buffer isn't visiting a Python file in a Projectile project."))
(let*
;; Get the path of the file relative to projectile-project-root and root-in-project
((file-path (file-relative-name buffer-file-name (expand-file-name root-in-project (projectile-project-root))))