In this short guide, I’ll show you several options to print only the error message without the traceback in Python.

This is very useful for presentations or when you have extremely long messages which are not needed.

Option 1: Print Error Message without Traceback with SystemExit(err)

The first option which we will use is using: SystemExit(err).

We are going to catch any exception and handle it. then we are going to print out only the error message.

To show only the error message use the next syntax:

try:
    
    pd.to_datetime(df['date'])
    
except Exception as err:
    raise SystemExit(err)

result:

An exception has occurred, use %tb to see the full traceback.

SystemExit: Out of bounds nanosecond timestamp: 1-06-13 00:00:00

by default the code below

import pandas as pd
pd.to_datetime(df['date'])

would raise very long error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/myuser/Software/Tensorflow/environments/venv36/lib/python3.6/site-packages/pandas/core/arrays/datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   2058         try:
-> 2059             values, tz_parsed = conversion.datetime_to_datetime64(data)
   2060             # If tzaware, these values represent unix timestamps, so we

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()

TypeError: Unrecognized value type: <class 'str'>

During handling of the above exception, another exception occurred:

OutOfBoundsDatetime                       Traceback (most recent call last)
<ipython-input-20-6dea9b57a428> in <module>()
----> 1 pd.to_datetime(df['date'])

/home/myuser/Software/Tensorflow/environments/venv36/lib/python3.6/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
    801             result = arg.map(cache_array)
    802         else:
--> 803             values = convert_listlike(arg._values, format)
    804             result = arg._constructor(values, index=arg.index, name=arg.name)
    805     elif isinstance(arg, (ABCDataFrame, abc.MutableMapping)):

/home/myuser/Software/Tensorflow/environments/venv36/lib/python3.6/site-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
    464             errors=errors,
    465             require_iso8601=require_iso8601,
--> 466             allow_object=True,
    467         )
    468

/home/myuser/Software/Tensorflow/environments/venv36/lib/python3.6/site-packages/pandas/core/arrays/datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   2062             return values.view("i8"), tz_parsed
   2063         except (ValueError, TypeError):
-> 2064             raise e
   2065
   2066     if tz_parsed is not None:

/home/myuser/Software/Tensorflow/environments/venv36/lib/python3.6/site-packages/pandas/core/arrays/datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
   2053             dayfirst=dayfirst,
   2054             yearfirst=yearfirst,
-> 2055             require_iso8601=require_iso8601,
   2056         )
   2057     except ValueError as e:

pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas/_libs/tslib.pyx in pandas._libs.tslib.array_to_datetime()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject()

pandas/_libs/tslibs/np_datetime.pyx in pandas._libs.tslibs.np_datetime.check_dts_bounds()

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-06-13 00:00:00

Option 2: Print Error Message without Traceback with traceback.print_exc(limit=1)

One more alternative solution is to use traceback.print_exc(limit=1). The usage can be found below:

import pandas as pd
import traceback

try:
    
    pd.to_datetime('Jun 1, 1111')

except Exception as e:
    traceback.print_exc(limit=1)
    exit(1)

The result error will be:

Traceback (most recent call last):
  File "/home/vanx/Software/Tensorflow/environments/venv36/lib/python3.6/site-packages/pandas/core/arrays/datetimes.py", line 2059, in objects_to_datetime64ns
    values, tz_parsed = conversion.datetime_to_datetime64(data)
TypeError: Unrecognized value type: <class 'str'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<ipython-input-22-ee6c6bd48a55>", line 5, in <module>
    pd.to_datetime('Jun 1, 1111')
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1111-06-01 00:00:00

Option 3: Print Error Message without Traceback with sys.tracebacklimit

Finally you can decrease the limit of the traceback by: sys.tracebacklimit.

To set the minimum level of traceback use:

import sys

sys.tracebacklimit = 0