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:

But in VSCode, everything is loaded fine:

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']
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"}
}
}
}
}
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"}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With