Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VisualStudio+ Python -- how to handle "overriding stdlib module" Pylance(reportShadowedImports) warning?

When running ipynbs in VS Code, I've started noticing Pylance warnings on standard library imports. I am using a conda virtual environment, and I believe the warning is related to that. An example using the glob library reads:

"env\Lib\glob.py" is overriding the stdlib "glob" modulePylance(reportShadowedImports)

So far my notebooks run as expected, but I am curious if this warning is indicative of poor layout or is just stating the obvious more of an "FYI you are not using the base install of python".

I have turned off linting and the problem stills persists. And almost nothing returns from my searches of the error "reportShadowedImports".

like image 807
Garrett Avatar asked Nov 25 '25 22:11

Garrett


2 Answers

The reason you find nothing by searching is because this check has just been implemented recently (see Github). I ran into the same problem as you because code.py from Micropython/Circuitpython also overrides the module "code" in stdlib.

The solution is simple, though you then loose out on this specific check. Just add reportShadowedImports to your pyright config. For VS Code, that would be adding it to .vscode/settings.json:

{
  "python.languageServer": "Pylance",
  [...]
  "python.analysis.diagnosticSeverityOverrides": {
      "reportShadowedImports": "none"
  },
  [...]
}
like image 121
WhatIsName Avatar answered Nov 27 '25 12:11

WhatIsName


If your filename has the same name as an stdlib python module - for example "datetime.py" - it's gonna have this problem.

It is good practice to avoid naming files the same names as stdlib modules, so in this case, the best action is to choose a different name.

like image 30
George_Tsamis Avatar answered Nov 27 '25 10:11

George_Tsamis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!