Skip to content

[Bug] Neovim remote plugin clear PYTHONPATH environment #358

Description

@Tias-dev
  • OS: NixOS
  • NeoVim Version: 0.11.6
  • Python Version: 3.13
  • Python is installed with: home-manager
`:checkhealth molten`

molten-nvim ~

  • ✅ OK NeoVim >=0.9
  • ✅ OK Python >=3.10
  • ✅ OK Python module pynvim found
  • ✅ OK Python module jupyter-client found
  • ✅ OK Python module cairosvg found
  • ✅ OK Python module pnglatex found
  • ✅ OK Python module plotly found
  • ⚠️ WARNING Optional python module kaleido not found
    • ADVICE:
      • pip install kaleido
  • ✅ OK Python module pyperclip found
  • ✅ OK Python module nbformat found
  • ✅ OK Python module pillow found

Description

When i call MoltenInit and choose kernel. Neovim became very laggy without any errors and clues. After dig inside plugin code i found that stucks happens in file rplugin/python3/molten/runtime.py line 217

            self.kernel_client.wait_for_ready(timeout=0)

There always RuntimeError is thrown, so after adding debug notification with this error

            except RuntimeError:
                return False
# become to
            except RuntimeError as e:
                notify_error(self.nvim, str(e))
                return False
Image

i found following error message

03:14:56 PM notify.error [Molten] Kernel died before replying to kernel_info
Image

To figure out why this happen i dig even deeper and connect stdout and stderr outputs from running ipykernel_launch by adding this (line 57):

            self.kernel_manager.start_kernel()
# change to this
            stdout=open("/tmp/kernelStdout.log", "wb")
            stderr=open("/tmp/kernelStderr.log", "wb")
            self.kernel_manager.start_kernel(stdout=stdout, stderr=stderr)
Image

and in file /tmp/kernelStderr.log i found

/nix/store/44rn0p64x92bnnh7cwn6x6ybvflybmvz-python3-3.13.12/bin/python: No module named ipykernel_launcher
Image

but in my nix-shell(neovim also started from nix-shell) i can run

/nix/store/44rn0p64x92bnnh7cwn6x6ybvflybmvz-python3-3.13.12/bin/python -m ipykernel_launcher

and it is found

Image

So after some investigations i found that by some reason molten (launched as remote plugin) have no PYTHONPATH environment variable is set(nix-shell have this env) and after manually adding this environment

Image

all works

Image

I don't really know why environment variable PYTHONPATH is cleared in remote plugin runtime but it is was really hard to figure out

Reproduction Steps

I use lazyvim config
with following molten config:

local filetypes = { "quarto", "markdown", "jupyter", "json" }
return {
  {
    "benlubas/molten-nvim",
    version = "*", -- use version <2.0.0 to avoid breaking changes
    dependencies = { "3rd/image.nvim" },
    build = ":UpdateRemotePlugins",
    init = function()
      -- these are examples, not defaults. Please see the readme
      vim.g.molten_image_provider = "image.nvim"
      vim.g.molten_output_win_max_height = 20
			vim.g.molten_tick_rate = 1000
			vim.g.molten_auto_image_popup = true
			vim.g.molten_auto_open_output = true
    end,
    ft = filetypes,
  },
  {
    "3rd/image.nvim",
    build = false, -- so that it doesn't build the rock https://github.com/3rd/image.nvim/issues/91#issuecomment-2453430239
    version = "1.1.0",
    opts = {
      processor = "magick_cli",
      backend = "kitty", -- Kitty will provide the best experience, but you need a compatible terminal
      integrations = {}, -- do whatever you want with image.nvim's integrations
      max_width = 100, -- tweak to preference
      max_height = 12, -- ^
      max_height_window_percentage = math.huge, -- this is necessary for a good experience
      max_width_window_percentage = math.huge,
      window_overlap_clear_enabled = true,
      window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
    },
    ft = filetypes,
  },
  {
    "quarto-dev/quarto-nvim",
    opts = {
      ft = filetypes,
      lspFeatures = {
        -- NOTE: put whatever languages you want here:
        languages = { "python" },
        chunks = "all",
        diagnostics = {
          enabled = true,
          triggers = { "BufWritePost" },
        },
        completion = {
          enabled = true,
        },
      },
      keymap = {
        -- NOTE: setup your own keymaps:
        hover = "K",
        definition = "gd",
        rename = "<leader>rn",
        references = "gr",
        format = "<leader>gf",
      },
      codeRunner = {
        enabled = true,
        default_method = "molten",
      },
    },
  },
  {
    "GCBallesteros/jupytext.nvim",
    dependencies = {
      "jmbuhr/otter.nvim",
      "nvim-treesitter/nvim-treesitter",
    },
    ft = filetypes,
		lazy=false,
    opts = {
      style = "markdown",
      output_extension = "md",
      force_ft = "markdown",
    },
  },
}

And following home-manager based neovim setup:

{ pkgs, ... }:
{
  programs.neovim = {
    enable = true;
    defaultEditor = true;
    # whatever other neovim configuration you have
    extraPackages = with pkgs; [
      tree-sitter

      # formatters
      alejandra
      tex-fmt

      # lsp
      stylua
      nil
      lua-language-server
      texlab
      clang-tools
      omnisharp-roslyn

      imagemagick # for image rendering
    ];
    extraLuaPackages =
      ps: with ps; [
        # ... other lua packages
        magick # for image rendering
      ];
    extraPython3Packages =
      ps: with ps; [
        pynvim
        nbformat
        jupyter-client
        ipykernel
        cairosvg # for image rendering
        pnglatex # for image rendering
        plotly # for image rendering
        pyperclip
      ];
  };
}

and following shell.nix file

{
  pkgs ? import <nixpkgs> { },
}:
pkgs.mkShell {
  buildInputs = [
    pkgs.python3
    (pkgs.python313.withPackages (
      ps: with ps; [
        notebook
        pynvim
				ipykernel
        jupyter-client
        jupyter-console
      ]
    ))
  ];
  shellHook = ''
    export LD_LIBRARY_PATH="${pkgs.stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH"
    export JUPYTER_ALLOW_INSECURE_WRITES=1
  '';
  packages = [
    pkgs.python3
    (pkgs.python313.withPackages (
      ps: with ps; [
        notebook
        pynvim
				ipykernel
        jupyter-client
        jupyter-console
      ]
    ))
  ];
}

In this repo:

https://github.com/Timur-ux/AILabs-CV/tree/molten-issue

Maybe there exist simple nix config option to handle this or it is really bug in nix based configurations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions