ModuleNotFoundError: No module named 'dnf' when running yum or dnf

12,270

Solution 1

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.

Solution 2

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.

Share:
12,270
polmonroig
Author by

polmonroig

I am working as a Data Scientist at HP and I am a programming enthusiasts.

Updated on June 26, 2022

Comments

  • polmonroig
    polmonroig almost 2 years

    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!

    • cvanelteren
      cvanelteren over 5 years
      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.
    • polmonroig
      polmonroig over 5 years
      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 ? @GlobalTraveler
    • cvanelteren
      cvanelteren over 5 years
      Did you read up on how to uninstall anaconda?
  • polmonroig
    polmonroig about 5 years
    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 OS
  • polmonroig
    polmonroig over 3 years
    It is really a mess when the system breaks... Now I always have a data backup and a disk partition recovery with Clonezilla.
  • Grwlf
    Grwlf over 3 years
    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.