Track your Python loops with visual progress bars using:
- tqdm
- alive-progress
- progressbar2
Complete guide with real life code examples.
1. tqdm - The Fastest and Most Popular
tqdm is the most widely-used Python progress bar library with only 60ns overhead per iteration (13x faster than alternatives). The name means "progress" in Arabic (taqadum).
Installation
pip install tqdm
Basic Example
from tqdm import tqdm
import time
# Simple progress bar
for i in tqdm(range(100)):
time.sleep(0.05) # Simulate work
Output:
100%|██████████████████████| 100/100 [00:05<00:00, 19.85it/s]
Advanced Example with Custom Description
from tqdm import tqdm
import time
# Progress bar with custom label and description
data = ['file1.txt', 'file2.txt', 'file3.txt', 'file4.txt']
for file in tqdm(data, desc="Processing files", unit="file"):
time.sleep(1) # Simulate file processing
tqdm.write(f"Completed: {file}")
Manual Progress Bar Update
from tqdm import tqdm
import time
# When you need manual control
with tqdm(total=100) as pbar:
for i in range(10):
time.sleep(0.1)
pbar.update(10) # Update by 10 each time
pbar.set_description(f"Step {i+1}")
Why use tqdm?
- Works everywhere: Terminal, Jupyter, IDEs
- Zero dependencies
- Integrates with pandas:
df.progress_apply() - Nested progress bars support
- Automatic ETA calculation
2. alive-progress - Modern and Animated
alive-progress offers beautiful animated progress bars with spinners, making your terminal output more engaging.
Installation
pip install alive-progress
Example with Animation
from alive_progress import alive_bar
import time
# Animated progress bar with spinner
items = range(100)
with alive_bar(len(items), title='Processing') as bar:
for item in items:
time.sleep(0.05) # Simulate work
bar() # Update progress
Output: (Shows animated spinner and progress bar)
Processing |████████████████████| 100/100 [100%] in 5.0s (20.0/s)
Custom Themes and Styles
from alive_progress import alive_bar
import time
# Use different bar styles
with alive_bar(100, bar='smooth', spinner='dots') as bar:
for i in range(100):
time.sleep(0.05)
bar()
Why use alive-progress?
- Visually appealing animations
- Multiple built-in themes
- Modern terminal aesthetics
- Great for demos and presentations
3. progressbar2 - Traditional and Reliable
progressbar2 is a stable, well-established library that offers classic progress bar functionality with extensive customization.
Installation
pip install progressbar2
Example
import progressbar
import time
# Classic progress bar
bar = progressbar.ProgressBar(maxval=100)
bar.start()
for i in range(100):
time.sleep(0.05)
bar.update(i + 1)
bar.finish()
Output:
100% |████████████████████████████████| 100/100
Customized Progress Bar
import progressbar
import time
# Custom widgets
widgets = [
'Progress: ', progressbar.Percentage(),
' ', progressbar.Bar(marker='█', left='[', right=']'),
' ', progressbar.ETA()
]
bar = progressbar.ProgressBar(maxval=100, widgets=widgets)
bar.start()
for i in range(100):
time.sleep(0.05)
bar.update(i + 1)
bar.finish()
Why use progressbar2?
- Mature and stable
- Extensive widget system
- Compatible with print statements
- Good for legacy projects
Quick Comparison Table
| Library | Speed | Installation | Best For |
|---|---|---|---|
| tqdm | 60ns/iter ⚡ | pip install tqdm |
General use, fastest |
| alive-progress | ~200ns/iter | pip install alive-progress |
Visual appeal, demos |
| progressbar2 | 800ns/iter | pip install progressbar2 |
Legacy projects, customization |
Jupyter Notebook Support
All three libraries work in Jupyter notebooks, but tqdm has special support:
from tqdm.notebook import tqdm
import time
# Jupyter-optimized progress bar
for i in tqdm(range(100)):
time.sleep(0.05)
This displays a colorful, widget-based progress bar in Jupyter.
Best Practices
1. Use progress bars for tasks over 2 seconds
# Good: Long operation
for i in tqdm(range(10000)):
complex_operation()
# Bad: Quick operation (adds unnecessary overhead)
for i in tqdm(range(10)):
simple_operation()
2. Disable in production when needed
from tqdm import tqdm
# Disable for production
for i in tqdm(range(1000), disable=True):
process_data()
3. Use descriptive labels
# Good: Clear description
for file in tqdm(files, desc="Downloading images"):
download(file)
# Bad: No context
for file in tqdm(files):
download(file)
Common Use Cases
Data Processing
from tqdm import tqdm
import pandas as pd
# Process DataFrame rows
tqdm.pandas()
df['result'] = df.progress_apply(lambda x: process(x), axis=1)
File Downloads
from tqdm import tqdm
import requests
url = "https://example.com/large_file.zip"
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))
with open('file.zip', 'wb') as file, tqdm(
total=total_size,
unit='B',
unit_scale=True,
desc='Downloading'
) as pbar:
for data in response.iter_content(chunk_size=1024):
file.write(data)
pbar.update(len(data))
Nested Loops
from tqdm import tqdm
import time
# Multiple progress bars
for i in tqdm(range(5), desc="Outer loop"):
for j in tqdm(range(100), desc="Inner loop", leave=False):
time.sleep(0.01)
Troubleshooting
Progress bar not showing?
- Check if running in an environment that supports carriage return (
\r) - For Jupyter: Use
from tqdm.notebook import tqdm - For Windows: May need
coloramapackage
Multiple lines instead of updating?
- Add
leave=Falsefor nested loops - Ensure terminal supports ANSI codes
Performance issues?
- Use
mininterval=0.5to reduce update frequency - Disable with
disable=Truein production
Conclusion
For most Python projects, tqdm is the recommended choice due to its speed, simplicity, and extensive features. Use alive-progress when you need eye-catching animations, and progressbar2 for projects requiring specific legacy compatibility.
All three libraries transform your terminal from a black box into an informative, user-friendly interface that shows real-time progress of your Python loops.
FAQ
Q: Which progress bar library is fastest?
A: tqdm is the fastest with only 60ns overhead per iteration, compared to progressbar2's 800ns/iter.
Q: Can I use progress bars with pandas?
A: Yes, use tqdm.pandas() then call df.progress_apply() instead of df.apply().
Q: Do progress bars work in Jupyter notebooks?
A: Yes, use from tqdm.notebook import tqdm for Jupyter-optimized progress bars.
Q: How do I create nested progress bars?
A: Use tqdm with leave=False for inner loops, or alive-progress which supports nesting natively.
Q: Can I customize progress bar appearance?
A: Yes, all libraries support customization. tqdm uses parameters like bar_format, alive-progress has themes, and progressbar2 uses widgets.