Python ModuleNotFoundError: No Module Named ‘rpi’

Quick Fix: Python raises the ImportError: No module named 'RPi.GPIO' when it cannot find the library rpi.gpio. The most frequent source of this error is that you haven’t installed rpi.gpio explicitly with pip install python3-rpi.gpio for Python 3 or pip install python-rpi.gpio for Python 2. Alternatively, you may have different Python versions on your computer, and rpi is not installed for the particular version you’re using.

In particular, you can try any of the following commands, depending on your concrete environment and installation needs.

πŸ’‘ If you have only one version of Python installed:
pip install python3-rpi.gpio

πŸ’‘ If you have Python 3 (and, possibly, other versions) installed:
pip3 install python3-rpi.gpio

πŸ’‘ If you don't have PIP or it doesn't work
python -m pip install python3-rpi.gpio
python3 -m pip install python3-rpi.gpio

πŸ’‘ If you have Linux and you need to fix permissions (any one):
sudo pip3 install python3-rpi.gpio
pip3 install python3-rpi.gpio --user

πŸ’‘ If you have Linux with apt
sudo apt install python3-rpi.gpio

πŸ’‘ If you have Windows and you have set up the py alias
py -m pip install python3-rpi.gpio

πŸ’‘ If you have Anaconda
conda install -c anaconda python3-rpi.gpio

πŸ’‘ If you have Jupyter Notebook
!pip install python3-rpi.gpio
!pip3 install python3-rpi.gpio

I have to admit that in 99.99% of cases, most commands won’t help because it is highly unlikely that you’ll install the RPI or RPI.gpio libraries on Windows or even Anaconda!

Here are the official installation instructions that are often missed by new coders just starting out with the Raspberry.

πŸ‘‰ The RPi.GPIO package controls the GPIO on a Raspberry Pi. GPIO is short for “general-purpose input/output” which is an uncommitted digital signal pin on an integrated circuit board used as an input or output, or both, and is controllable by software. The RPi.GPIO module is preinstalled per default in Raspbian.

You can make sure it is updated to its newest version using these two commands:

$ sudo apt-get update
$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

Don’t copy and paste the $ symbol, it’s only there to show that you run this code in a terminal and not in the Python shell.

Here’s how to install the latest development version from the project source:

$ sudo apt-get install python-dev python3-dev
$ sudo apt-get install mercurial
$ sudo apt-get install python-pip python3-pip
$ sudo apt-get remove python-rpi.gpio python3-rpi.gpio
$ sudo pip install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO
$ sudo pip-3.2 install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO

Copy and paste one command at a time, ignoring the $ symbols.

In case you feel you corrupted your installation, here’s how to revert back to the default version in Raspbian:

$ sudo pip uninstall RPi.GPIO
$ sudo pip-3.2 uninstall RPi.GPIO
$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

Understanding the “import” Statement

import rpi

In Python, the import statement serves two main purposes:

  • Search the module by its name, load it, and initialize it.
  • Define a name in the local namespace within the scope of the import statement. This local name is then used to reference the accessed module throughout the code.

What’s the Difference Between ImportError and ModuleNotFoundError?

What’s the difference between ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

You can see this in this screenshot from the docs:

You can also check this relationship using the issubclass() built-in function:

>>> issubclass(ModuleNotFoundError, ImportError)
True

Specifically, Python raises the ModuleNotFoundError if the module (e.g., rpi) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an ImportError.

If an import statement cannot import a module, it raises an ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a ModuleNotFoundError.

Related Videos

The following video shows you how to resolve the ImportError:

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError: