ModuleNotFoundError: No module named 'dnf' when running yum or dnf
Asked Answered
G

7

8

A few days ago I wanted to run dnf but I received the following error

Traceback (most recent call last):
  File "/usr/bin/dnf", line 57, in <module>
    from dnf.cli import main
ModuleNotFoundError: No module named 'dnf'

Thus I tried to run yum and I received something similar:

Traceback (most recent call last):
  File "/usr/bin/yum", line 57, in <module>
    from dnf.cli import main
ModuleNotFoundError: No module named 'dnf'

I do not know what happened and I hope someone can help me find the problem and solve it. The only thing I know is that it might be related to the fact that a few days before I installed tensorflow in python using conda. Although it seems conda does not work now because I get this:

Traceback (most recent call last):
  File "/usr/bin/conda", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3126, in <module>
    @_call_aside
  File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3110, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3139, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 581, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 898, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 784, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'conda==4.5.11' distribution was not found and is required by the application

I am using fedora 29 Thanks!

Gentianaceous answered 22/12, 2018 at 10:6 Comment(3)
The error is raised as you mixed a virtual env with the system python. It is recommended to keep this separated and activate the virtual env when needed. Concretely, fedora has a system python and you installed anaconda version of python. When running yum it looks for a python version. The system python has the module dnf, but the anaconda version doesn't. Hence the error. Uninstall anaconda and setup the environment correctly to prevent future issues like this.Peroneus
Okey, but how can I uninstall anaconda if I get the third error when I try to run any conda command like conda - conda uninstall ? @GlobalTravelerGentianaceous
Did you read up on how to uninstall anaconda?Peroneus
F
7

I had similar problem on Rocky Linux 9 Docker image (with pre-installed Python 3.9) when installing Python 3.11 from packages.

dnf install python3.11

python3.11 executable worked fine, but when setting a symlink python3.11 -> python3

$ ls -l /usr/bin/pyth*
lrwxrwxrwx 1 root root    16 Sep 19 12:53 /usr/bin/python -> /usr/bin/python3
lrwxrwxrwx 1 root root    19 Sep 19 12:53 /usr/bin/python3 -> /usr/bin/python3.11
-rwxr-xr-x 1 root root 15448 Jun 23 06:04 /usr/bin/python3.11
-rwxr-xr-x 1 root root 15440 Apr 17 23:46 /usr/bin/python3.9

dnf stopped working with ModuleNotFoundError: No module named 'dnf' error because its Python modules are installed in /usr/lib/python3.9/site-packages/dnf directory.

Solution: for me helped to replace default #!/usr/bin/python3 shebang in /usr/bin/dnf Python script with a specific one #!/usr/bin/python3.9:

sed -i 's|#!/usr/bin/python3|#!/usr/bin/python3.9|g' /usr/bin/dnf

Then with

$ head -1 /usr/bin/dnf
#!/usr/bin/python3.9

it works fine:

$ dnf --version
4.14.0
From answered 19/9, 2023 at 13:21 Comment(1)
This worked after I manually compiled and installed Python 3.11.Presentday
A
6

I had the same problem, but after all trial and error the only thing actually worked was:

rpm -qa python3

This will announce your python3 rpm package.

I've searched and downloaded it from web

Go to the downloaded directory like:

sudo rpm -e --nodeps python3-3.7.2-4.fc29.x86_64(my python3 rpm package)
sudo rpm -i python3-3.7.2-4.fc29.x86_64.rpm

I tried many other methods so this'll not be a only factor that resolve that problem.

Affair answered 22/3, 2019 at 7:46 Comment(2)
Okey, thank you for answering although in the end I didn't found any solution and I was tired of searching, thus I reinstalled my OSGentianaceous
I found this answer useful. One probably need to clarify, that author essentially suggests to uninstall current python3 package and to install it again. This could work if the existing installation was broken for some reason. In my case, it was broken because someone make-installed custom python3 systemwide, which didn't come with dnf package.Africa
U
2

