Reputation: 183
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
Reputation: 520
Try this:
pylance
Python> Analysis: Diagnostic Severity Overrides
Edit in settings.json
"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
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
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
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