Running existing Python automation scripts, web services, or developer tools in Ubuntu only to be greeted by the error ModuleNotFoundError: No module named '[modulename]' immediately after upgrading your Ubuntu release or installing a new Python minor version (such as moving from Python 3.10 to Python 3.12) is a frequent Linux development breakdown. In Ubuntu, third-party Python packages installed via pip or apt are bound strictly to specific minor version directory trees (e.g. /usr/local/lib/python3.10/dist-packages/). When the system Python interpreter increments, the new interpreter does not automatically inherit libraries from the older version.
Identify Your Running Python Version and Module Search Paths
Before reinstalling packages, verify which Python interpreter is executing your script and examine where it seeks library dependencies.
- Open a Terminal window (press Ctrl + Alt + T).
- Check the active default Python version:
python3 --version - Inspect Python’s module search path (
sys.path) by running:python3 -c "import sys; print('\n'.join(sys.path))" - Notice the versioned directory paths listed (for example,
/usr/lib/python3.12). If your missing module is installed inside an olderpython3.10directory, the new interpreter cannot see it.
Reinstall Modules for the Upgraded Python Version via apt
If you previously installed Python modules using the system package manager, install the versioned packages for the new release.
- Update your local package lists:
sudo apt update - Install the Python 3 development tools and pip for your current release:
sudo apt install -y python3-pip python3-dev - Install system-level libraries using apt (e.g. for requests or numpy):
sudo apt install -y python3-requests python3-numpy
Understand PEP 668 and Rebuild Isolated Virtual Environments
Ubuntu 23.04, 24.04 LTS, and Debian 12 enforce PEP 668 (“externally managed environment”), preventing users from running pip install directly on system Python. The sanctioned, robust solution is using a virtual environment.
- Install the virtual environment package:
sudo apt install -y python3-venv - Navigate to your project directory:
cd ~/my-project/ - Create a fresh virtual environment tied cleanly to the upgraded Python release:
python3 -m venv .venv - Activate the virtual environment:
source .venv/bin/activate - Install your project requirements cleanly:
pip install -r requirements.txt - Execute your script inside the activated virtual environment without encountering version or path mismatches.
Manage Alternative Python Versions via update-alternatives
If an essential legacy script strictly requires an older Python interpreter, configure update-alternatives to switch interpreters cleanly.
- Register both Python versions with update-alternatives:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2 - Switch between interpreters on demand:
sudo update-alternatives --config python3 - Select the desired version number to re-link
/usr/bin/python3.