[SOLVED] Jetbot_ros: Error about missing python module (Adafruit_motorHAT) when running the ROS motor script

I have successfully followed the installation steps in the README found here:

…That is up until testing the motors (or OLED).

The installation of ROS and the rest of the steps went well.

When running the test script the following happens:

jetbot@nano-4gb-jp441:~$ rosrun jetbot_ros jetbot_motors.py
Traceback (most recent call last):
File “/home/jetbot/workspace/catkin_ws/src/jetbot_ros/scripts/jetbot_motors.py”, line 5, in
from Adafruit_MotorHAT import Adafruit_MotorHAT
ImportError: No module named Adafruit_MotorHAT

Even though I have installed already installed the Adafruit_motorHAT module, I tried again and receive the following:

jetbot@nano-4gb-jp441:~$ sudo pip install Adafruit_MotorHAT
[sudo] password for jetbot:
WARNING: The directory ‘/home/jetbot/.cache/pip’ or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
Requirement already satisfied: Adafruit_MotorHAT in /usr/local/lib/python3.6/dist-packages/Adafruit_MotorHAT-1.4.0-py3.6.egg (1.4.0)
Requirement already satisfied: Adafruit-GPIO>=0.7 in /usr/local/lib/python3.6/dist-packages/Adafruit_GPIO-1.0.4-py3.6.egg (from Adafruit_MotorHAT) (1.0.4)
Requirement already satisfied: adafruit-pureio in /usr/local/lib/python3.6/dist-packages/Adafruit_PureIO-1.1.8-py3.6.egg (from Adafruit-GPIO>=0.7->Adafruit_MotorHAT) (1.1.8)
Requirement already satisfied: spidev in /usr/local/lib/python3.6/dist-packages/spidev-3.5-py3.6-linux-aarch64.egg (from Adafruit-GPIO>=0.7->Adafruit_MotorHAT) (3.5)
WARNING: You are using pip version 20.3.3; however, version 21.0.1 is available.
You should consider upgrading via the ‘/usr/bin/python3 -m pip install --upgrade pip’ command.

The solution was to check my PYTHONPATH variable by executing:

“echo $PYTHONPATH”

This showed me it pointed to python2.7 and not python3 where pip had been installing Adafruit_MotorHAT.

Therefore I executed the following command to install Adafruit_MotorHAT for python2.7:

“python2.7 -m pip install Adafruit_MotorHAT”

This then solved my problem.