The Testing Academy · Class Notes Sunday, 15 November (IST)
Live class · study guide

Python exceptions, the os module, packages and pytest

Exceptions with try, except and finally, the order of excepts and custom exceptions, reading a file, the os module and environment variables, packages, pip and uv, and the first pytest tests. Files 159 to 179.

By Pramod Dutta, The Testing Academy. Study notes from the AI Tester Blueprint 4x class, built from the session recording, the class's files 159 to 179 and the 15 November section of the batch's Eraser notes deck. An os module cheat sheet was pushed to the batch repository at the end of the class and the exercises 35 minutes later, with README notes that the page credits where it uses them. Every file was run with Python 3.12 and pytest 9.1, and the output shown is what it prints. File 170 was run only against addresses on the same machine, not against the address typed in class, and uv add, which downloads packages, was not run.

Hides every output until you tap Reveal, so you can predict it first.
01

What this class covered

  • Exceptions, and the errors of files 159 to 164
  • try, except and finally, files 165 to 169
  • Catch order and custom exceptions, files 170 to 172
  • Reading a file, file 173
  • The os module, files 174 and 175
  • Environment variables, file 176
  • Packages, file 177
  • pip and uv
  • pytest basics, test_178 and test_179
  • Tasks and announcements: exercises up to 179 on your GitHub, CrewAI with pytest next week, a hackathon
  • In Python, an exception is an error that happens while the program runs: Python makes no difference between an error and an exception, while Java does.
  • Put only the lines that can fail inside try, and catch an error by its class: one except per error, or a tuple of them. finally runs every time.
  • Python takes the first except that matches, so the biggest one, except Exception, goes last.
  • raise throws an error yourself, and a custom exception is a class that inherits Exception.
  • The os module talks to the operating system: getcwd(), listdir(), makedirs(..., exist_ok=True), remove(), path.join(), path.exists(). os.getenv() gives None for a variable that is not set.
  • A folder with __init__.py is a package you can import from. pip installs packages; uv does it much faster, after uv init.
  • pytest runs the test_ functions in test_ files: each assert compares expected with actual, a dot is a pass and F a failure.
02

Exceptions: files 159 to 164

WHO IS THE PARENT: THE ERRORS OF FILES 159 TO 173 BaseException KeyboardInterrupt beside Exception, not under it Exception the parent: except Exception catches every error below NameError file 159 ArithmeticError ZeroDivisionError files 160, 165 to 169 TypeError file 161 ValueError files 162, 166 to 169 LookupError IndexError file 163 SyntaxError file 164 OSError FileNotFoundError file 173 InvalidAgeException file 172, the class's own A parent catches all its children, so the biggest except goes last.
Each line is the class's real parent, as Python's __bases__ gives it, and each error is the one the named file raises or catches when it runs.

Last class covered inheritance, polymorphism, abstraction and static, files 139 to 158. Today, the instructor said, the class would complete its Python topics: exceptions, the os module, file I/O and a couple of important concepts of pytest. Exceptions are very, very important if you come from a Java background, and important for AI agents, MCPs and RAG too, the instructor said. The deck's definition, which the instructor read out almost word for word:

Exceptions in Python are events that occur during the execution of a program that disrupt the normal flow of instructions.

a way to handle errors and unexpected situations gracefully.

An exception is an error that happens while a program is running, the instructor said, and this is the biggest difference from Java: in Java, an error is different and an exception is different, while in Python there is no differentiation between the two. If it is not handled, the program stops, or creates a problem. It is a runtime problem, not a syntax problem.

Python's list of exceptions is huge, the instructor said: there are base exceptions, then keyboard exceptions, and we are more interested in Exception; then there are AttributeError and ArithmeticError. The figure shows the part of that tree this class used, as Python itself has it. The common built-in ones, which are very popular, the instructor listed: ValueError, TypeError, ZeroDivisionError, IndexError, KeyError and FileNotFoundError.

One line, one error: files 159 to 163. If you try to use something which does not exist, a NameError comes, the instructor said: the simplest one. Each file keeps its error in a comment, and each run stops with it.

159_Exceptions.pyPython
print(a)
# NameError: name 'a' is not defined
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/159_Exceptions.py", line 1, in <module>
    print(a)
          ^
NameError: name 'a' is not defined

Question, file 160. If I do 10 divided by 0, is this correct syntax-wise?

Show the answer

Answer. Absolutely correct, the instructor said, but you will get a ZeroDivisionError, because you cannot divide by 0. The line is valid Python, and it fails only when it runs, as the output below shows.

160_E1.pyPython
print(10/0)
# ZeroDivisionError
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/160_E1.py", line 1, in <module>
    print(10/0)
          ~~^~
ZeroDivisionError: division by zero

Question, file 161. What happens when you try to concatenate an integer with a string?

Show the answer

Answer. Run it, and you will see a TypeError, the instructor said: not an arithmetic exception, as a learner suggested, but a type error, because an integer and a string cannot be added. Line 1 stops with TypeError: unsupported operand type(s) for +: 'int' and 'str'.

161_Ex.pyPython
print( 1 + "1")
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/161_Ex.py", line 1, in <module>
    print( 1 + "1")
           ~~^~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Converting the letter a to an integer does not make sense, so you get a ValueError:

162.pyPython
print(int("a"))
# ValueError: invalid literal for int() with base 10: 'a'
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/162.py", line 1, in <module>
    print(int("a"))
          ^^^^^^^^
ValueError: invalid literal for int() with base 10: 'a'

The list has three elements, and line 2 asks for index 4: you get an IndexError, automatically.

163.pyPython
my_list = [1, 2, 3]
print(my_list[4])
# IndexError: list index out of range
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/163.py", line 2, in <module>
    print(my_list[4])
          ~~~~~~~^^^
IndexError: list index out of range

A syntax error: file 164. This one is invalid syntax, the instructor said: you cannot have a syntax like this, while True and a print, in a single line.

164.pyPython
while True print("Hello World!")
SyntaxError: invalid syntax
Output
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/164.py", line 1
    while True print("Hello World!")
               ^^^^^
SyntaxError: invalid syntax

There is no Traceback line this time: Python reads the whole file before it runs any of it, finds line 1 invalid and stops, so line 2, the message pasted without a #, is never reached. A SyntaxError cannot be caught at all, as the README notes point out, because the file never starts.

Question. What is the difference between an error and an exception?

Show the answer

Answer. In Python, there is no difference, the instructor answered a learner, and it was the first thing taught in this class: Python does not differentiate between an error and an exception. In Java, an error is one thing and an exception another.

03

try, except and finally: files 165 to 169

A hand-drawn sketch on warm paper titled Python Exceptions. A box labelled try, with a page of code, has an arrow to a box labelled except, where a net catches a falling bug, with four tags beside it: ZeroDivisionError, ValueError, TypeError and FileNotFoundError. An arrow runs on to a box labelled finally, with a broom and the words always runs. A second arrow, labelled no error, curves over from try straight to finally. At the bottom, a tag labelled raise, with an upward arrow and the words your own exception.
The README's sketch of this chapter, from the class's repo: try, except, finally, and raise.

The basic way to handle an exception is try and except, and this is very, very different from Java, the instructor said: Java has try, catch and finally, while Python has try and, instead of catch, except. Like multiple catches in Java, you can have multiple excepts, and you can raise a custom exception with the raise keyword. There is a finally too, plus one more block that a lot of people have difficulty digesting: else. Try this; if it does not work, except is called; if it works, else is executed; and finally is always called. This is like a tree, the instructor said: a very strange syntax, but a useful and meaningful one. None of this class's files uses else.

The instructor's best practices: catch a specific exception, not the base one; keep the try block small; log or print helpful messages; use finally for cleanup; raise clear exceptions; do not hide a real bug silently; and avoid except Exception: pass.

The program: file 165. This program looks absolutely fine, the instructor said: it reads two numbers and divides them. Run in a terminal with 10 and 10, then with 10 and 0, it prints the two outputs below. The prompts have no space after them, so what you type runs straight on: Enter num 1, then 10.

165.pyPython
a = int(input("Enter num 1"))
b = int(input("Enter num 2"))
c = a/b
print(c)
# ZeroDivisionError: division by zero
Output
Enter num 110
Enter num 210
1.0
Output
Enter num 110
Enter num 20
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/165.py", line 3, in <module>
    c = a/b
        ~^~
ZeroDivisionError: division by zero

Question, file 165. Which line number has a problem?

Show the answer

Answer. The third line, the instructor said: c = a/b stops with a ZeroDivisionError when the second number is 0, as the second run shows. So that is the line to put in try.

Only the vulnerable line: file 166. Whichever code is vulnerable, only that goes into try, the instructor said: do not put everything into try, that is a bad practice, and generally, this is a difference between you and a good tester. The lines that cannot give a ZeroDivisionError stay outside, lines 1 and 2 here. Runs with 10 and 0, with 10 and 10, where 1.0 comes, a floating-point number, and with a word instead of a number:

166_Fix.pyPython
a = int(input("Enter num 1"))
b = int(input("Enter num 2"))
try:
    c = a/b
    print(c)
except ZeroDivisionError:
    print("Error bue to ZDE")
Output
Enter num 110
Enter num 20
Error bue to ZDE
Output
Enter num 110
Enter num 210
1.0
Output
Enter num 1abc
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/166_Fix.py", line 1, in <module>
    a = int(input("Enter num 1"))
        ^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'abc'

Question, file 166. What if I enter a name instead of a number?

Show the answer

Answer. Then we get a ValueError, the instructor said, which means these lines are vulnerable too. In file 166 they sit outside try, so the third run above stops at line 1, before the division is ever reached.

A tuple of errors: file 167. So the class added one more fix: the input lines go inside try too, and the except takes the errors comma separated. If there is a TypeError or a NameError as well, you can add that too, the instructor said: whatever errors you know. Runs with a word, then with 10 and 0:

167_Fix2pyPython

try:
    a = int(input("Enter num 1"))
    b = int(input("Enter num 2"))
    c = a/b
    print(c)
