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
  • Please edit your question to clearly describe exactly (step-by-step) how you "reinstall[ed] python and ufw." The details matter. If you were following online instructions, please provide a link. Also, please describe the issue you encountered that made such a reinstall seems desirable. – user535733 Nov 23 '19 at 15:16
  • I added it to my original question. – Bobface Nov 23 '19 at 19:10
  • Hmmm. Purging python3 should have broken apt, rendering apt impotent and incapable of installing (or reinstalling) anything. If your apt still functioned, then something else is going on that we don't know about. – user535733 Nov 23 '19 at 19:15
  • Tbh, I do not remember if I used purge or not. It was a few days ago. Quite possible I did not, since you say it would have rendered apt unusable. I will remove it from the question. – Bobface Nov 23 '19 at 19:28
  • I suggest looking in /var/log/apt/history.log. It will tell you what you did, and what the result was. Copy and paste the entire session into your Question above, if possible. – user535733 Nov 23 '19 at 19:29

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
  • Still getting the same error. – Bobface Nov 23 '19 at 14:20
  • Well, the last resort for you is to backup your data and re-install Ubuntu. By removing python, you might have broken the dependencies of ufw – Tejas Lotlikar Nov 23 '19 at 14:35
  • 2
    Seems unlikely: See packages.ubuntu.com/bionic-updates/ufw . The ufw package in 18.04 is built against Python3, not Python2. Py2 is not a dependency of ufw. – user535733 Nov 23 '19 at 15:12

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
  • There seems to be no package called ufw: ERROR: Could not find a version that satisfies the requirement ufw (from versions: none) ERROR: No matching distribution found for ufw – Bobface Nov 29 '19 at 17:33
  • @thesys, it works perfectly on my machine, so I assume that the problem is with pip3. So, edit the question to include the output of pip3 list – Olimjon Dec 1 '19 at 15:03
  • I added it to the question. Though it seems like pip3 cannot find find a ufw package at all. – Bobface Dec 1 '19 at 16:22
  • @thesys, I updated the answer. Try this command. – Olimjon Dec 1 '19 at 17:11
  • 1
    @thesys, I am sorry, I could not figure it out how to help you in this particular situation (I thought the problem is with pip3, but this was not the case). Check the answer given by @ketan-patel, it will surely help you. – Olimjon Dec 3 '19 at 15:45

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).


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

  • The output is: /usr/lib/python2.7/dist-packages/ufw and /usr/lib/python3/dist-packages/ufw. It just says python3. Which version should I use? – Bobface Dec 3 '19 at 20:10
  • You already have installed python 3.8 so proceed with installing python 2.7. Package is python. – KK Patel Dec 4 '19 at 10:17
  • Python 2.7 is also installed already. – Bobface Dec 4 '19 at 22:09

In ubuntu 18.04, all dependency are automatically handled by package manager. You suppose not to manually install dependency.

It seems like you are using python package which is not compatible with UFW. If you don't have python project running, try to remove python and allow UFW to reinstall with dependency automatically.

Stop the process, if you get message like other package will get removed.

Still problem. Try :

sudo apt install aptitude

sudo aptitude install ufw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.