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

4

20

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".

Mither answered 2/12, 2022 at 18:40 Comment(0)
T
36

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"
  },
  [...]
}
Tetratomic answered 4/12, 2022 at 12:27 Comment(4)
This seems to conflict with the idea of a conda env. It seems like when I'm using a conda env and its packages, then there should be no errors reported. Is there a way to make pylance consider stdlibs to be those in the conda env being used?Muzzle
By the way to found the settings.json file, search these locations: » Windows: %APPDATA%\Code\User\settings.json </br> » macOS: $HOME/Library/Application\ Support/Code/User/settings.json </br> » Linux: $HOME/.config/Code/User/settings.jsonCapua
An easier solution is to rename your code.py to main.py so that it doesn't conflict. It will still be loaded on boot. Disabling all override warnings has the unwanted side-effect of removing useful warnings.Freshman
@Stefan: Your comment seems to me to be the better solution. Thanks. Maybe you set it as a separate answer.Chorale
A
8

If your filename has the same name as a 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 that case, the best action is to choose a different name.

Asternal answered 26/3, 2023 at 19:44 Comment(0)
C
4

Open the User settings in VSCode:

  • Open the command palette (either with F1 or Ctrl+Shift+P)
  • Type "open settings"
  • You are presented with a few options¹, choose Open User Settings (JSON)

enter image description here

This error may be due to a recognition error caused by python created by venv being located in the workspace folder.

Please note if your venv is in your project folder just exclude it with below settings (or replace venv with whatever is your virtual environment name).

settings.json

"python.analysis.exclude": ["venv"]
Colton answered 19/2, 2024 at 7:58 Comment(0)
S
0

I had the same issue while importing an API_key that I had stored in the secrets.py file. just change the name of the file and it should work just fine.

Selection answered 6/8, 2024 at 13:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.