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:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.