Unable to use UFW after reinstalling python

After reinstalling python and ufw I get the following error:

user@machine:~$ sudo ufw status
Traceback (most recent call last):
  File "/usr/sbin/ufw", line 25, in <module>
    import ufw.frontend
ModuleNotFoundError: No module named 'ufw'

I have tried to reinstall ufw multiple times but I don't get it to work.

What I have noticed about pip is that sudo uses pip2 instead of pip3 but also uses python3 as python:

user@machine:~$ pip -V
pip 19.3.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)
user@machine:~$ sudo pip -V
pip 19.3.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
user@machine:~$ sudo -i
root@machine:~# pip -V
pip 19.3.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

user@machine:~$ python -V
Python 3.8.0
user@machine:~$ sudo python -V
Python 3.8.0
user@machine:~$ sudo -i
root@machine:~# python -V
Python 3.8.0

I am not sure if that causes the problems though.

Apt history:

apt remove --purge python3-pip # This broke ufw probably because --purge
apt remove python3
apt install python3 
apt remove python3 # Noticed outdated version, uninstall again and built from source afterwards
apt install python3-pip # Was done after building and installing python3 from source

sudo pip3 list:

Package    Version
---------- ---------
certifi    2019.9.11
chardet    3.0.4
idna       2.8
pip        19.3.1
requests   2.22.0
setuptools 41.2.0
urllib3    1.25.7

python3 -c 'import sys; print(sys.version); print(sys.path)':

3.8.0 (default, Dec  1 2019, 09:38:21)
[GCC 7.4.0]
['', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/                                                      python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages']

ls /usr/lib/python3/dist-packages/ufw/

applications.py  backend_iptables.py  backend.py  common.py  frontend.py  __init__.py  parser.py  __pycache__  util.py

You are not supposed to remove python from Ubuntu because a lot of packages are built on it. Since you have already installed python, please execute the commands listed below to reinstall ufw

sudo apt-get purge --auto-remove ufw
sudo apt-get install ufw

It seems ufw was removed from pip3, when you have uninstalled python3-pip

Try this to reinstall it:

sudo pip3 install https://launchpad.net/ufw/0.36/0.36/+download/ufw-0.36.tar.gz

It looks like python version ufw was using is removed and installed version is different than it was, So ufw is reporting error.

You can traceback which python version ufw is using by following command.

sudo find /usr/lib/ -name "ufw"

For example, You will have output similar below (exact depends on your python version ufw is using).

/usr/lib/python3.6/site-packages/ufw

In above example, ufw is using python 3.6. You need to re-install particular python version according to your case.