Think of a more complex function where n
is bound depending on some condition, or not. You don't have to del
the name in question, it also happens if the compiler sees an assignment, so the name is local, but the code path is not taken and the name gets never assigned anything. Another stupid example:
def f():
def g(x):
return x * n
if False:
n = 10
return g
Most likely, you are assigning to re (presumably inadvertently) at some point below line 561, but in the same function. This reproduces your error:,Think of a more complex function where n is bound depending on some condition, or not. You don't have to del the name in question, it also happens if the compiler sees an assignment, so the name is local, but the code path is not taken and the name gets never assigned anything. Another stupid example:,best practice to design table schema and/or index for multiple composite index,Azure command-line task - What is the difference between 'Fail on standard error' and 'Continue on error (unchecked)'?
ch = 'a a b c d'
words = set(ch.split(' '))
d = {
mot: ch.count(mot) for mot in words
}
print(d)
def f():
def g(x):
return x * n
if False:
n = 10
return g
import re
def main():
term = re.compile("foo")
re = 0
main()
button4 = Button(user, text = "Export", command = save1())
button4 = Button(user, text = "Export", command = save1)
myvar = tr_list[8].css('td ::text').extract()
item['myvar'] = [
n
for i in myvar
if len(n: = re.sub(PATTERN, "", i).strip()) > 0
]
global ar
ar = tf.cond(tf.less(arg[i], 1), lambda: tf.concat([ar, [1.0]], axis = 0), lambda: tf.concat([ar, [0.0]], axis = 0))
NameError Annotated variable Custom name Free variable referenced Generic Missing import Synonym ,Friendly aims to provide friendlier feedback when an exception is raised than what is done by Python. Below, we can find some examples. SyntaxError cases, as well as TabError and IndentationError cases, are shown in a separate page. Not all cases handled by friendly are included here.,KeyError ChainMap Forgot to convert to string Generic key error Popitem empty ChainMap Popitem empty dict Similar names String by mistake ,TypeError Bad type for unary operator Builtin has no len Can only concatenate Cannot convert dictionary update sequence Cannot multiply by non int Cannot unpack non iterable object Comparison not supported Derive from BaseException Indices must be integers or slices Not an integer Not callable Object is not iterable Object is not subscriptable Slice indices must be integers or None Too few positional argument Too many positional argument Tuple no item assignment Unhachable type Unsupported operand types function has no len
Traceback (most recent call last):
File "TESTS:\runtime\test_arithmetic_error.py", line 9, in test_Generic
raise ArithmeticError('error')
ArithmeticError: error
`ArithmeticError` is the base class for those built-in exceptions
that are raised for various arithmetic errors.
It is unusual that you are seeing this exception;
normally, a more specific exception should have been raised.
Exception raised on line 9 of file TESTS:\runtime\test_arithmetic_error.py.
7: # Usually, a subclass such as ZeroDivisionError, etc., would
8: # likely be raised.
--> 9: raise ArithmeticError('error')
10: except ArithmeticError as e:
ArithmeticError: <class ArithmeticError>
Traceback (most recent call last):
File "TESTS:\runtime\test_assertion_error.py", line 8, in test_Generic
raise AssertionError("Fake message")
AssertionError: Fake message
In Python, the keyword `assert` is used in statements of the form
`assert condition`, to confirm that `condition` is not `False`,
nor equivalent to `False` such as an empty list, etc.
If `condition` is `False` or equivalent, an `AssertionError` is raised.
Exception raised on line 8 of file TESTS:\runtime\test_assertion_error.py.
6: # We raise it explicitly, rather than with the keyword assert, since
7: # we don't want pytest to rewrite out test.
--> 8: raise AssertionError("Fake message")
9: except AssertionError as e:
AssertionError: <class AssertionError>
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 299, in test_Attribute_from_other_module
keyword.pi
AttributeError: module 'keyword' has no attribute 'pi'
Did you mean one of the following modules: `math, cmath`?
An `AttributeError` occurs when the code contains something like
`object.x`
and `x` is not a method or attribute (variable) belonging to `object`.
Instead of the module `keyword`, perhaps you wanted to use
the attribute `pi` of one of the following modules:
`math, cmath`.
Exception raised on line 299 of file TESTS:\runtime\test_attribute_error.py.
297: import cmath
298: try:
-->299: keyword.pi
300: except AttributeError as e:
keyword: <module keyword> from PYTHON_LIB:\keyword.py
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 217, in test_Builtin_function
len.text
AttributeError: 'builtin_function_or_method' object has no attribute 'text'
Did you mean `len(text)`?
An `AttributeError` occurs when the code contains something like
`object.x`
and `x` is not a method or attribute (variable) belonging to `object`.
`len` is a function. Perhaps you meant to write
`len(text)`
Exception raised on line 217 of file TESTS:\runtime\test_attribute_error.py.
215: text = 'Hello world!'
216: try:
-->217: len.text
218: except AttributeError as e:
text: 'Hello world!'
len: <builtin function len>
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 234, in test_Builtin_module_with_no_file
sys.foo
AttributeError: module 'sys' has no attribute 'foo'
An `AttributeError` occurs when the code contains something like
`object.x`
and `x` is not a method or attribute (variable) belonging to `object`.
Python tells us that no object with name `foo` is
found in module `sys`.
Exception raised on line 234 of file TESTS:\runtime\test_attribute_error.py.
232:
233: try:
-->234: sys.foo
235: except AttributeError as e:
sys: <module sys (builtin)>
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 329, in test_Circular_import
import my_turtle1
File "TESTS:\my_turtle1.py", line 4, in <module>
a = my_turtle1.something
AttributeError: partially initialized module 'my_turtle1' has no attribute 'something' (most likely due to a circular import)
Did you give your program the same name as a Python module?
An `AttributeError` occurs when the code contains something like
`object.x`
and `x` is not a method or attribute (variable) belonging to `object`.
I suspect that you used the name `my_turtle1.py` for your program
and that you also wanted to import a module with the same name
from Python's standard library.
If so, you should use a different name for your program.
Execution stopped on line 329 of file TESTS:\runtime\test_attribute_error.py.
327: stdlib_modules.names.append("my_turtle1")
328: try:
-->329: import my_turtle1
330: except AttributeError as e:
Exception raised on line 4 of file TESTS:\my_turtle1.py.
2: import my_turtle1
3:
-->4: a = my_turtle1.something
^^^^^^^^^^^^^^^^^^^^
my_turtle1: <module my_turtle1> from TESTS:\my_turtle1.py
NameError Annotated variable Custom name Free variable referenced Generic Missing import Missing module name Missing self 1 Missing self 2 Synonym missing import2 missing import3 ,Free variable referenced,Below, we can find some examples. SyntaxError cases, as well as TabError and IndentationError cases, are shown in a separate page. Not all cases handled by friendly are included here.,Friendly tiene como objetivo proporcionar comentarios más amigables cuando se produce una excepción se plantea que lo que hace Python (sentence translated by Google Translate).
Traceback (most recent call last):
File "TESTS:\runtime\test_arithmetic_error.py", line 9, in test_Generic
raise ArithmeticError('error')
ArithmeticError: error
`ArithmeticError` es la clase base para aquellas excepciones integradas al lenguaje
que se producen por diversos errores aritméticos.
Es inusual que veas esta excepción;
normalmente, una excepción más específica debería haber sido lanzada.
Excepción elevada en la linea 9 del archivo TESTS:\runtime\test_arithmetic_error.py.
4: def test_Generic():
5: try:
6: # I am not aware of any way in which this error is raised directly
7: # Usually, a subclass such as ZeroDivisionError, etc., would
8: # likely be raised.
--> 9: raise ArithmeticError('error')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10: except ArithmeticError as e:
ArithmeticError: <class ArithmeticError>
Traceback (most recent call last):
File "TESTS:\runtime\test_assertion_error.py", line 8, in test_Generic
raise AssertionError("Fake message")
AssertionError: Fake message
En Python, la palabra clave `assert` se utiliza en sentencias de la forma
`assert condition`, para confirmar que `condition` no es `False`,
ni equivalente a `False`, como una lista vacía, etc.
Si `condición` es `False` o equivalente, se produce un `AssertionError`.
Excepción elevada en la linea 8 del archivo TESTS:\runtime\test_assertion_error.py.
4: def test_Generic():
5: try:
6: # We raise it explicitly, rather than with the keyword assert, since
7: # we don't want pytest to rewrite out test.
-->8: raise AssertionError("Fake message")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9: except AssertionError as e:
AssertionError: <class AssertionError>
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 325, in test_Attribute_from_other_module
keyword.pi
AttributeError: module 'keyword' has no attribute 'pi'
¿Querías utilizar uno de los módulos: `math, cmath`?
Un `AttributeError` ocurre cuando el código contiene algo como
objeto.x
y `x` no es un método o atributo (variable) perteneciente al `objeto`.
En lugar del modulo `keyword`, quizás querías usar el
atributo `pi` de uno de los siguientes módulos:
`math, cmath.
Excepción elevada en la linea 325 del archivo TESTS:\runtime\test_attribute_error.py.
321: assert "Did you mean `math`?" in result
322:
323: import cmath
324: try:
-->325: keyword.pi
^^^^^^^^^^
326: except AttributeError as e:
keyword: <module keyword> from PYTHON_LIB:\keyword.py
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 223, in test_Builtin_function
len.text
AttributeError: 'builtin_function_or_method' object has no attribute 'text'
¿Querías decir `len(text)`?
Un `AttributeError` ocurre cuando el código contiene algo como
objeto.x
y `x` no es un método o atributo (variable) perteneciente al `objeto`.
`len` es una función. Quizás quisiste escribir
`len(text)`
Excepción elevada en la linea 223 del archivo TESTS:\runtime\test_attribute_error.py.
220: def test_Builtin_function():
221: text = 'Hello world!'
222: try:
-->223: len.text
^^^^^^^^
224: except AttributeError as e:
text: 'Hello world!'
len: <builtin function len>
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 240, in test_Builtin_module_with_no_file
sys.foo
AttributeError: module 'sys' has no attribute 'foo'
Un `AttributeError` ocurre cuando el código contiene algo como
objeto.x
y `x` no es un método o atributo (variable) perteneciente al `objeto`.
Python nos indica que no se encuentra ningún objeto con nombre `foo` en
el módulo `sys`.
Excepción elevada en la linea 240 del archivo TESTS:\runtime\test_attribute_error.py.
236: """Issue 116"""
237: import sys
238:
239: try:
-->240: sys.foo
^^^^^^^
241: except AttributeError as e:
sys: <module sys (builtin)>
Traceback (most recent call last):
File "TESTS:\runtime\test_attribute_error.py", line 359, in test_Circular_import
import my_turtle1
File "TESTS:\my_turtle1.py", line 4, in <module>
a = my_turtle1.something
AttributeError: partially initialized module 'my_turtle1' has no attribute 'something' (most likely due to a circular import)
¿Has dado a tu programa el mismo nombre que un módulo de Python?
Un `AttributeError` ocurre cuando el código contiene algo como
objeto.x
y `x` no es un método o atributo (variable) perteneciente al `objeto`.
Sospecho que has utilizado el nombre `my_turtle1.py` para tu programa
y que también querías importar un módulo con el mismo nombre
de la biblioteca estándar de Python.
Si es así, deberías usar un nombre diferente para tu programa.
La ejecución se detuvo en la linea 359 del archivo TESTS:\runtime\test_attribute_error.py.
356: from friendly_traceback.runtime_errors import stdlib_modules
357: stdlib_modules.names.append("my_turtle1")
358: try:
-->359: import my_turtle1
^^^^^^^^^^^^^^^^^
360: except AttributeError as e:
Excepción elevada en la linea 4 del archivo TESTS:\my_turtle1.py.
1: """To test attribute error of partially initialized module."""
2: import my_turtle1
3:
-->4: a = my_turtle1.something
^^^^^^^^^^^^^^^^^^^^
my_turtle1: <module my_turtle1> from TESTS:\my_turtle1.py