except (ZeroDivisionError, ValueError, TypeError, NameError):
    print("Error bue to ZDE or Value")
Output
Enter num 1abc
Error bue to ZDE or Value
Output
Enter num 110
Enter num 20
Error bue to ZDE or Value

The file's name has no dot before py: it ends in Fix2py, not .py. Python runs it all the same when you give its name in full, as the runs above do.

One except per error: file 168. Or there is another fix, the instructor said: multiple excepts, first the ValueError, then the ZeroDivisionError. Runs with a word, then with 10 and 0:

168_Fix3.pyPython
try:
    a = int(input("Enter num 1"))
    b = int(input("Enter num 2"))
    c = a / b
    print(c)
except ValueError:
    print("Value Error")
except ZeroDivisionError:
    print("Div Error")
Output
Enter num 1abc
Value Error
Output
Enter num 110
Enter num 20
Div Error

finally: file 169. finally means it will always be executed, the instructor said: it does not matter whether the program ran properly or there was a ValueError, finally has to execute. The class's run, with 10 and 34:

169_Finally.pyPython
try:
    a = int(input("Enter num 1"))
    b = int(input("Enter num 2"))
    c = a / b
    print(c)
except ValueError:
    print("Value Error")
except ZeroDivisionError:
    print("Div Error")
finally:
    print("I will always execute!")
Output
Enter num 110
Enter num 234
0.29411764705882354
I will always execute!

Three runs, one for each way through:

You type What runs It prints
10, then 34 all of try, then finally 0.29411764705882354, then I will always execute!
abc except ValueError, then finally Value Error, then I will always execute!
10, then 0 except ZeroDivisionError, then finally Div Error, then I will always execute!
04

Catch order and custom exceptions: files 170 to 172

Two panels on cream paper, each with a short chute that rolls coloured pebbles down to a row of three buckets, small, medium and very large. Left, titled Biggest first, with the sublabel Catches all: the very large bucket stands first under the chute and every pebble lands in it, while the small and medium buckets behind it stay empty. Right, titled Biggest last, with the sublabel Last resort: the small bucket stands first and holds the blush pebbles, the medium one holds the terracotta pebbles, and the very large bucket at the end holds only the two grey pebbles the others missed.
The order of excepts, as the class put it: the biggest one, Exception, goes last, as the last resort; put first, it catches all the exceptions.

A real example: file 170. When we work with an API, there is a module named requests which helps you make an API request, the instructor said. The file reads a URL from the user, calls it with requests.get() and prints the status code. There can be a connection error, and there can be a timeout too, and we do not know which one it will be. So the last except is the biggest one, Exception, kept as e and printed. The deck holds the same code.

170_REAL.pyPython
import requests

try:
    url = input("Enter the url")
    response = requests.get(url=url)
    print(response.status_code)
except requests.exceptions.ConnectionError:
    print("Exception due to connection error")
except requests.exceptions.Timeout:
    print("Exception due to connection error")
except Exception as e:
    print(e)

Run here only against this machine: a small web server started for the run, a port where nothing listens, and a URL with no http://. The address typed in class was not run.

You type Where it goes It prints
http://127.0.0.1:8731/, a web server on this machine no except: the request works 200
http://127.0.0.1:1, a port where nothing listens the ConnectionError except, line 7 Exception due to connection error
abc, with no http:// except Exception as e, line 11 Invalid URL 'abc': No scheme supplied. Perhaps you meant https://abc?

Question, file 170. If you see the diagram of exceptions, who is the parent, the super of every exception?

Worth practising too: move except Exception as e: and its print to the top, above line 7. What does the closed port print then?

Show the answer

Answer. Exception is the parent, the instructor said, as the figure at the top of the exceptions section shows too. So the connection error is tried first, then the timeout, and if both miss, the biggest one catches it, as a last resort: it is like the biggest bucket. You should not have the biggest first: it would catch all the exceptions, and the excepts below it would be redundant code, a total waste. The biggest exceptions always go last, and this is asked in interviews too. Moved to the top, Exception catches the closed port before line 7 can, and prints the raw error, as the run below shows.

Python
import requests

try:
    url = input("Enter the url")
    response = requests.get(url=url)
    print(response.status_code)
except Exception as e:
    print(e)
except requests.exceptions.ConnectionError:
    print("Exception due to connection error")
except requests.exceptions.Timeout:
    print("Exception due to connection error")
Output
Enter the urlhttp://127.0.0.1:1
HTTPConnectionPool(host='127.0.0.1', port=1): Max retries exceeded with url: / (Caused by NewConnectionError("HTTPConnection(host='127.0.0.1', port=1): Failed to establish a new connection: [Errno 61] Connection refused"))

The deck's P.S. says the same as the instructor:

Biggest exceptions are always in the last. We will always start from the lower exceptions to the higher exception at the end

ValueError and ZeroDivisionError are siblings. In file 168, either order works: neither class is the other's parent, as the figure at the top of the exceptions section shows, and issubclass() gives False both ways round. The order matters when one except names a parent of another, as Exception is to every error in file 170.

Lines 8 and 10 print the same text. The timeout's except, line 10, prints Exception due to connection error, the text of line 8, so a caught Timeout would read like a connection error. Line 10 needs a message of its own, such as Exception due to timeout.

