Skip to content

Configuring lsp mode on a ein notebook cell on top of python major mode #923

@dcguim

Description

@dcguim

I am attempting to integrate lsp-mode when I am inside a ein notebook cell, it's not working as expected, I am getting LSP[Disconnected] or "No workspaces associated with the current buffer" when connecting to lsp from a ein cell. It would be nice if one could shed light on a potential error, when I run M-x lsp I get no entries in *Messages*, so the error is appearing before the time *lsp-log* buffer is even created.

I am running a notebook from a custom torch-env kernel that I created - follow my config:

$ which pylsp python3

/Users/dguim/Library/Python/3.9/bin/pylsp
/Users/dguim/work/pytorch-book/torch-env/bin/python

Follow my lsp and ein .emacs config:

(use-package ein
  :ensure t
  :init
  ;; Set default notebook directory to current directory
  (setq ein:jupyter-server-use-command "jupyter")
  (setq ein:jupyter-server-use-containers nil)
  (setq ein:jupyter-default-notebook-directory default-directory)
  (defun ein-activate-venv ()
    "Activate the Poetry or virtual environment before running ein:run."
    (let ((poetryenv-path (string-trim (shell-command-to-string "poetryenv")))
          (virtualenv-path (getenv "VIRTUAL_ENV")))
      (cond
       ;; Activate Poetry environment if detected
       ((and poetryenv-path (file-exists-p (concat poetryenv-path "/bin/activate")))
	(pyvenv-activate poetryenv-path)
	(message "Activated Poetry environment: %s" poetryenv-path))
       ;; Activate virtual environment from VIRTUAL_ENV if set
       ((and virtualenv-path (file-exists-p (concat virtualenv-path "/bin/activate")))
	(pyvenv-activate virtualenv-path)
	(message "Activated virtual environment: %s" virtualenv-path))
       ;; No environment detected
       (t (message "No virtual environment detected for EIN")))))
  (add-hook 'ein:notebooklist-login-hook 'ein-activate-venv)
  ;; Automatically activate lsp environment when using python kernel
  (defun ein-notebook-activate-lsp ()
    "Activate LSP mode for Python files in EIN Jupyter notebooks."
    (when (or (derived-mode-p 'python-mode)
              (derived-mode-p 'ein:notebook-multilang-mode))
      (lsp-deferred)))
  (add-hook 'python-mode-hook 'ein-notebook-activate-lsp)
  ;; Enable auto-completion and undo in notebooks
  (setq ein:worksheet-enable-undo t)
  (setq ein:use-auto-complete nil))

(use-package lsp-mode
  :ensure t
  :config
  (setq lsp-log-io t)
  (setq lsp-pylsp-server-command '("/Users/dguim/Library/Python/3.9/bin/pylsp"))
  :hook (python-mode . lsp))

The output of M-x describe-mode:

Minor modes enabled in this buffer: Company Ein:Notebook Eldoc Font-Lock Lsp

The major mode is Python mode defined in python.el:

Major mode for editing Python files.

When using lsp from a normal .py file the major python mode is configured as expected, and lsp works normally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions