PyPrind (Python Progress Indicator)
PyPrind is a Python module that provides simple yet effective progress bar and percentage indicator utilities. It allows developers to visualize the progress of iterative computations, which is particularly useful for long-running processes or when handling large datasets. The library's current version is 2.11.3, with its last release in April 2021, indicating a slow release cadence and a current status of maintenance.
Common errors
-
ValueError: psutil package is required when using the `monitor` option.
cause Attempting to use `ProgBar(monitor=True)` or `ProgPercent(monitor=True)` without having the `psutil` library installed.fixInstall `psutil` using pip: `pip install psutil`. -
NameError: name 'pyprind' is not defined
cause The `pyprind` module or its classes/functions were used without being imported first.fixAdd `import pyprind` or `from pyprind import ProgBar` (or ProgPercent) at the top of your script. -
IndentationError: expected an indented block
cause Python relies on consistent indentation. This error often occurs in loops where `bar.update()` or similar calls are not correctly indented within the loop body.fixEnsure all code within a block (like a `for` loop) is indented consistently, typically with 4 spaces.
Warnings
- gotcha Enabling CPU/memory monitoring (`monitor=True`) requires the `psutil` package to be installed. Without it, a `ValueError` will be raised.
- deprecated Prior to version 2.11.0, the progress bar output would occupy two lines in the terminal. As of v2.11.0, it was consolidated to use only one line for a more compact display.
- breaking In versions prior to 2.11.2, iterating over a completed `ProgBar` object could print unwanted new lines, causing display issues.
- gotcha Older versions (pre-2.10.0) had issues with Jupyter Notebooks where the ETA was printed on new lines, disrupting the output format.
Install
-
pip install pyprind
Imports
- ProgBar
from pyprind import ProgBar
- ProgPercent
from pyprind import ProgPercent
- prog_bar
import pyprind for i in pyprind.prog_bar(range(n)): pass
Quickstart
import time
import pyprind
n = 10000000 # Number of iterations
# Using ProgBar class
bar = pyprind.ProgBar(n, title='Processing Data')
for i in range(n):
# Simulate some computation
# time.sleep(0.000001) # Uncomment to slow down and see progress
bar.update()
print(bar)
print("\nAlternatively, using the generator function:")
# Using prog_bar generator function
for i in pyprind.prog_bar(range(n), title='Another Task'):
# Simulate some computation
pass # time.sleep(0.000001)
# Example with monitor=True (requires psutil)
# try:
# bar_monitor = pyprind.ProgBar(100, monitor=True, title='Monitoring System')
# for i in range(100):
# time.sleep(0.1)
# bar_monitor.update()
# print(bar_monitor)
# except ValueError as e:
# print(f"Warning: {e}. Install psutil (pip install psutil) for monitoring.")