[Solved] Ubuntu22.04 of OpenHarmony executes hb set error

Error message

Recently, when using Ubuntu22.04 to build the OpenHarmony compilation environment, the following error occurred when executing the hb set command to select the development board:

The detailed log is as follows:

fangye@fangye-virtual-machine:~/niobeu4_src$ hb set
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/hb/__main__.py", line 84, in main
[OHOS ERROR] status = args.command(args)
[OHOS ERROR] File "/home/fangye/niobeu4_src/build/lite/hb_internal/set/set.py", line 45, in exec_command
[OHOS ERROR] return set_product() == 0
[OHOS ERROR] File "/home/fangye/niobeu4_src/build/lite/hb_internal/set/set.py", line 62, in set_product
[OHOS ERROR] product_info = Product.product_menuconfig()
[OHOS ERROR] File "/home/fangye/niobeu4_src/build/lite/hb_internal/common/product.py", line 260, in product_menuconfig
[OHOS ERROR] product = menu.list_promt('product', 'Which product do you need?',
[OHOS ERROR] File "/home/fangye/niobeu4_src/build/lite/hb_internal/cts/menuconfig.py", line 34, in list_promt
[OHOS ERROR] return self._promt(questions, **kwargs)
[OHOS ERROR] File "/home/fangye/niobeu4_src/build/lite/hb_internal/cts/menuconfig.py", line 25, in _promt
[OHOS ERROR] prompt = importlib.import_module('hb_internal.cts.prompt')
[OHOS ERROR] File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
[OHOS ERROR] return _bootstrap._gcd_import(name[level:], package, level)
[OHOS ERROR] File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
[OHOS ERROR] File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
[OHOS ERROR] File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
[OHOS ERROR] File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
[OHOS ERROR] File "<frozen importlib._bootstrap_external>", line 883, in exec_module
[OHOS ERROR] File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
[OHOS ERROR] File "/home/fangye/niobeu4_src/build/lite/hb_internal/cts/prompt.py", line 21, in <module>
[OHOS ERROR] from prompt_toolkit.shortcuts import run_application
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/__init__.py", line 16, in <module>
[OHOS ERROR] from .interface import CommandLineInterface
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/interface.py", line 19, in <module>
[OHOS ERROR] from .application import Application, AbortAction
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/application.py", line 8, in <module>
[OHOS ERROR] from .key_binding.bindings.basic import load_basic_bindings
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/key_binding/bindings/basic.py", line 9, in <module>
[OHOS ERROR] from prompt_toolkit.renderer import HeightIsUnknownError
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/renderer.py", line 11, in <module>
[OHOS ERROR] from prompt_toolkit.styles import Style
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/styles/__init__.py", line 8, in <module>
[OHOS ERROR] from .from_dict import *
[OHOS ERROR] File "/home/fangye/.local/lib/python3.10/site-packages/prompt_toolkit/styles/from_dict.py", line 9, in <module>
[OHOS ERROR] from collections import Mapping
[OHOS ERROR] ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
[OHOS ERROR] Unhandled error: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

Cause Analysis

There is no such problem on Ubuntu20.04, its python version is 3.8.x, and the python version of Ubuntu22.04 is 3.10.x, since the hb command is written in python, it is guessed that this is a problem caused by the python version.
According to the error message:
Unhandled error: cannot import name 'Mapping' from 'collections'
It can be known that the reason for the error is that the import of the Mapping package failed. After searching for this error, many people have encountered similar problems. The error message is very similar to this situation:
https://blog.csdn.net/hongel110/article/details/121333144

Solution

Modify the python script that reports the error, and change from collections import Mapping to from collections.abc import Mapping:

vim ~/.local/lib/python3.10/site-packages/prompt_toolkit/styles/from_dict.py