How to Fix ‘Module Not Found’ Error After Upgrade in Ubuntu

Upgrading an Ubuntu system to a newer distribution release (such as moving from Ubuntu 20.04 LTS to 22.04 LTS or 24.04 LTS) brings improved security, modern kernel versions, and upgraded system packages. However, developers and system administrators frequently discover that existing scripts and automated services suddenly fail with the fatal exception: ModuleNotFoundError: No module named ‘xyz’ or modprobe: FATAL: Module xyz not found.

This failure occurs because distribution upgrades often transition the default Python interpreter to a newer minor version (for example, upgrading from Python 3.10 to Python 3.12). System Python packages installed via pip or distribution package managers reside in version-specific directory trees (such as /usr/local/lib/python3.10/dist-packages/). When the system begins invoking Python 3.12, it no longer looks inside the older directory. Below are the steps to restore missing modules in Ubuntu.

Identify Your Active Python Version and Module Directory

Before reinstalling packages, determine which Python interpreter your environment is executing and inspect its library search path:

python3 --version
python3 -c "import sys; print(sys.path)"

Check whether the output paths point to the expected Python version directory. If your scripts explicitly call /usr/bin/python3 while third-party modules were installed under a different user or virtual environment, Python will be unable to resolve imports.

Reinstall Modules via apt or pip with the Current Interpreter

In Ubuntu 24.04 and newer, PEP 668 restricts installing packages globally using pip install to prevent breaking system tools. Reinstall the required packages using native Ubuntu repositories or dedicated virtual environments:

  1. Check if the missing package exists in the Ubuntu repository:
    sudo apt update
    sudo apt install python3-requests python3-yaml python3-pip
  2. If you require packages only available on PyPI, create and activate an isolated Python virtual environment:
    python3 -m venv ~/myproject_env
    source ~/myproject_env/bin/activate
    pip install missing-module-name
  3. Ensure your cron jobs, systemd services, or application scripts invoke the virtual environment interpreter:
    ~/myproject_env/bin/python3 /path/to/script.py

Migrate Packages from Previous Python Directory

If you had numerous custom packages installed under an older Python version, you can generate a list of installed modules from the older site-packages directory and install them into your current environment:

python3.10 -m pip freeze > requirements.txt
python3.12 -m pip install -r requirements.txt

Alternatively, inspect the contents of /usr/local/lib/ to see which legacy Python folders contain unmigrated packages:

ls /usr/local/lib/

Fix Kernel ‘Module Not Found’ Errors via DKMS

If the “Module Not Found” error occurs during boot or hardware initialization (such as Nvidia GPU drivers, VirtualBox guest additions, or WireGuard kernel modules), the kernel modules were compiled against an older kernel header that was retired during the distribution upgrade:

  1. Reinstall DKMS (Dynamic Kernel Module Support) and kernel headers matching your running kernel:
    sudo apt install --reinstall dkms linux-headers-$(uname -r)
  2. Trigger DKMS to automatically rebuild all third-party kernel modules:
    sudo dkms autoinstall
  3. Reboot your system to load the newly compiled kernel drivers:
    sudo reboot

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.