neovim / pynvim

Python client and plugin host for Nvim
http://pynvim.readthedocs.io/en/latest/
Apache License 2.0
1.48k stars 118 forks source link

importing my Python module does not work #494

Closed wakita closed 2 years ago

wakita commented 2 years ago

My attempts to split a Python remote plugin into multiple Python files so far do not work.

The following is the simplest example that demonstrates my problem. I have two Python files. a.py defines an empty class names A. Another file _a.py defines a neovim command named AAA in _A class that extends A.

# rplugin/python3/a.py

class A(object): pass
# rplugin/python3/_a.py

import pynvim
import a

@pynvim.plugin
class _A(A):
  def __init__(self, nvim):
    self.nvim = nvim

  @pynvim.command('AAA', nargs='*', range='', sync=False)
  def a(self, args, range):
    self.nvim.out_write('A message from A\n')

:UpdateRemotePlugins fails to find a.py

Encountered ModuleNotFoundError loading plugin at /Users/wakita/Dropbox/lib/nvim/rplugin/python3/_a.
py: No module named 'a'
Traceback (most recent call last):
  File "/Users/wakita/.venvs/nvim/lib/python3.9/site-packages/pynvim/plugin/host.py", line 165, in _
load
    module = imp.load_module(name, file, pathname, descr)
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/im
p.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/im
p.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 711, in _load
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
ModuleNotFoundError: No module named 'a'

I tried to move a.py to everywhere I could imagine but nothing worked. I tried sys.path.append trick but UpdateRemotePlugin ignores.

By the way, UpdateRemotePlugin seems to recognize import inside a Python comment. I think it should skip comments.

Shougo commented 2 years ago

Hm... Why my deoplete works? It imports deoplete module in it.

https://github.com/Shougo/deoplete.nvim/blob/master/rplugin/python3/deoplete/__init__.py#L11

Shougo commented 2 years ago

Hm. You need to create subdirectory and rename the files.

It works for me.

# rplugin/python3/aaa/__init__.py

import pynvim
import aaa.a import A

@pynvim.plugin
class _A(A):
  def __init__(self, nvim):
    self.nvim = nvim

  @pynvim.command('AAA', nargs='*', range='', sync=False)
  def a(self, args, range):
    self.nvim.out_write('A message from A\n')
# rplugin/python3/aaa/a.py

class A(object): pass
wakita commented 2 years ago

Thank you! Your trick did work on my code. Many thanks!

(you may want to change the second line of your version of __init__.py: you meant from aaa.a import A)

Shougo commented 2 years ago

You should close it.