Вы можете поместить код внутри блока try, except.
$ python3 -c "\
try:
import cow
print('\nModule was installed')
except ImportError:
print('\nThere was no such module installed')"
There was no such module installed
$ python3 -c "\
try:
import regex
print('\nModule was installed')
except ImportError:
print('\nThere was no such module installed')"
Module was installed