Dnf got corrupted at some point

At some point over the past years, dnf stopped working for me. I don’t recall the details at this point; only that when I attempt to use dnf I see the following error.
$ dnf
Traceback (most recent call last):
File “/usr/bin/dnf”, line 57, in
from dnf.cli import main
File “/usr/local/lib/python3.9/site-packages/dnf/init.py”, line 30, in
import dnf.base
File “/usr/local/lib/python3.9/site-packages/dnf/base.py”, line 29, in
import libdnf.transaction
ModuleNotFoundError: No module named ‘libdnf’

My current release is Fedora 4.20.13-200.fc29.x86_64 and I’d like to upgrade to the fc33, but would like to update other stuff as well and will need a working dnf to do so. How can I restore dnf so that it works correctly?

You can try to install the package that is said to be missing.
Maybe yum still works?
yum whatprovides libdnf
yum install libdnf

It looks like yum uses the same libdnf package…

$ yum whatprovides libdnf
Traceback (most recent call last):
File “/usr/bin/yum”, line 57, in
from dnf.cli import main
File “/usr/local/lib/python3.9/site-packages/dnf/init.py”, line 30, in
import dnf.base
File “/usr/local/lib/python3.9/site-packages/dnf/base.py”, line 29, in
import libdnf.transaction
ModuleNotFoundError: No module named ‘libdnf’
[riwarren@rawlinux python3.9]$

First suggestion: Back up your files and do a clean install.

If that is not an option, download this and install it with rpm -i path-of-downloaded-file command:

If you need other dnf packages:

2 Likes

System packages never install into /usr/local, so the error with

/usr/local/lib/python3.9/site-packages/dnf/init.py

is concerning. Did you try to build or install DNF not as a package at some point, or did you install a different version of python via some method other than rpm?

2 Likes

I did install a version of python from python.org
I’ve also been attempting to restore the dnf functionality over time, so it is possible I’ve made things worse.

You probably can switch back to your default python version.
It should still be installed.

Have a look here

or there:

You can use whereis python or locate to find your installation which should be under /usr/bin/python or exactly /usr/bin/python3.9.

You can use the alternatives command or modify .bashrc to point to the default installation - which should include a folder named libdnf.