4

I'm trying to use nvim 0.6 as my IDE and I'm working on a large project that has several local modules (located in the same directory as the main Python script, not installed through pip). When I use VSCode, these modules are imported just fine. However, in nvim, I get a "could not be resolved" error on every local module. I'm using pyenv but the problem is identical if I switch to any other versions; setting Python to system or any other version causes errors for the pip-installed modules, since those aren't installed in those environments, but in all environments VSCode loads the local modules correctly and nvim does not. What am I missing?

Errors with nvim: enter image description here

But in VSCode, everything is loaded fine: enter image description here

Both are using Python 3.9.5 via pyenv as their interpreter. If I open a Python prompt from the command line I can import those modules and use them no problem:

>python
Python 3.9.5 (default, Jul  3 2021, 18:40:06)
[Clang 13.0.0 (clang-1300.0.18.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import weewx
>>> dir(weewx)
['CHECK_LOOP', 'CMD_ERROR', 'CONFIG_ERROR', 'CRCError', 'CannotCalculate', 'DB_ERROR', 'END_ARCHIVE_PERIOD', 'Event', 'HardwareError', 'IO_ERROR', 'METRIC', 'METRICWX', 'NEW_ARCHIVE_RECORD', 'NEW_LOOP_PACKET', 'POST_LOOP', 'PRE_LOOP', 'RetriesExceeded', 'STARTUP', 'StopNow', 'US', 'UnitError', 'UnknownAggregation', 'UnknownArchiveType', 'UnknownBinding', 'UnknownDatabase', 'UnknownDatabaseType', 'UnknownType', 'UnsupportedFeature', 'ViolatedPrecondition', 'WakeupError', 'WeeWxIOError', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'absolute_import', 'all_service_groups', 'debug', 'launchtime_ts', 'require_weewx_version', 'time']
user2597451
  • 1,903
  • 2
  • 12
  • 10

2 Answers2

2

I was able to resolve this by manually setting the extraPath variable for the built-in nvim LSP in init.lua. I'm still not sure how to make this happen automatically.

require("lspconfig").pyright.setup {
  settings = {
    python = {
      analysis = {
        extraPaths = {"path/to/desired/modules"}
      }
    }
  }
}
user2597451
  • 1,903
  • 2
  • 12
  • 10
1

I was able to resolve this by create pyrightconfig.json in the root of my project, and set the root path in the file like this:

{
  "executionEnvironments": [
     {"root": "src"}
  ]
}
Jack
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Sep 19 '22 at 11:37