raise: file 171. We can also raise a custom exception directly, the instructor said, with raise. Here a vwo_login() function raises an exception when the user is not admin, not authorized, and otherwise returns a welcome. The deck holds the same code.

171_Custom_Exception.pyPython
def vwo_login(user):
    if user!= "admin":
        raise Exception("You are not admin, not auth.")
    return "Welcome Admin"

# print(vwo_login("pramod"))
print(vwo_login("admin"))
Output
Welcome Admin

The class's first run called it with another name, line 6, which is not admin, so the custom exception came:

Python
def vwo_login(user):
    if user!= "admin":
        raise Exception("You are not admin, not auth.")
    return "Welcome Admin"

print(vwo_login("pramod"))
# print(vwo_login("admin"))
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/171_Custom_Exception.py", line 6, in <module>
    print(vwo_login("pramod"))
          ^^^^^^^^^^^^^^^^^^^
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/171_Custom_Exception.py", line 3, in vwo_login
    raise Exception("You are not admin, not auth.")
Exception: You are not admin, not auth.

Your own exception class: file 172. You can also raise a custom exception by creating a class, the instructor said: InvalidAgeException inherits from Exception, which is single inheritance, and pass means it is an incomplete class, with nothing written in it. Line 8 raises it when the age is less than 18. With 25, nothing was printed at first, because there was no else, the instructor said, so the class added one, line 9, to print it.

172_Exceptions.pyPython
# Create a Class for the Custom Exception

class InvalidAgeException(Exception):
    pass

def can_you_drink(age):
    if age < 18:
        raise InvalidAgeException("You can't drink!")
    else:
        print("You can drink!")

# can_you_drink(17)
can_you_drink(25)
Output
You can drink!

With line 12 switched on and line 13 off, the age is 17:

Python
# Create a Class for the Custom Exception

class InvalidAgeException(Exception):
    pass

def can_you_drink(age):
    if age < 18:
        raise InvalidAgeException("You can't drink!")
    else:
        print("You can drink!")

can_you_drink(17)
# can_you_drink(25)
Output
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/172_Exceptions.py", line 12, in <module>
    can_you_drink(17)
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions/172_Exceptions.py", line 8, in can_you_drink
    raise InvalidAgeException("You can't drink!")
InvalidAgeException: You can't drink!

As the instructor traced it, the invalid age exception leads to Exception, Exception to the base exception, and the base exception gives you the error. Python's own order, printed at the end of the file, is ['InvalidAgeException', 'Exception', 'BaseException', 'object'].

Question, file 172. Can you drink, based on your age: at 17, and at 25?

Show the answer

Answer. At 17, you get an error, the instructor said: line 8 raises InvalidAgeException with You can't drink!, as the run with line 12 switched on shows. At 25, you can: the else on line 9 prints You can drink!.

05

Reading a file: file 173

In Python, it is very easy to open a file, the instructor said. A small file, test.json, holds only one thing, a name:

test.jsonJSON
{
    "name" : "PrrammodDutta"
}

open() opens the file and read() reads it, the instructor said. The first run found no such file, though test.json was there, so the instructor gave the path from the chapter folder, and the file was read. If the path is wrong, there will be an exception, a FileNotFoundError, so the open went into try, with except FileNotFoundError as fnf, fnf as a short name for it.

173_FIO.pyPython
try:
    data = open("09_Exceptions/test.json").read()
    print(data)
except FileNotFoundError as fnf:
    print(fnf)

The path on line 2 is relative: Python looks for it from the folder you run in, so run the file from ex_18_OOPs_Python, as the README notes point out, or you get the FileNotFoundError it is there to catch. From ex_18_OOPs_Python:

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python
python3 09_Exceptions/173_FIO.py
Output
{
    "name" : "PrrammodDutta"
}

From the file's own folder, 09_Exceptions:

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/09_Exceptions
python3 173_FIO.py
Output
[Errno 2] No such file or directory: '09_Exceptions/test.json'

Question, file 173. If the path is wrong, what will happen, and what do we need to do to fix it?

Show the answer

Answer. There will be an exception, a FileNotFoundError, the instructor said, so we need to add it into try and except. From 09_Exceptions, the path 09_Exceptions/test.json does not exist, and line 5 prints the error instead of stopping the program.

A learner had expected the class to import pandas. Pandas is required when you want heavy file loading or a heavy Excel file, the instructor said; for us, it is very easy to just use open().

06

The os module: files 174 and 175

MAKE, WRITE, CHECK, CLEAN UP: FILE 175 LINES 3 TO 7 os.getcwd() the folder you ran it from join(..., "demo_output") LINE 9 os.makedirs(folder, exist_ok=True) no error if it is already there LINES 11 TO 13 open(file_path, "w") writes hello.txt: Hello from the os module LINES 16 TO 18 check it Exists? True Size: 24 bytes Files: ['hello.txt'] LINES 20 AND 21 os.remove(file_path) os.rmdir(folder) file first: rmdir needs it empty LINE 22 not os.path.exists(...) Cleaned up: True demo_output is gone again One run leaves the folder exactly as it found it.
Every printed value is from a real run of file 175, from its own folder, 10_OS_Module.

While doing exceptions, we also have to learn one more concept, the instructor said: the os module, the operating system module, which will be used a lot of the time, everywhere in AI projects and in CrewAI too. The deck's heading calls it operating system interactions. Any kind of activity related to the operating system, copying a file, finding a directory, moving a file, the os module can be used for directly. It has so many functions, and we are interested in a few of them.

During the class, the instructor asked for a list of the common os functions as a .md file in the learnings folder, and toured it; the repo received it in the last minutes of the class, as learnings/python-os-module-cheatsheet.md. It closes with a top ten:

python-os-module-cheatsheet.mdText
1. `os.getcwd()`
2. `os.listdir()`
3. `os.makedirs(path, exist_ok=True)`
4. `os.remove()`
5. `os.rename()`
6. `os.walk()`
7. `os.path.join()`
8. `os.path.exists()`
9. `os.path.abspath(__file__)` + `os.path.dirname()`
10. `os.getenv()` / `os.environ`

The top ones you need to remember, the instructor said: get the current directory, list a directory, make a directory, remove, walk, path join, exists, the absolute path and get the environment variable. chmod, in the cheat sheet too, is short for change mode, the permissions, the instructor answered a learner.

First steps: file 174. os.name, the current folder, and what is in it:

174_OS.pyPython
import os
print(os.name)
print(os.getcwd())
# print(os.mkdir("AI"))
print(os.listdir())

print(os.remove("AI.txt"))
print(os.rename("AI.txt","testdata.txt"))

Run from its own folder, it prints posix, the folder and its three files, and stops at line 7. os.getcwd() prints the full path of the folder; on this page, every path starts at the repository, and the part before it, which differs on every machine, is left out.

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
python3 174_OS.py
Output
posix
chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
['174_OS.py', '175.py', '176_Env.py']
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module/174_OS.py", line 7, in <module>
    print(os.remove("AI.txt"))
          ^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'AI.txt'

Question, file 174. Can you run this code and tell me what os.name gives you?

Show the answer

Answer. posix on the instructor's Mac, which means Unix, and posix in the run above too. For Windows, the instructor first guessed win, then said nt, a short form for Windows NT; the cheat sheet has the same: posix for macOS and Linux, nt for Windows.

Line 7 stops the run: you will get an error, the instructor said, because there is no file. Line 4 is switched off: run, os.mkdir("AI") printed None and created a folder named AI, and since it already existed, the class took the line out. The line on its own, run twice:

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
python3 -c 'import os; print(os.mkdir("AI"))'
python3 -c 'import os; print(os.mkdir("AI"))'
Output
None
Traceback (most recent call last):
  File "<string>", line 1, in <module>
FileExistsError: [Errno 17] File exists: 'AI'

Lines 7 and 8 cannot both work. Line 7 removes AI.txt, and line 8 renames the same AI.txt. With an empty AI.txt in the folder, line 7 removes it and prints None, and line 8 then finds no file to rename, as the run below shows. os.remove() or os.rename() on a file that is not there raises FileNotFoundError, as the README notes point out, so run one of the two at a time.

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
touch AI.txt
python3 174_OS.py
Output
posix
chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
['174_OS.py', '175.py', 'AI.txt', '176_Env.py']
None
Traceback (most recent call last):
  File "chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module/174_OS.py", line 8, in <module>
    print(os.rename("AI.txt","testdata.txt"))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'AI.txt' -> 'testdata.txt'

Make, write, check, clean up: file 175. Let me show you something interesting, the instructor said: file 175, the same code as the deck's os module section.

175.pyPython
import os

print("CWD:", os.getcwd())
print("OS:", os.name)

folder = os.path.join(os.getcwd(), "demo_output")
print(folder)

os.makedirs(folder, exist_ok=True)

file_path = os.path.join(folder, "hello.txt")
with open(file_path, "w") as f:
    f.write("Hello from the os module")


print("Exists?", os.path.exists(file_path))
print("Size:", os.path.getsize(file_path), "bytes")
print("Files:", os.listdir(folder))

os.remove(file_path)
os.rmdir(folder)
print("Cleaned up:", not os.path.exists(folder))
Output
CWD: chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
OS: posix
chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module/demo_output
Exists? True
Size: 24 bytes
Files: ['hello.txt']
Cleaned up: True

Line 6 joins the current folder and demo_output: a separate folder, outside, not inside, the instructor said. Line 9 creates it, and exist_ok=True means that if it already exists, do not create it again and do not give an error, the instructor said. Lines 11 to 13 write Hello from the os module into hello.txt, and the next three lines check it: it exists, its size is 24 bytes, and it is the only file in the folder. Then the file is removed, then the folder, and line 22 prints True, which means it is gone. First we got the current working directory, created a directory, created a file, checked the files and the bytes, and then removed it, the instructor summed up: this gives all the examples of os. With demo_output already there before the run, line 9 gives no error, and the file prints the same seven lines:

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
mkdir demo_output
python3 175.py
Output
CWD: chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
OS: posix
chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module/demo_output
Exists? True
Size: 24 bytes
Files: ['hello.txt']
Cleaned up: True
07

Environment variables: file 176

AN EXPORT REACHES THE PROGRAMS STARTED FROM ITS TERMINAL: FILE 176 TERMINAL 1, BEFORE THE EXPORT python3 176_Env.py FILE 176 RUNS os.getenv("TOKEN") TOKEN is not set here IT PRINTS None TERMINAL 1, AFTER THE EXPORT export TOKEN=dummy-value python3 176_Env.py FILE 176 RUNS os.getenv("TOKEN") gets the terminal's TOKEN IT PRINTS dummy-value TERMINAL 2, OPENED AFTER IT python3 176_Env.py FILE 176 RUNS os.getenv("TOKEN") this terminal has no TOKEN IT PRINTS None A program gets a copy of its terminal's variables when it starts.
Three real runs of file 176, with a dummy value: two in one terminal session, and one in a second session that never ran the export.

One more is very important, the instructor said: getting an environment variable, and setting one. Environment variables are variables available in your operating system: where your Java is present, where your Jenkins is, where Zoom is installed. To set one, you use export, for example export abc=xyz, and os.getenv() reads it. The class read abc and got xyz; a variable that was not set gave None, until it was exported too. You can set your API URL this way, and store secret values, such as a token. The pushed file reads TOKEN:

176_Env.pyPython
import os
print( os.getenv("TOKEN"))

Run before and after an export, in one terminal, with a dummy value:

Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/10_OS_Module
python3 176_Env.py
export TOKEN=dummy-value
python3 176_Env.py
Output
None
dummy-value

Question. Where is an environment variable stored by default?

Worth practising too: export TOKEN in one terminal, then run file 176 in a second terminal. What does it print?

Show the answer

Answer. At runtime, at the operating system level, the instructor answered a learner. The second terminal prints None: an export reaches the programs started from its own terminal, and a terminal opened afterwards does not have it, as the figure at the top of this section shows.

export is for Mac and Linux terminals. The class ran it on a Mac, where os.name printed posix. On Windows, the Command Prompt and PowerShell set a variable with commands of their own.

08

Packages: file 177

A hand-drawn sketch on warm paper titled Modules and Packages. An open folder labelled package, captioned a folder of modules, holds three file cards: __init__.py with a star, util_module.py and util_module2.py. An arrow leads to a ticket reading from package import util_module, and another to a page labelled 177_Util.py with a green tick.
The README's sketch of file 177, from the class's repo: the package folder, the import, and the script that uses it.

Before pip, the instructor explained folders and packages: in Python, a normal directory is just a folder, but a package has an __init__.py file in it, and the advantage of a package is that you can import it. Python recognizes a folder as a package when it has this file. The deck says a folder is promoted to a package when it contains the special file __init__.py, and that Python recognizes it as a package.

You can create your own package too. In 11_Package, the folder named package holds an empty __init__.py, which makes it a package, and two utility modules with one function each:

util_module.pyPython
def blah(name):
    print(name)
util_module2.pyPython
def blahblah(name):
    print(name)

File 177 imports both from the package folder and calls a function from each. It will run absolutely fine, the instructor said.

177_Util.pyPython
from package import util_module,util_module2

util_module2.blahblah("das")
util_module.blah("das")
Output
das
das

Very simple: it is just borrowing, the instructor said. If something is not working, we can borrow from our neighbour, and in the same way, from the package folder you can borrow directly.

But if it is a normal folder, like normal_dir next to it, this one cannot be used, the instructor said: ideally, py packages with __init__ in them are the easy way of importing, because Python recognizes them.

A plain folder imports too. normal_dir has no __init__.py, and its one file, util_normal.py, is empty. The deck's note says Python does not import a plain folder, but the README notes add that Python 3 can still import it, as a namespace package. Three lines added at the end of 177 show both kinds:

Python
from package import util_module,util_module2

util_module2.blahblah("das")
util_module.blah("das")

import package, normal_dir
print(package)
print(normal_dir)
Output
das
das
<module 'package' from 'chapter_15_Python_PyTest/ex_18_OOPs_Python/11_Package/package/__init__.py'>
<module 'normal_dir' (namespace) from ['chapter_15_Python_PyTest/ex_18_OOPs_Python/11_Package/normal_dir']>

package loads from its __init__.py; normal_dir has none, so Python marks it namespace, and there is nothing in it to call. __init__.py is what makes package a regular package, the kind the class used.

Question, normal_dir. Suppose this is a normal folder, a normal utility: can we use it?

Worth practising too: run file 177 from the repository's top folder. Does the import still work?

Show the answer

Answer. No, the instructor answered: ideally, py packages with __init__ are the easy way of importing. Python 3 does load normal_dir, as the README notes and the run above show, but only as a namespace package, while package, with its __init__.py, is a regular one. Run from the top folder, 177 still prints das twice: Python looks for package in the folder of the script you run, as the README notes explain, so 177 finds it from any folder.

09

pip and uv