I had the same problem on Linux master01 4.19.90-17.5.ky10.aarch64, It may be because the first line of configuration in /usr/bin/yum is incorrect and needs to be modified to the correct python path.

vim /usr/bin/yum
#!/usr/bin/python -> #!/usr/bin/python2 or #!/usr/bin/python3

If the path is normal, then there may be a lack of dnf related dependencies. This is very complicated, you can refer toHere's my fix

Unsightly answered 7/9, 2023 at 2:50 Comment(0)
D
1

Hello,

i have just been through this in Fedora 31 and do not exactly know what was the cause that led to this same issue, it must have been some package managing over another context that came up only on my next need to use DNF.

I am assuming that before this stated error:

Traceback (most recent call last):
  File "/usr/bin/dnf", line 57, in 
    from dnf.cli import main
ModuleNotFoundError: No module named 'dnf'

You were having this message when executing dnf:

/usr/local/lib/python3.7/site-packages/dnf.py:15: UserWarning: The DNF Python API is not currently available via PyPI.

Please install it with your distro package manager (typically called
'python2-dnf' or 'python3-dnf'), and ensure that any virtual environments
needing the API are configured to be able to see the system site packages
directory.

After some hours researching and trying to install each DNF rpm dependency of the DNF rpm itself(https://pkgs.org/download/dnf) i felt as an intuition that maybe i should try to remove(mv sure)

/usr/local/lib/python3.7/

which had just one folder in it named site-packages

It worked!

Thank you.

Dreamland answered 29/3, 2020 at 15:13 Comment(0)
S
1

had the same error.

my fix ->

cd into /usr/bin. has many python files...

latest python (3.11) has pytest, python3 -> python3.11 (symlink), python3.11, python3.11-config, python3.11-x86_64-config, python -> python3

had been using 3.9, not familiar with python so much, so changed symlink back to python3->python3.9 after Fedora 36 update changed symlink to 3.11 and installed python3.11. when i changed symlink back to 3.9, this created the error message. When i changed the symlink back to python3->python3.11 the error disappeared and all works fine. DNF is fine etc. did nothing else.

since all the other python files were pointing to the 3.11 versions, introduction of the symlink to 3.9 must have blown the fuse...

Seventieth answered 20/2, 2023 at 0:26 Comment(0)
G
0

I have a friend who met the same problem.

He tried to uninstall python3.7 in linux server by some amazing cmd rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps and whereis python3 |xargs rm -frv.
This caused the yum and dnf to break.

I changed the /usr/bin/yum to use a local python3.8 version, but it caused ModuleNotFoundError: No module named 'dnf' at last, and didn't solved it.

It is not a good way to download many rpm packages on the Internet. And I can't use yum either. But I have another same linux OS server, so I've tried to copy the correlation files about python3.7.

$ rpm -ql python3-3.7.0-9.h4.eulerosv2r8.aarch64> py.log
$ while read -r line;do dirname $line |xargs -I {} ssh root@$remoteip "mkdir -p {}" ;scp $line root@$remoteip:$line  ;done<py.log

$ rpm -ql python3-libs-3.7.0-9.h4.eulerosv2r8.aarch64 >pylib.log
$ while read -r line;do dirname $line |xargs -I {} ssh root@$remoteip "mkdir -p {}" ;scp $line root@$remoteip:$line  ;done<pylib.log

scp -r /usr/lib/python3.7/site-packages root@$remoteip:/usr/lib/python3.7/

I recovered yum this way.

Gusher answered 24/9, 2020 at 7:12 Comment(1)
It is really a mess when the system breaks... Now I always have a data backup and a disk partition recovery with Clonezilla.Gentianaceous
H
0

I had the same issue. For me the solution was to simply uninstall the python3 dnf package (including from all virtualenvs) that I had obviously installed somewhen.

pip uninstall dnf
Humphrey answered 28/12, 2020 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.