3

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!

polmonroig
  • 937
  • 4
  • 12
  • 22
  • 1
    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. – cvanelteren Dec 22 '18 at 10:27
  • 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 – polmonroig Dec 22 '18 at 10:48
  • Did you read up on how to [uninstall anaconda](https://docs.anaconda.com/anaconda/install/uninstall/)? – cvanelteren Dec 22 '18 at 10:49

5 Answers5

5

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.

skratchi.at
  • 1,151
  • 7
  • 22
mixify
  • 51
  • 1
  • 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 OS – polmonroig Mar 22 '19 at 09:00
  • 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. – Grwlf Oct 19 '20 at 15:32
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.

Community
  • 1
  • 1
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.

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45
Yu.Dolphin
  • 11
  • 1
  • It is really a mess when the system breaks... Now I always have a data backup and a disk partition recovery with Clonezilla. – polmonroig Sep 24 '20 at 08:26
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
0

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

James Danforth
  • 781
  • 7
  • 7