A sheet titled pip vs uv in Python, subtitled Package installation, virtual environments, and workflow differences, in eight numbered panels. 1, What is pip?: the default package installer, with python -m pip install requests. 2, What is uv?: a fast package and project manager, written in Rust, with uv add requests. 3, Main difference: pip is an installer first, uv a workflow tool plus package management. 4, Install package side by side: python -m pip install pytest against uv add pytest. 5, Virtual environment: python -m venv .venv, then activate and pip install flask, against uv venv and uv add flask. 6, Requirements and lock style: requirements.txt against pyproject.toml, uv.lock and uv sync. 7, When to use which: pip for basics and simple setups, uv for speed and a modern project setup. 8, Simple pros and cons of each.
The pip and uv sheet the instructor added in class, 13_PIP/image.png, shown without its closing takeaway row.

Before pytest, one more concept, the instructor said: pip, the Python package manager, which is how the deck has it too. pip is an installer very similar to npm: it helps you install and upgrade Python libraries, and we need it because we want to install third-party libraries. You can check the pip version, install a package, uninstall one, check the list, and upgrade. A requirements.txt file holds the versions of the libraries you want, and with a virtual environment, you create it, activate it, install the package and use it directly. pip install selenium, for example, installs Selenium into the project. The sheet at the top of this section is the image the instructor added in class, 13_PIP/image.png.

Then uv. pip was a little slow, so people created their own version, uv, the instructor said: pip is the default package manager, but uv is a fast one, written in Rust, because Rust is a very fast programming language, and it is all in one. The commands are similar; on the class's sheet, uv venv, uv add flask and uv sync. Whichever of the two you want to use, you are free to use, the instructor said; we will always prefer pip as of now.

First, you need to install uv, the instructor said, and pip install uv also works. On Windows, you run one PowerShell command, which installs uv for you; the deck links uv's installation guide, which gives it as:

Terminal
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Then uv init first, and uv add, as the deck has it:

Terminal
uv init
uv add selenium

The class first ran uv add without uv init, and there was no project toml file, hmm, strange, the instructor said. Both commands, run here in an empty folder named like the class's project, with the network switched off, in the order the class met them:

Terminal
uv add selenium
Output
error: No `pyproject.toml` found in current directory or any parent directory
Terminal
uv init
Output
Initialized project `aitesterblueprin4x`

uv add downloads the package, so it was not run here. In class, Selenium installed, a little fast, then Playwright, super fast, then the OpenAI library, super fast: that is why people prefer to use uv, the instructor said. Installed with pip, it will be very slow, the instructor said, adding that Playwright was already installed on that machine.

The repo is now a uv project, as the README notes say: .python-version pins Python 3.13, and pyproject.toml lists the four libraries the class added.

.python-versionText
3.13
pyproject.tomlText
requires-python = ">=3.13"
dependencies = [
    "openai>=3.51.0",
    "playwright>=1.65.2",
    "pytest>=9.2.0",
    "selenium>=4.52.0",
]
10

pytest basics: test_178 and test_179

THREE TESTS, TWO DOTS AND AN F pytest test_179.py LINES 3 AND 4 test_positive() assert 4 != 5 THE ASSERT True: passes nothing raised . LINES 7 AND 8 test_positive2() assert 4 != 5 THE ASSERT True: passes nothing raised . LINES 10 AND 11 test_negative() assert 4 == 5 THE ASSERT False: fails AssertionError: test stops F RESULT ..F 1 failed, 2 passed pytest runs each test_ function: a dot is a pass, F a failure.
The dots, the F and the summary are from a real run of pytest test_179.py, from its own folder.

Now the last and the most important concept, the instructor said: pytest. We will not be able to learn the DeepEval framework and CrewAI until we know pytest. It is a Python testing framework, very similar to TestNG, which allows you to have a test run. The deck:

A quick reference for pytest, the most popular Python testing framework.

Question. What is a test?

Show the answer

Answer. Whenever we want to check that the expected result is equal to the actual result, the instructor said: whatever the agent has given, the expected result should be equal to the actual result. In pytest, that is what we are testing, nothing else. The deck puts it as ER == AR.

pip install pytest is the easy way, the instructor said, or uv add pytest if you want it fast: both of them work. On the class's machine, it said requirement already satisfied. The cheat sheet the instructor added to 12_PyTest lists the commands the class read through:

PyTest.cheatSheet.mdTerminal
pip install pytest

pytest                          # run all tests in current dir
pytest test_180.py              # run one file
pytest -k "smoke"               # run tests matching keyword (name or marker)
pytest -m smoke                 # run tests with marker @pytest.mark.smoke
pytest -v                       # verbose output (shows each test name)
pytest -s                       # show print() output (disables capture)
pytest -v -s                    # combine both
pytest --maxfail=2              # stop after 2 failures
pytest -x                       # stop at first failure
pytest --lf                     # rerun only last failed tests
pytest --co                     # collect only (dry run, no execution)

test_ first: test_178. Every pytest file is created with test_ at the start of its name, test_178.py: remember this, it is very, very important, the instructor said. Import pytest, write a function, and assert. If you want, you can mark it: the instructor marked it as regression, and the file marks it reg. To run it, go to the terminal and type pytest and the file.

