Ansible module availability check in python library

Issue - We are having Ansible engine version 2.10 running on RHEL8.9 server for playbook execution on target RHEL VM, We have a custom python filter which is included in an ansible playbook task and that python filter is using two ansible modules(import errors & import sys) and we are not sure how to check this modules are supported for python3 libraries or the available module will support only python2.7 libraries

Regards,
M Giridhar

Well, you can check aavilability by catching the ImportError
exception. Exception from some of my code:

 try:
     from contextlib import nullcontext
 except ImportError:
     @contextmanager
     def nullcontext():
         ''' A simple `nullcontext` for older Pythons
         '''
         yield None

This version checks for the name nullcontext in the contextlib
stdlib module, but you can check for an install module the same way:

 try:
     import something
 except ImportError as e:
     warning("cannot import something: %s", e)
     ... do whatever you intend for when this isn't present ...
     ... example:
     something = None

 ... elsewhere ...
 if something:
     ... use the module ...
 else:
     ... whatever else ...