How PySnooper Can Replace Print Statements for Smarter Python Debugging
This article explains why traditional print‑based debugging is cumbersome, introduces the open‑source PySnooper decorator that automatically logs executed lines and variable values, demonstrates its usage with example code and output, and shows how to install and redirect its logs to a file.
Limitations of print‑based debugging
Using print statements to trace program execution requires inserting many statements, manually removing them after debugging, and clutters the source code. This approach is error‑prone and inefficient, especially when many variables or code paths need inspection.
PySnooper overview
PySnooper is an open‑source Python library that provides a decorator @pysnooper.snoop(). When applied to a function, it automatically logs each executed line, timestamps, and the values of local variables, eliminating the need for manual print statements.
Traditional print‑debug example
def removeDuplicates(nums):
"""
:type nums: List[int]
:rtype: int
"""
flag = 0
i = 1
while i < len(nums):
if nums[i] == nums[i - 1]:
flag += 1
i += 1
print("flag=", flag)
print("i=", i)
if flag >= 2:
del nums[i - 1]
i -= 1
print("i=", i)
print("nums=", nums)
else:
print("else")
i += 1
flag = 0
return len(nums)
nums = [1, 1, 1, 2]
print(removeDuplicates(nums))This code prints intermediate values to the console, but the output is plain text and the statements must be removed after debugging.
Using PySnooper
import pysnooper
@pysnooper.snoop()
def removeDuplicates(nums):
flag = 0
i = 1
while i < len(nums):
if nums[i] == nums[i - 1]:
flag += 1
i += 1
if flag >= 2:
del nums[i - 1]
i -= 1
else:
i += 1
flag = 0
return len(nums)
nums = [1, 1, 1, 2]
print(removeDuplicates(nums))Running this version produces a colored log where:
Red frames highlight the line currently being executed.
Blue, green, and yellow frames show the values of variables at that point.
Redirecting output to a file
If writing to standard error is inconvenient, the log can be saved to a file:
import pysnooper
@pysnooper.snoop('file.log')
def removeDuplicates(nums):
# function body identical to the example above
...The generated file.log contains the same detailed line‑by‑line execution information.
Installation
pip install pysnooperIf the default PyPI source is slow, a mirror can be used:
pip install pysnooper -i https://pypi.douban.com/simpleRepository
Source code, documentation, and additional examples are available at https://github.com/cool-RR/PySnooper.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
