Python: “Cannot import module” issue and installing needed Python bits.

You have your Python program in front of you. You have already made it executable with chmod +x and you are ready to run it. But, when you execute your program via terminal typing something like python you will be greeted with an error message saying something similar to this:

“Cannot import module X, no such module.”

The failure to import module means that you are basically missing a piece which Python requires in order to run the program at hand. Even if you have done the installation of python, python3 or any similar metapackage there is a good chance that you might be still missing something essential.

Below is my current list of essential Python metapackages and modules, which you might want to consider adding if you have any problems. If you are using one of my Python programs then I would strongly recommend installing the packages listed below – so that any difficulties could be avoided. Do note that this list has been composed with Debian base and you  might need to check and change some package names depending upon your distribution.

#General metapackages/packages

sudo apt-get install python-gi python3-gi python-gst-1.0 python-gst0.10 python3-gst-1.0 python-pyalsa python-alsaaudio python3 python-bs4 python-minimal python

#Important module packages

sudo apt-get install gir1.2-webkit* xdg-utils gir1.2-gtk-2.0 gir1.2-gtk-3.0 gir1.2-gst-plugins-base-1.0 gir1.2-clutter-gst-2.0

sudo apt-get install gir1.2-poppler-0.18 gir1.2-pango-1.0 gir1.2-gtk-2.0 gir1.2-gtkclutter-1.0 gir1.2-json-1.0 gir1.2-javascriptcoregtk-3.0

sudo apt-get install gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0 gir1.2-gsf-1 gir1.2-clutter-1.0 gir1.2-clutter-gst-2.0 gir1.2-gdkpixbuf-2.0 gir1.2-glib-2.0

#Terminal. This module is needed if you use python based terminals, which usually require vte.

sudo apt-get install python-vte

#The list below is imported from Crosslinker FM v.5

sudo apt-get install python python3 python-gi python3-gi python-minimal python-gobject python-pygame python-eyed3 python-pymad python-magic xfce4-notifyd

Once you install all the above you should be pretty well seated to run a wide variety of Python programs.