You can add the following settings in settings.json configuration file:
"python.analysis.diagnosticSeverityOverrides": { "reportUndefinedVariable": "none" }
Or you can search for python.analysis.diagnosticSeverityOverrides in the settings, click Add Item button to select "reportUndefinedVariable", "none":

Result:

You can add the following settings in settings.json configuration file:
"python.analysis.diagnosticSeverityOverrides": { "reportUndefinedVariable": "none" }
Or you can search for python.analysis.diagnosticSeverityOverrides in the settings, click Add Item button to select "reportUndefinedVariable", "none":

Result:

Following the issue that @Jill Cheng linked in the comments, the pylance devs suggest using a # type: ignore comment after the line in question. You will lose out on other linting abilities for this line though so apply with caution.
As a side note, if you're using this to quiet the messages due to "import <module> could not be resolved" then you should look into correctly configuring your workspace rather than overriding the message. Here's an example answer to help you solve that issue.
Is there a setting to turn off specific errors displayed by Pylance?
Suppress reportUnboundVariable for a variable or a code block
python - Pylance in VS Code reports undefined variable with import * - Stack Overflow
visual studio code - vscode python gettext : Pylance reports undefined variable '_' - Stack Overflow
I imported all functions from a python file util.py in a Jupyter notebook using from util import *.
util.py:
def zprint(
_text1 : str,
_text2 : str,
_tabSpace : int,
):
print(
(
_text1 +
'\t:' +
_text2
)
.expandtabs(_tabSpace)
)
def testFunc():
print("hello")Jupyter notebook cell:
from util import *
pwd = os.getcwd()
zprint('pwd', pwd, 20)
However, Pylance reports "zprint" is not defined Pylance(reportUndefinedVariable) on the zprint function when I use it in the notebook cell.
There is no such warning if I import the function specifically, as shown below.
from util import zprint
pwd = os.getcwd()
zprint('pwd', pwd, 20)=
May I ask how to make Pylance ignore such case when using import *?
I did find the following post on this issue, but I don't understand the suggested solution:
https://stackoverflow.com/questions/69509625/pylance-in-vs-code-reports-undefined-variable-with-import
Finally I found the answer:
...you may want to tell Pyright about these additional symbols that are available at runtime. To do so, you can add a local type stub file called
__builtins__.pyi
Inserting a file called __builtins__.pyi with the contents _ = str in the base of the project will be interpreted by pyright so that _ is evaluated as a global symbol of type str.
There is nothing wrong with pylance's report that it cannot detect that _ is defined by gettext.install() and therefore assumes it is an undefined variable.
You can indicate that this _ is a function that returns a translated string by adding a type hint
from typing import Callable
def _(s: str) -> str:
return s
You can add your variables to the 'additional-builtins' option so pylint will consider them as defined.
This has to be done in a rc file, it can't be done inlined in the code.
Disabling E0602 in the code:
# make pylint think that it knows about 'injected_var' variable
injected_var = injected_var # pylint:disable=invalid-name,used-before-assignment
Obviously, that needs to be done once per module, all occurrences of injected_var after this line would be legal for pylint.
That is a Pylance error.
You can create a pyrightconfig.json file at the root of your workspace and define the files to be exclude-d from analysis or completely ignore-d:
{
"ignore": [
"**/*.ipynb",
],
}
You can even list up specific filenames:
{
"ignore": [
"notimportant.ipynb",
"test.ipynb",
],
}
Historical Notes:
It initially didn't work for Jupyter Notebooks (.ipynb):
https://github.com/microsoft/pylance-release/issues/2135
This happens because pyright doesn't see the file as a "*.ipynb". The file is being preprocessed (to combine all of the cells) in the notebook by the VS Code Python extension, and the resulting combined file is then passed to pyright for analysis.
The pylance team is actively working on changing the way this works. I'm going to transfer this bug to the pylance-release repo so it gets the attention it deserves.
That Github issue has since been resolved the fix was deployed as part of pylance 2022.8.51: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2022851-31-august-2022-prerelease
Notable changes:
- ...
- Bug Fix: Ignoring *.ipynb files does not work (pylance-release#2135)
If it somehow still does not work, check the version of pylance on your VS Code.
Gino's solution doesn't work for me as of VSCode 1.88.1 + Pylance v2024.4.1.
What worked for me was adding the following to my pyproject.toml:
[tool.pyright]
exclude = ["**/*ipynb"]
insert this:
# type: ignore
paste it at the top of the file to ignore the file
paste it at the end of a line to ignore the line
Reference: https://www.reddit.com/r/VisualStudioCode/comments/i3mpct/comment/g5bkx9u/
Add this to your settings.json:
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingImports": "none"
}
So I'm trying not to rely on IDE's error checking and am wanting to disable linting entirely.
I include:
"python.linting.pylintEnabled": false,"python.linting.pylintUseMinimalCheckers": false,"python.linting.lintOnSave": false,"python.linting.enabled": false,
all in the settings.json file but even with these changed I am still getting error underlining in the editor.
Does anyone know how to get rid of all the error checking in the editor despite the language? I've heard that it's a good idea not to become reliant on IDE's for error checking and such so I would like to get rid of error checking entirely.
I'd also like to have it as an option that's easily accessible like the command >Python: Enable Linting: on/off but when I do that command nothing happens