In my case I am using pyenv to manage my python versions and $PATH and symlinks were all correct pointing to the correct paths.
Check your python paths
$ pyenv which python
/Users/<username>/.pyenv/versions/3.7.3/bin/python
$ which python
/Users/<username>/.pyenv/shims/python
Check ansible configuration
ansible configuration at /usr/local/bin/ansible pointed the correct python version 3.7
#!/usr/local/opt/python/bin/python3.7
.
.
.
but ansible --version returned python 2.7 as its interpreter
$ ansible --version
ansible 2.9.12
configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/<username>/.pyenv/versions/2.7.16/lib/python2.7/site-packages/ansible
executable location = /Users/<username>/.pyenv/versions/2.7.16/bin/ansible
python version = 2.7.16 (default, Apr 2 2020, 13:02:51) [Clang 11.0.3 (clang-1103.0.32.29)]
Ansible for Python3
Official ansible docs said to use pip3 to install ansible for python3 and I uninstalled ansible and reinstalled using pip3 but the interperter still pointed to python2.7.
Finally I manually added .ansible.cfg file in my home path and configured python interpreted manually by adding
ansible_python_interpreter=/usr/bin/python
Example config file for ansible.cfg
Now ansible is configured for python3 correctly
ansible 2.9.12
config file = /Users/<username>/.ansible.cfg
configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/<username>/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ansible
executable location = /Users/<username>/.pyenv/versions/3.7.3/bin/ansible
python version = 3.7.3 (default, Apr 2 2020, 13:02:51) [Clang 11.0.3 (clang-1103.0.32.29)]