Build error (ModuleNotFoundError: No module named 'elftools')

Sometimes it’s good to write down the errors that happen, even if I was able to troubleshoot the error. I saw the following when building the Golioth “hello” sample on the command line:

3/365] Generating include/generated/version.h
-- Zephyr version: 3.4.99 (/home/chrisg/golioth/latest/zephyr), build: v3.4.99-ncs1-2
[6/365] Generating include/generated/kobj-typ...e-to-str.h, include/generated/otype-to-size.h
FAILED: zephyr/include/generated/kobj-types-enum.h zephyr/include/generated/otype-to-str.h zephyr/include/generated/otype-to-size.h /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/kobj-types-enum.h /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/otype-to-str.h /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/otype-to-size.h 
cd /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr && /home/chrisg/golioth/latest/.venv/bin/python3 /home/chrisg/golioth/latest/zephyr/scripts/build/gen_kobject_list.py --kobj-types-output /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/kobj-types-enum.h --kobj-otype-output /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/otype-to-str.h --kobj-size-output /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/otype-to-size.h --include-subsystem-list /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/misc/generated/struct_tags.json
Traceback (most recent call last):
  File "/home/chrisg/golioth/latest/zephyr/scripts/build/gen_kobject_list.py", line 62, in <module>
    import elftools
ModuleNotFoundError: No module named 'elftools'
[7/365] Generating include/generated/driver-validation.h
FAILED: zephyr/include/generated/driver-validation.h /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/driver-validation.h 
cd /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr && /home/chrisg/golioth/latest/.venv/bin/python3 /home/chrisg/golioth/latest/zephyr/scripts/build/gen_kobject_list.py --validation-output /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/include/generated/driver-validation.h --include-subsystem-list /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build/zephyr/misc/generated/struct_tags.json
Traceback (most recent call last):
  File "/home/chrisg/golioth/latest/zephyr/scripts/build/gen_kobject_list.py", line 62, in <module>
    import elftools
ModuleNotFoundError: No module named 'elftools'
[8/365] Generating include/generated/syscall_dispatch.c, include/generated/syscall_list.h
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/chrisg/golioth/latest/modules/lib/golioth-firmware-sdk/examples/zephyr/build

Normally when I’m troubleshooting Zephyr build errors (especially on the command line) I look at the most recent error. In this case that would be ModuleNotFoundError: No module named 'elftools'

If it’s not able to find elftools, that probably means that it doesn’t see the build tools generally. I had a Python virtual environment already active, so I went back through and made sure i ran the following two commands from our Zephyr setup guide:

  • west zephyr-export
  • pip install -r ~/golioth-zephyr-workspace/zephyr/scripts/requirements.txt

I had created my Zephyr build in a different location, so for me, I went to the top level folder called latest and modified for the zephyr folder there:

pip install -r zephyr/scripts/requirements.txt

All we’re trying to do is to get pip to install additional dependencies to work in conjunction with the west meta tool.

Once I ran the command and saw all the dependencies installing, it made me think that I probably just skipped this step in the past. The build worked great after that.