I just used Yum to install Git on my Linux AWS EC2 instance using the following commands: πΌ
# Perform update on your instance: sudo yum update -y # Install git in your EC2 instance sudo yum install git -y # Check git version git version
Error
But when running these commands in my shell, I got the error messages ModuleNotFoundError: No module named 'dnf'
: π‘
[ec2-user@ip-172-31-19-40 ~]$ sudo yum update -y
Traceback (most recent call last):
File "/usr/bin/yum", line 61, in <module>
from dnf.cli import main
ModuleNotFoundError: No module named 'dnf'
[ec2-user@ip-172-31-19-40 ~]$ sudo yum install git -y
Traceback (most recent call last):
File "/usr/bin/yum", line 61, in <module>
from dnf.cli import main
ModuleNotFoundError: No module named 'dnf'
The web didn’t provide good answers, so I thought I share my resolution in this short tutorial:
The problem was that I had just set the command python3
to my new Python installation 3.11, as explained in this tutorial:
β‘ Tutorial: How to Use Python 3.X (3.9, 3.10, or 3.11) as Default for βpython3β in Linux?
Solution
Per default, python3
pointed to Python installation 3.9. While I did install Python 3.11 freshly on my Linux, I obviously had not installed the module 'dnf'
for the new Python version. But as yum
now used (uninitialized) Python 3.11 instead of (correctly initialized) Python 3.9, it raises the ModuleNotFoundError: No module named 'dnf'
.
To fix this, you can simply set Python to the previous (correctly initialized) version 3.9 by running the following command:
sudo update-alternatives --config python3
In my case, I needed to select 1
to reset the command python3
to the old and correctly initialized Python version 3.9.
Now if I use Yum, everything works properly as set up by Amazon per default:
Of course, you can always reset Python3 to the new version again by running the same command again and choosing the other version:
sudo update-alternatives --config python3
If you want to keep learning Python and tech, feel free to join my newsletter by downloading our free cheat sheets here: