Module not found error with multiple versions of Python on Mac M2

I am using M2 Pro Sonoma 14.5 and new to Python. Python used to work earlier.Installed another version using brew ,from then python programs does not work. I always get module not found error.

Tried uninstalling Homebrew version ,still it does not work.

I followed the following link from the forum

which -a Python

/opt/homebrew/bin/python3

/usr/bin/python3

python3

Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

import sys

print(sys.version)

3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)]

pip -V

pip 24.0 from /opt/homebrew/lib/python3.11/site-packages/pip (python 3.11)

Environment:

M2 Pro Sonoma 14.5

How can I switch to the 3.11 which from 3.12.4 or vice versa fix the module not found error.

Please read: