what causes AttributeError?
AttributeError happens when a name that is NOT in an object (everything in Python is an object) is used.
what is an attribute?
An attribute is a name (variable?) for something that belongs to an object (a class), for example, a human being has attributes like height, weight, sex and color, they are also known as properties.
preview
I have these tests by the end of the chapter
questions about TypeError
Questions to think about as I go through the chapter
start the project
I name this project
attribute_errorI open a terminal
I change directory to the
attribute_errorfolder in thepumping_pythonfoldercd attribute_errorthe terminal shows
cd: no such file or directory: attribute_errorthere is no folder with the name
attribute_errorin this folder.I use uv to make a directory for the project and initialize it
uv init attribute_errorthe terminal shows
Initialized project `attribute-error` at `.../pumping_python/attribute_error`then goes back to the command line.
I change directory to the project
cd attribute_errorthe terminal shows I am in the
attribute_errorfolder.../pumping_python/attribute_errorI make a directory for the tests
mkdir teststhe terminal goes back to the command line.
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I make a Python file for the tests in the
testsdirectorytouch tests/test_attribute_error.pyNew-Item tests/test_attribute_error.pythe terminal goes back to the command line.
I use the mv program to change the name of
main.pytotest_attribute_error.pyand move it to thetestsfoldermv main.py tests/test_attribute_error.pyMove-Item main.py tests/test_attribute_error.pythe terminal goes back to the command line.
I open
test_attribute_error.pyI delete all the text then add the first failing test to
test_a.py1def test_failure(): 2 assert False is TrueI go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtthe terminal goes back to the command line.
I add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use uv to install pytest-watcher with the requirements file
uv add --requirement requirements.txtthe terminal shows that it installed pytest-watcher and its dependencies.
I add the new files and folder to git for tracking
git add .the terminal goes back to the command line.
I add a git commit message
git commit -am 'setup project'the terminal shows a summary of the changes then goes back to the command line.
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== FAILURES ======================== ______________________ test_failure ______________________ def test_failure(): > assert False is True E assert False is True test_attribute_error.py:2: AssertionError ================ short test summary info ================= FAILED test_attribute_error.py::test_failure - assert False is True =================== 1 failed in X.YZs ====================if the terminal does not show the same error, then check
if your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyif you ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
fix those errors and try to run
uv run pytest-watcher . --nowagainI add AssertionError to the list of Exceptions seen in
test_attribute_error.py1def test_failure(): 2 assert False is True 3 4 5# Exceptions seen 6# AssertionErrorI change True to False in the assertion
1def test_failure(): 2 # assert False is True 3 assert False is False 4 5 6# Exceptions seen 7# AssertionErrorthe test passes.
I add an import statement at the top of
test_attribute_error.py1import src.attribute_errorthe terminal is my friend, and shows ModuleNotFoundError
ModuleNotFoundError: No module named 'src'because Python cannot find anything named
srcin this project.I add ModuleNotFoundError to the list of Exceptions seen
9# Exceptions seen 10# AssertionError 11# ModuleNotFoundErrorI open a new terminal then change directories to
attribute_errorcd attribute_errorI use mkdir to make a folder named
srcin the projectmkdir srcI go back to the terminal where the tests are running
I go to
test_attribute_error.pyand use ctrl/command+s on the keyboard to run the test again. The terminal shows ModuleNotFoundErrorModuleNotFoundError: No module named 'src.attribute_error'because there is nothing named
attribute_errorin thesrcfolderI go to the second terminal
I use touch to make
attribute_error.pyin thesrcfoldertouch src/attribute_error.pyI go to the terminal where the tests are running and it shows the test is green again.
test_attribute_error_w_variables
AttributeError happens when I use a variable that does not exist in an object.
RED: make it fail
I change test_failure to
test_attribute_error_w_variables1import src.attribute_error 2 3 4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 7 8# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_00'because when
src.attribute.variable_00runs, Python follows this pathsrc └── attribute_error.py └── variable_00 # does not exist in attribute_error.pywhich raises AttributeError since there is nothing named
variable_00inattribute_error.pyin thesrcfolder, it is empty.I add AttributeError to the list of Exceptions seen in
test_attribute_error.py8# Exceptions seen 9# AssertionError 10# ModuleNotFoundError 11# AttributeError
GREEN: make it pass
I open
attribute_error.pyfrom thesrcfolderI add
variable_00toattribute_error.py1variable_00the terminal is my friend, and shows NameError
NameError: name 'variable_00' is not definedbecause I used a name that is not defined in this file
I add NameError to the list of Exceptions seen in
test_attribute_error.py8# Exceptions seen 9# AssertionError 10# ModuleNotFoundError 11# AttributeError 12# NameErrorI point
variable_00to None to define it, inattribute_error.py1# variable_00 2variable_00 = Nonethe test passes because
When
import src.attribute_errorruns, Python brings in an object for theattribute_error.pyfile from thesrcfolder so I can use it intest_attribute_error.pyassrc.attribute_error.When
src.attribute_error.variable_00runs, Python looks at what thevariable_00variable from the object it imported for theattribute_error.pyfile from thesrcfolder (src.attribute_error) points to.
I think of
src.attribute_error.variable_00like an addresssrc └── attribute_error.py └── variable_00 = Nonevariable_00is something inattribute_error, in this case it is a variable inattribute_errorattribute_erroris something insrc, in this case it isattribute_error.py(a module) in thesrcfoldersrcis something Python can import (a module, Python package or folder)variable_00is now an attribute/property ofattribute_error.pyin thesrcfolder. I can use it from outside the file withsrc.attribute_error.variable_00
REFACTOR: make it better
I add a statement for
variable_01totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 8 9# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_01'. Did you mean: 'variable_00'?I add
variable_01toattribute_error.py1# variable_00 2variable_00 = None 3variable_01the terminal is my friend, and shows NameError
NameError: name 'variable_01' is not definedI point it to None to define it
1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = Nonethe test passes because
variable_01is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_01.I add a statement for
variable_02totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 9 10# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_02'. Did you mean: 'variable_00'?I add
variable_02and point it to None inattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = Nonethe test passes because
variable_02is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_02.I add a line for
variable_03totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 10 11# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_03'. Did you mean: 'variable_00'?I add
variable_03toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = Nonethe test passes because
variable_03is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_03.I add a line for
variable_04totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 11 12# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_04'. Did you mean: 'variable_00'?I add
variable_04toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = None 7variable_04 = Nonethe test passes because
variable_04is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_04.I add a line for
variable_05totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 12 13# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_05'. Did you mean: 'variable_00'?I add
variable_05toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = None 7variable_04 = None 8variable_05 = variable_04the test passes because
variable_05is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_05.I add a line for
variable_06totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 13 14# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_06'. Did you mean: 'variable_00'?I add
variable_06toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = None 7variable_04 = None 8variable_05 = variable_04 9variable_06 = variable_05the test passes because
variable_06is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_06.I add a line for
variable_07totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 src.attribute_error.variable_07 13 14 15# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_07'. Did you mean: 'variable_00'?I add
variable_07toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = None 7variable_04 = None 8variable_05 = variable_04 9variable_06 = variable_05 10variable_07 = variable_06the test passes because
variable_07is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_07.I add a line for
variable_08totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 src.attribute_error.variable_07 13 src.attribute_error.variable_08 14 15 16# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_08'. Did you mean: 'variable_00'?I add
variable_08toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = None 7variable_04 = None 8variable_05 = variable_04 9variable_06 = variable_05 10variable_07 = variable_06 11variable_08 = variable_07the test passes because
variable_08is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_08.I add a line for
variable_09totest_attribute_error.py4def test_attribute_error_w_variables(): 5 src.attribute_error.variable_00 6 src.attribute_error.variable_01 7 src.attribute_error.variable_02 8 src.attribute_error.variable_03 9 src.attribute_error.variable_04 10 src.attribute_error.variable_05 11 src.attribute_error.variable_06 12 src.attribute_error.variable_07 13 src.attribute_error.variable_08 14 src.attribute_error.variable_09 15 16 17# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'variable_09'. Did you mean: 'variable_00'?I add
variable_09toattribute_error.py1# variable_00 2variable_00 = None 3# variable_01 4variable_01 = None 5variable_02 = None 6variable_03 = None 7variable_04 = None 8variable_05 = variable_04 9variable_06 = variable_05 10variable_07 = variable_06 11variable_08 = variable_07 12variable_09 = variable_08the test passes because
variable_09is now an attribute ofattribute_error.pyin thesrcfolder, and I can use it from outside the file withsrc.attribute_error.variable_09.I remove the commented lines
1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = variable_04 7variable_06 = variable_05 8variable_07 = variable_06 9variable_08 = variable_07 10variable_09 = variable_08I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_variables'
test_attribute_error_w_functions
RED: make it fail
I go back to the terminal where the tests are running
I add a test for functions to test_attribute_error.py
15 src.attribute_error.variable_07
16 src.attribute_error.variable_08
17 src.attribute_error.variable_09
18
19 def test_attribute_error_w_functions():
20 src.attribute_error.function_00()
21
22
23# Exceptions seen
the terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error'
has no attribute 'function_00'
GREEN: make it pass
I add the name and point it to None in
attribute_error.py1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = None 9variable_08 = None 10variable_09 = None 11 12 13function_00 = Nonethe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause I cannot call None like a function.
I add TypeError to the list of Exceptions seen in
test_attribute_error.py23# Exceptions seen 24# AssertionError 25# AttributeError 26# NameError 27# TypeErrorI change the variable to a function in
attribute_error.py13# function_00 = None 14def function_00(): 15 return Nonethe test passes because
function_00is now an attribute/property ofattribute_error.pyin thesrcfolderI can call it from outside the file with
src.attribute_error.function_00()
REFACTOR: make it better
I remove the commented line
1variable_00 = None 2variable_01 = None 3variable_02 = None 4variable_03 = None 5variable_04 = None 6variable_05 = None 7variable_06 = None 8variable_07 = None 9variable_08 = None 10variable_09 = None 11 12 13def function_00(): 14 return Nonetime to make it a drill.
I add a call to
src.attribute_error.function_01in test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 23 24# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_01'. Did you mean: 'function_00'?I add the function to
attribute_error.py13def function_00(): 14 return None 15 16 17def function_01(): 18 return Nonethe test passes.
I add a line for
src.attribute_error.function_02to test_attribute_error_w_functions intest_attribute_error.py13 def test_attribute_error_w_functions(): 14 src.attribute_error.function_00() 15 src.attribute_error.function_01() 16 src.attribute_error.function_02() 17 18 19# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_02'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py17def function_01(): 18 return None 19 20 21def function_02(): 22 return Nonethe test passes.
I add a line for
src.attribute_error.function_03to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 25 26# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_03'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py21def function_02(): 22 return None 23 24 25def function_03(): 26 return Nonethe test passes.
I add a line for
src.attribute_error.function_04to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 26 27# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_04'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py25def function_03(): 26 return None 27 28 29def function_04(): 30 return Nonethe test passes.
I add a line for
src.attribute_error.function_05to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 27 28# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_05'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py29def function_04(): 30 return None 31 32 33def function_05(): 34 return Nonethe test passes.
I add a line for
src.attribute_error.function_06to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 28 29# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_06'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py33def function_05(): 34 return None 35 36 37def function_06(): 38 return Nonethe test passes.
I add a line for
src.attribute_error.function_07to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 src.attribute_error.function_07() 28 29 30# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_07'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py37def function_06(): 38 return None 39 40 41def function_07(): 42 return Nonethe test passes.
I add a line for
src.attribute_error.function_08to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 src.attribute_error.function_07() 28 src.attribute_error.function_08() 29 30 31# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_08'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py41def function_07(): 42 return None 43 44 45def function_08(): 46 return Nonethe test passes.
I add a line for
src.attribute_error.function_09to test_attribute_error_w_functions intest_attribute_error.py19 def test_attribute_error_w_functions(): 20 src.attribute_error.function_00() 21 src.attribute_error.function_01() 22 src.attribute_error.function_02() 23 src.attribute_error.function_03() 24 src.attribute_error.function_04() 25 src.attribute_error.function_05() 26 src.attribute_error.function_06() 27 src.attribute_error.function_07() 28 src.attribute_error.function_08() 29 src.attribute_error.function_09() 30 31 32# Exceptions seenthe terminal is my friend, and shows AttributeError
AttributeError: module 'src.attribute_error' has no attribute 'function_09'. Did you mean: 'function_00'?I add a function for it in
attribute_error.py45def function_08(): 46 return None 47 48 49def function_09(): 50 return Nonethe test passes.
I add a git commit message in the other terminal
git commit -am \ 'add test_attribute_error_w_functions'
close the project
I close
attribute_error.pyandtest_attribute_error.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
attribute_errorcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I ran tests for AttributeError with
I also saw these Exceptions
My problem with the tests is that they all show the correct way to use attributes I made in attribute_error.py. If someone reads the file or runs it, there is no way for them to know how the code relates to AttributeError unless they go through the process with me, there has to be a better way.
code from the chapter
what is next?
You know
Would you like to test using a class to remove repetition of inputs from functions?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.