Software & AppsOperating SystemLinux

Fixing “ModuleNotFoundError: No module named ‘apt_pkg'” after installing Python 3.7 on Ubuntu 18.04

Ubuntu 8

If you’ve recently installed Python 3.7 on your Ubuntu 18.04 machine and encountered the ModuleNotFoundError: No module named 'apt_pkg' error, you’re not alone. This error is common and can be fixed with a few steps. In this article, we’ll walk you through the solutions to resolve this issue.

Understanding the Error

Before we dive into the solutions, it’s important to understand the error. The ModuleNotFoundError: No module named 'apt_pkg' error is thrown when the system can’t find the apt_pkg module. This module is a part of the python3-apt package, which is a Python interface to libapt-pkg, the library behind the APT package manager.

This error often occurs when you upgrade the default Python version in Ubuntu. The system’s applications, including the APT package manager, expect the default Python version and may not work correctly with the newer version.

Solution 1: Reinstalling python3-apt

The first solution is to reinstall the python3-apt package. This can be done by running the following command in your terminal:

sudo apt-get install --reinstall python3-apt

Here, sudo gives you superuser privileges, apt-get is the APT package handling utility, install tells it to install a package, --reinstall tells it to reinstall the package if it’s already installed, and python3-apt is the name of the package.

Reinstalling the python3-apt package ensures that the apt_pkg module is installed correctly.

Solution 2: Rolling Back to the Default Python Version

If reinstalling python3-apt doesn’t solve the problem, you can roll back the changes and use the default Python version. Here’s how to do it:

  1. Remove the alternative you added to point python3 to python3.7 by running:
sudo update-alternatives --remove-all python3

In this command, update-alternatives is a utility to maintain symbolic links determining default commands, --remove-all removes all alternatives for a group, and python3 is the group name.

  1. Fix the symlink to point python3 back to the original version (3.6) distributed with Ubuntu 18.04 by running:
sudo ln -sf /usr/bin/python3.6 /usr/bin/python3

Here, ln is the command to create links, -sf forces the creation of the link and makes it symbolic, and /usr/bin/python3.6 and /usr/bin/python3 are the source and target of the link, respectively.

This will ensure that the system uses the default Python version and avoids potential compatibility issues with other applications.

Solution 3: Using python3.7 Explicitly

If you still want to use Python 3.7, you can do so without changing the default Python version. Instead of relying on the python3 command, use python3.7 for everything that requires Python 3.7. For example, when creating a virtual environment, use:

python3.7 -m venv <venv_name>

In this command, python3.7 is the Python version, -m is the option to run library module as a script, venv is the module to create virtual environments, and <venv_name> is the name of your virtual environment.

This approach allows you to work with Python 3.7 without interfering with the system’s default Python version.

Conclusion

It’s generally safe to use the default Python version provided by the distribution, as applications in Ubuntu often depend on it. Upgrading the default Python version can potentially break compatibility with these applications. Therefore, it’s recommended to avoid changing the default Python version unless necessary.

By following the steps outlined in this article, you should be able to resolve the ModuleNotFoundError: No module named 'apt_pkg' error and work with Python 3.7 on your Ubuntu 18.04 machine. If you continue to face issues, consider reaching out to the Python community or the Ubuntu community for further assistance.

Why am I getting the `ModuleNotFoundError: No module named ‘apt_pkg’` error after installing Python 3.7 on Ubuntu 18.04?

This error occurs because the system’s applications, including the APT package manager, expect the default Python version and may not work correctly with the newer version. The apt_pkg module is a part of the python3-apt package, which is a Python interface to libapt-pkg, the library behind the APT package manager.

How can I fix the `ModuleNotFoundError: No module named ‘apt_pkg’` error?

There are a few solutions to fix this error. One solution is to reinstall the python3-apt package by running the command sudo apt-get install --reinstall python3-apt in the terminal. If that doesn’t work, you can roll back to the default Python version by removing the alternative you added to point python3 to python3.7 and fixing the symlink to point python3 back to the original version (3.6) distributed with Ubuntu 18.04. Another option is to use python3.7 explicitly instead of relying on the python3 command.

Is it safe to upgrade the default Python version in Ubuntu?

It’s generally recommended to avoid changing the default Python version unless necessary. Applications in Ubuntu often depend on the default Python version provided by the distribution, and upgrading it can potentially break compatibility with these applications.

Leave a Comment

Your email address will not be published. Required fields are marked *