Skip to content

Instantly share code, notes, and snippets.

@AyakoGFX
Created December 28, 2025 00:17
Show Gist options
  • Select an option

  • Save AyakoGFX/27e21550f6401084b370d56ae2862900 to your computer and use it in GitHub Desktop.

Select an option

Save AyakoGFX/27e21550f6401084b370d56ae2862900 to your computer and use it in GitHub Desktop.
Emacs Python

NOTES

uv init project-name
uv venv
M-x uv-activate 
uv add pyright
M-!   
M-&

Config

 (use-package eglot
:ensure t
:defer t
:hook ((python-mode . eglot-ensure)
       (go-mode . eglot-ensure))
:config
(add-to-list 'eglot-server-programs
             `(python-mode
               . ,(eglot-alternatives
                   '(("pyright-langserver" "--stdio")
                     "jedi-language-server"
                     "pylsp")))))

 ;;; Yasnippet
 (use-package yasnippet
   :ensure t
   :init
   (yas-global-mode 1))

 (use-package yasnippet-snippets
   :ensure t)

 ;;; Company
 (use-package company
   :ensure t
   :hook ((prog-mode . company-mode))
   :bind (:map company-active-map
               ("<return>" . nil)
               ("RET" . nil)
               ("C-<return>" . company-complete-selection)
               ("C-h" . my/company-show-doc)
               ([tab] . company-complete-selection)
               ("TAB" . company-complete-selection))
   :config
   (setq company-backends '(company-capf company-yasnippet)))

 (defun my/company-show-doc ()
   (interactive)
   (let ((doc-buffer
          (company-call-backend
           'doc-buffer
           (nth company-selection company-candidates))))
     (when doc-buffer
       (with-selected-window (split-window-below)
         (switch-to-buffer doc-buffer)
         (read-only-mode 1)
         (local-set-key (kbd "q") #'kill-buffer-and-window)))))

 (setq company-minimum-prefix-length 1
       company-idle-delay 0)

 ;;; Company Box
 (use-package company-box
   :ensure t
   :hook (company-mode . company-box-mode))

 (setq company-box-doc-delay 0.2)

 ;;; Eglot ? Yasnippet integration
 (add-hook 'eglot-managed-mode-hook
           (lambda ()
             (yas-minor-mode 1)))

 ;;; UV https://mclare.blog/posts/using-uv-in-emacs/
 (defun uv-activate ()
   "Activate Python environment managed by uv based on current project directory.
 Looks for .venv directory in project root and activates the Python interpreter."
   (interactive)
   (let* ((project-root (project-root (project-current t)))
          (venv-path (expand-file-name ".venv" project-root))
          (python-path (expand-file-name
                        (if (eq system-type 'windows-nt)
                            "Scripts/python.exe"
                          "bin/python")
                        venv-path)))
     (if (file-exists-p python-path)
         (progn
           ;; Set Python interpreter path
           (setq python-shell-interpreter python-path)

           ;; Update exec-path to include the venv's bin directory
           (let ((venv-bin-dir (file-name-directory python-path)))
             (setq exec-path (cons venv-bin-dir
                                   (remove venv-bin-dir exec-path))))

           ;; Update PATH environment variable
           (setenv "PATH" (concat (file-name-directory python-path)
                                  path-separator
                                  (getenv "PATH")))

           ;; Update VIRTUAL_ENV environment variable
           (setenv "VIRTUAL_ENV" venv-path)

           ;; Remove PYTHONHOME if it exists
           (setenv "PYTHONHOME" nil)

           (message "Activated UV Python environment at %s" venv-path))
       (error "No UV Python environment found in %s" project-root))))
 ;; --------new-lsp-end--------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment