Report undefined variable python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined Variable error when importing modules/functions in a function and defining them global #1154

Undefined Variable error when importing modules/functions in a function and defining them global #1154

Comments

Environment data

  • VS Code version: 1.55.1
  • Extension version (available under the Extensions sidebar): v2021.3.680753044
  • OS and version: Windows 10 20H2
  • Python version : 3.7.6
  • Type of virtual environment used : N/A
  • Relevant/affected Python packages and their versions: —
  • Relevant/affected Python-related VS Code extensions and their versions: —
  • Value of the python.languageServer setting: Pylance
Читайте также:  Java calendar add seconds

Expected behaviour

Pylance not saying there is an Undefined-variable error

Actual behaviour

Well I get almost 100 errors about mentionning an apparently not defined function

Steps to reproduce:

Here is a sample code that triggers this bug

def importFunc() : global math try : import math except ImportError : print("There was a fatal import error"); exit() if __name__ == '__main__' : importFunc() math.sqrt(4) # Here pylance should underline in red saying math is undefined variable

Note : same would happen if instead I did global sqrt; from math import sqrt

Picture

image

Logs

Output for Python in the Output panel ( View → Output , change the drop-down the upper-right of the Output panel to Python )

> ~\AppData\Local\Programs\Python\Python37\python.exe c:\Users\cleme\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\pyvsc-run-isolated.py pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template=',,,:' --reports=n --output-format=text "c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python\test.py" cwd: c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python > ~\AppData\Local\Programs\Python\Python37\python.exe c:\Users\cleme\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\pyvsc-run-isolated.py pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template=',,,:' --reports=n --output-format=text "c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python\test.py" cwd: c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python ##########Linting Output - pylint########## ------------------------------------ Your code has been rated at 10.00/10 > ~\AppData\Local\Programs\Python\Python37\python.exe c:\Users\cleme\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\pyvsc-run-isolated.py pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template=',,,:' --reports=n --output-format=text "c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python\test.py" cwd: c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python > ~\AppData\Local\Programs\Python\Python37\python.exe c:\Users\cleme\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\pyvsc-run-isolated.py pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template=',,,:' --reports=n --output-format=text "c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python\test.py" cwd: c:\Users\cleme\OneDrive\Sauvegarde PC-STDO\Desktop\Documents\Python ##########Linting Output - pylint########## ************* Module test 8,4,error,undefined-variable:Undefined variable 'math' ------------------------------------------------------------------- Your code has been rated at 5.00/10 (previous run: 10.00/10, -5.00) 

The text was updated successfully, but these errors were encountered:

Источник

How to Solve an Undefined Variable NameError in Python

How to Solve an Undefined Variable NameError in Python

In Python, a NameError: name ‘x’ is not defined error is raised when the program attempts to access or use a variable that has not been defined or assigned a value. This can happen if the variable is spelled incorrectly, or if it is accessed before it has been defined.

What Causes Undefined Variable

In Python, a variable is not created until a value is assigned to it. If an attempt is made to use a variable before it is defined, a NameError: name ‘x’ is not defined error is thrown.

The error message typically includes the name of the variable that is causing the problem and the line of code where the error occurred.

Python Undefined Variable Example

Here’s an example of a Python NameError: name ‘x’ is not defined thrown when using an undefined variable:

In this example, an undefined variable x is used in the range() function, throwing the NameError: name ‘x’ is not defined error:

Traceback (most recent call last): File "test.py", line 1, in for i in range(x): NameError: name 'x' is not defined 

How to Solve Undefined Variable in Python

To solve the NameError: name ‘x’ is not defined error in Python, you need to make sure that the variable is properly defined and assigned a value before it is used. The variable should also be referenced correctly, with the correct case and spelling.

The earlier example can be updated to define the variable before it is used:

x = 5 for i in range(x): print(i) 

Here, x is defined by being assigned a value before it is used in the range() function. The above code executes successfully, producing the correct output as expected:

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Python errors easier than ever. Try it today!

Источник

Оцените статью