Greg Iven
Greg Iven

Reputation: 183

VS Code import resolution error with Pylance (ModuleNotFoundError)

Having an import resolution error with pylance not recognizing packages. (Windows 10)

Receiving the error:

ModuleNotFoundError: No module named 'abc123'

Inside the file, hovering over the broken import says

"packageFoo" is not accessible

Import "packageFoo" could not be resolved Pylance

Upvotes: 1

Views: 9826

Answers (4)

ocean11
ocean11

Reputation: 520

Try this:

  1. Preferences > Settings > Search pylance
  2. Find Python> Analysis: Diagnostic Severity Overrides
  3. Click Edit in settings.json
  4. Add following in "python.analysis.diagnosticSeverityOverrides": { }'s curly brackets { }
        "reportGeneralTypeIssues": "none",
        "reportOptionalMemberAccess": "none",
        "reportOptionalSubscript": "none",

This should solve some un-intended underlines. There are other options to consider:

  • "reportMissingImports": Controls the severity of diagnostics related to import resolution.
  • "reportMissingModuleSource": Controls the severity of diagnostics for missing source files of imported modules.
  • "reportOptionalCall": Controls the severity of diagnostics when calling something that might be None.
  • "reportOptionalIterable": Controls the severity of diagnostics when iterating on something that might be None.
  • "reportOptionalContextManager": Controls the severity of diagnostics when using something that might be None as a context manager.
  • "reportOptionalOperand": Controls the severity of diagnostics when operating on something that might be None.
  • "reportUntypedBaseClass": Controls the severity of diagnostics when a base class is untyped.
  • "reportUntypedNamedTuple": Controls the severity of diagnostics when a named tuple does not have types.
  • "reportPrivateUsage": Controls the severity of diagnostics when accessing a private member (i.e., begins with an underscore).
  • "reportConstantRedefinition": Controls the severity of diagnostics when a constant is redefined in a local scope.

Each of these options can be set to "error", "warning", "information", or "none" depending on your preference.

Upvotes: 0

Mack Raymond
Mack Raymond

Reputation: 182

Make sure there is a .py extension on the module.

Upvotes: 1

user11717481
user11717481

Reputation: 1602

If you have correctly installed Pylance when you open a .py file, this extension would be activate. Now check settings.json file and add the following sentence: "python.languageServer": "Default" or "Pylance".

 It is probably that your problem is caused because Pylance is not pointed to the path where is your library installed or another package and the error is originated (ModuleNotFoundError: No module named 'abc123'), then you may need to verify in your settings.json this configuration:

python.analysis.autoSearchPaths : true

python.analysis.stubPath:./typings

python.analysis.extraPaths:["./folder"] (if your project use a specific folder)

python.analysis.autoImportCompletions:true

Upvotes: 2

Greg Iven
Greg Iven

Reputation: 183

I found many similar questions, but not this specific answer.

In vscode, locate the file dropdown on the top toolbar.

Select preferences > Settings (Ctrl +)

In the search bar, search for pylance

Scroll down to Python > Analysis: Extra Paths

vscode pylance settings

Click 'add item' button

Next step you need to identify the installation path for python in windows. In that path there is a 'Lib' folder. Under lib folder there is a 'site-packages' folder. This is the folder for all your pip installed packages.

For me it looked like this:

C:\Users\user_name_here\AppData\Local\Programs\Python\Python310\Lib\site-packages

Copy this path and add it as the item in the pylance vscode settings.

Additionally: You can open the 'Python > Analysis: Indexing' settings below to edit a JSON file. Here you copy and paste the same path, with extra '' - backspace escape characters. Noting there is a comma after the first entry, if it exists.

Upvotes: 10

Related Questions