test_178.pyPython
import pytest

@pytest.mark.reg
def test_check():
    assert 3 == 4
Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/12_PyTest
pytest test_178.py
Output
============================= test session starts ==============================
platform darwin -- Python 3.12.7, pytest-9.1.1, pluggy-1.6.0
rootdir: .
configfile: pyproject.toml
collected 1 item

test_178.py F                                                            [100%]

=================================== FAILURES ===================================
__________________________________ test_check __________________________________

    @pytest.mark.reg
    def test_check():
>       assert 3 == 4
E       assert 3 == 4

test_178.py:5: AssertionError
=============================== warnings summary ===============================
test_178.py:3
  chapter_15_Python_PyTest/ex_18_OOPs_Python/12_PyTest/test_178.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.reg - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.reg

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ============================
FAILED test_178.py::test_check - assert 3 == 4
========================= 1 failed, 1 warning in 0.01s =========================

The class's first run asserted 3 == 3, and the dot means it passed:

Python
import pytest

@pytest.mark.reg
def test_check():
    assert 3 == 3
Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/12_PyTest
pytest test_178.py
Output
============================= test session starts ==============================
platform darwin -- Python 3.12.7, pytest-9.1.1, pluggy-1.6.0
rootdir: .
configfile: pyproject.toml
collected 1 item

test_178.py .                                                            [100%]

=============================== warnings summary ===============================
test_178.py:3
  chapter_15_Python_PyTest/ex_18_OOPs_Python/12_PyTest/test_178.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.reg - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.reg

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================= 1 passed, 1 warning in 0.00s =========================

Question, test_178. If I say 4, what will happen?

Show the answer

Answer. False, because 3 is not equal to 4, the instructor said: pytest prints F instead of the dot, and FAILURES shows the line, assert 3 == 4. In pytest, everything is a function, and in it you check the assertion.

The warning is about the marker. pytest does not know reg until it is registered, so it adds a PytestUnknownMarkWarning, as the README notes point out; the cheat sheet registers markers in a pytest.ini. What a marker means comes later, with CrewAI.

Two dots and an F: test_179. One more example, the instructor said, with three test functions:

test_179.pyPython
import pytest

def test_positive():
    assert 4 != 5


def test_positive2():
    assert 4 != 5

def test_negative():
    assert 4 == 5
Terminal
cd chapter_15_Python_PyTest/ex_18_OOPs_Python/12_PyTest
pytest test_179.py
Output
============================= test session starts ==============================
platform darwin -- Python 3.12.7, pytest-9.1.1, pluggy-1.6.0
rootdir: .
configfile: pyproject.toml
collected 3 items

test_179.py ..F                                                          [100%]

=================================== FAILURES ===================================
________________________________ test_negative _________________________________

    def test_negative():
>       assert 4 == 5
E       assert 4 == 5

test_179.py:11: AssertionError
=========================== short test summary info ============================
FAILED test_179.py::test_negative - assert 4 == 5
========================= 1 failed, 2 passed in 0.01s ==========================

Dot means pass, and F means failure, the instructor said: two dots and one F, two passed and one failed. assert is a hard assertion, the instructor said: it stops the execution, as the figure at the top of this section shows for test_negative().

11

Tasks and announcements

  • Exercises up to 179, on your GitHub. The code is already posted, the instructor said: do the practice. Meeti, the TA, was asked to add this, the exercises up to 179: everyone has to do them and upload them to their own GitHub repository.
  • Previous projects too. Is it OK to upload the previous projects now, a learner asked? Yes, you can, the instructor said.
  • Next week: CrewAI with pytest. Next week covers the important concepts of CrewAI with pytest, the instructor said, because CrewAI uses pytest. Markers, xfail and skip, priority and grouping as in TestNG, and parallel and sequential runs come there, slowly, with CrewAI. The pytest cheat sheet the instructor added in class is in the repo.
  • Four topics left. Today we will complete Python, I think, the instructor said at the start: I hope so. The four topics remaining are CrewAI, LangChain, DeepEval and LangGraph, starting with CrewAI next week. When file I/O came up at the end: it was already covered, the instructor said, in the operating system part, and with CrewAI and DeepEval it will come automatically.
  • pip, for now. The class will always prefer pip as of now, the instructor said; uv is there if you want it.
  • A hackathon is coming. Probably next to next week or so, the instructor said: be prepared, and start your preparations. The details will follow.
  • The notes as a PDF. Can the notes be downloaded, a learner asked? Yes, you can, the instructor said, adding I hope so: there is a way to export them. In the end, the instructor generally shares the exported version with everyone, and said the overall documented PDF will be given in the end: a final PDF with all the diagrams, if you want it. Remind the instructor in the end.
  • Regex. The regex notes are there; a small video on regex is still to be made, and the instructor said it will be created soon.

Task: exercises up to 179. Do every exercise up to 179 and upload them to your own GitHub repository.

Show the answer

Answer. Files 159 to 179 are all on this page, each with what it prints. Write each one yourself, run it, compare your output with the page, then push them to your own repository.