Error Message "No module named'dnf'" Is Displayed When Checking the Source or Installation Dependency

Symptom

Run the yum makecache or yum install -y xxx command to install a dependency package. The following error information is displayed:

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

Possible Causes

Yum depends on the default Python 3 version. Python 3 downloaded and installed by users does not support Yum.

Solution

  • To delete the Python environment variables written into the ~/.bashrc file, perform the following steps:
    1. Run the vi ~/.bashrc command in any directory on the server to delete the Python environment variables. Information similar to the following (an example) is displayed. Delete the information based on the actual Python environment variables.
      1
      2
      export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
      export PATH=/usr/local/python3.7.5/bin:$PATH
      
    2. Run the following command for the deletion to take effect:
      1
      source ~/.bashrc
      
  • If you set Python environment variables in export mode, the environment variables set in this mode are valid only in the current window. In this case, open another window and run yum commands.

    After running the yum command in another window, return to the previous window to avoid Python environment errors.