PrettyTable
PrettyTable is a Python library for easily displaying tabular data in a visually appealing ASCII table format. It supports various output formats including ASCII, HTML, CSV, JSON, LaTeX, and MediaWiki. The library is actively maintained with regular releases, currently at version 3.17.0.
Common errors
-
ModuleNotFoundError: No module named 'prettytable'
cause The 'prettytable' package is not installed in the Python environment where the script is being executed.fixpip install prettytable -
AttributeError: 'module' object has no attribute 'PrettyTable'
cause A local Python file named 'prettytable.py' exists in the same directory as your script, shadowing the installed 'prettytable' library and preventing the correct module from being imported.fixRename your local file from 'prettytable.py' to something else (e.g., 'my_table_script.py') and delete any associated '.pyc' files. -
TypeError: PrettyTable.add_row() takes 2 positional arguments but X were given
cause The 'add_row()' method expects a single iterable (like a list or tuple) containing all the values for the row as its argument, but individual values are being passed as separate arguments.fixPass a list or tuple of values to 'add_row()', for example: `table.add_row([value1, value2, value3])`. -
AttributeError: 'module' object has no attribute 'set_field_names'
cause The 'set_field_names()' method was deprecated and removed in PrettyTable version 0.6. Field names are now set directly via the 'field_names' attribute.fixInstead of `table.set_field_names(['Header 1', 'Header 2'])`, use `table.field_names = ['Header 1', 'Header 2']`.
Warnings
- breaking PrettyTable 3.17.0 dropped support for Python 3.9. Users on Python 3.9 or older should use an earlier version of PrettyTable (e.g., 3.16.0 or below) or upgrade their Python interpreter.
- deprecated The direct constants `hrule` and `tableStyle` were deprecated in version 3.12.0. Use the corresponding enum values from `HRuleStyle`, `VRuleStyle`, and `TableStyle` instead for clarity and type safety.
- gotcha `ModuleNotFoundError: No module named 'prettytable'` is a common issue, often caused by installing `prettytable` in a different Python environment (e.g., virtual environment) than the one your IDE or script is using.
- gotcha Prior to 3.15.1, `add_rows()` could raise an `IndexError` if provided with an empty list that was not handled gracefully.
- gotcha In versions prior to 3.14.0, specifying `sortby` directly at class declaration might not have been correctly applied, requiring it to be set after instantiation or via `get_string()` arguments.
Install
-
pip install prettytable
Imports
- PrettyTable
from prettytable import PrettyTable
- ColorTable
from prettytable.colortable import ColorTable
- TableStyle
from prettytable import TableStyle
Quickstart
from prettytable import PrettyTable table = PrettyTable() table.field_names = ["City name", "Area", "Population"] table.add_row(["Adelaide", 1295, 1158259]) table.add_row(["Brisbane", 5905, 1857594]) table.add_row(["Sydney", 2058, 4336374]) print(table) # Example with alignment and style table.align["City name"] = "l" table.align["Population"] = "r" # To use custom styles, import TableStyle from prettytable import TableStyle table.set_style(TableStyle.MARKDOWN) print(table)