Is there a way to customize error messages shown by PyInputPlus?

I’ve recently learned about PyInputPlus and how to use it. It is really awesome, but I am wondering is there a way to customize error messages that PyInputPlus shows when user enters invalid values. For example, in the code below

import pyinputplus as pyip
response=pyip.inputInt('please, enter a number: ')

if a user enters letter ‘s’, PyInputPlus shows –

‘s’ is not an integer

I would like to cutomize that message, to show it in a different (non-english) language. I tried to find the solution in the official documentation at PyInputPlus but found nothing except using inputCustom which I am not interested in.


To start, I would highly discourage using 3rd-party libraries whenever possible, because they will add faults you do not understand to your code. Many are extraordinarily useful, are well-supported, and/or have some feature it would take you an age to reproduce.

However, you can use int(input()) for this case, so do so!
(it will behave much more the way you expect)

Digging in the code, the validation actually calls another 3rd-party library by the same author

This looks like it goes to the code path here where the real Exception is and then goes here to be caught


while True:
    value = input("Please enter a number (q to quit): ")
    if value.startswith(('Q', 'q')):
        value = int(value)
    except ValueError as ex:  # custom error message below!
        print("invalid input '{}', expected int".format(value))
    else:  # did not raise Exception
        break  # escape while loop

If you’re ever looking for what a specific Exception is, repr() is very useful

>>> try:
...     raise ValueError("some string")
... except IOError:
...     print("reached IOError")
... except Exception as ex:
...     print("unexpected Exception: {}".format(repr(ex)))
...     raise ex
unexpected Exception: ValueError('some string')
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "<stdin>", line 2, in <module>
ValueError: some string

Leave a Reply

Your email address will not be published. Required fields are marked *