Python utterly broken

Hi!

I am trying to build a project, however, the sdk-packed python installation seems utterly broken.

With 2.5 toolchain:

Traceback (most recent call last):
 File "/opt/firmware/device/c2usb/modules/hid-rp/modules/hid-usage-tables/src/hid/codegen/cpp.py", line 14, in <module>
   from hid.parser import parse_database
 File "/home/me/.local/lib/python3.11/site-packages/hid/__init__.py", line 2, in <module>
   import ctypes
 File "/home/me/ncs/toolchains/7795df4459/usr/local/lib/python3.8/ctypes/__init__.py", line 7, in <module>
   from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'



With 2.4, 2.3 and 2.2 toolchains:

-- Found Python: /home/me/ncs/toolchains/1f9b40e71a/usr/local/bin/python3.8 (found suitable version "3.8.2", minimum required is "3") found components: Interpreter
Python path configuration:
  PYTHONHOME = '/home/me/ncs/toolchains/1f9b40e71a/usr/local'
  PYTHONPATH = '/home/me/ncs/toolchains/1f9b40e71a/usr/local/lib/python3.8/site-packages'
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = '/home/me/ncs/toolchains/1f9b40e71a/usr/local/lib/python3.11'
  sys._base_executable = '/usr/bin/python'
  sys.base_prefix = '/home/me/ncs/toolchains/1f9b40e71a/usr/local'
  sys.base_exec_prefix = '/home/me/ncs/toolchains/1f9b40e71a/usr/local'
  sys.platlibdir = 'lib'
  sys.executable = '/usr/bin/python'
  sys.prefix = '/home/me/ncs/toolchains/1f9b40e71a/usr/local'
  sys.exec_prefix = '/home/me/ncs/toolchains/1f9b40e71a/usr/local'
  sys.path = [
    '/home/me/ncs/toolchains/1f9b40e71a/usr/local/lib/python3.8/site-packages',
    '/home/me/ncs/toolchains/1f9b40e71a/usr/local/lib/python311.zip',
    '/home/me/ncs/toolchains/1f9b40e71a/usr/local/lib/python3.11',
    '/home/me/ncs/toolchains/1f9b40e71a/usr/local/lib/python3.11/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'


With 2.1 toolchain:

CMake Error at /opt/firmware/nrfconnect/zephyr/cmake/modules/FindZephyr-sdk.cmake:56 (find_package):
  Could not find a configuration file for package "Zephyr-sdk" that is
  compatible with requested version "0.15".

  The following configuration files were considered but not accepted:

    /home/me/ncs/toolchains/v2.1.0/opt/zephyr-sdk/cmake/Zephyr-sdkConfig.cmake, version: 0.14.1

Call Stack (most recent call first):
  /opt/firmware/nrfconnect/zephyr/cmake/modules/FindHostTools.cmake:53 (find_package)
  /opt/firmware/nrfconnect/zephyr/cmake/modules/dts.cmake:9 (find_package)
  /opt/firmware/nrfconnect/zephyr/cmake/modules/zephyr_default.cmake:115 (include)
  /opt/firmware/nrfconnect/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
  /opt/firmware/nrfconnect/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
  CMakeLists.txt:2 (find_package)

Thanks in advance,

Karel

  • Hi Karel,

    I see from the error message that you are running a python script /opt/firmware/device/c2usb/modules/hid-rp/modules/hid-usage-tables/src/hid/codegen/cpp.py. That is your script so I am not sure what that does? Also, the paths to python is a combination of a local installation "/home/me/.local/lib/python3.11" and the toolchain "/home/me/ncs/toolchains/7795df4459/usr/local/lib/python3.8/" so something is seemingly changing the environment variables compared to what you shoudld get witht he tooclhain manager.

    I do not know what the script is doing, but could that be it? Or could there be an issue with how you start the toolchain manager shell for instance? If you are not able to resolve this, perhaps you can elaborate in detail what you and the script does so that we get a better understanding and may be able to spot a specific issue.

    Einar

  • Hi Einar, and thanks for reply!

    > That is your script so I am not sure what that does?

    Honestly I have no idea either - from my point of view it is a third party dependency at github.com/.../hid-usage-tables, and its documentation is not very descriptive. But when started from the commandline using system python installation, it does not crash. (Or to be precise, does not crash with this error message.)

    The `ModuleNotFoundError: No module named '_ctypes'` error seems to be a fairly common problem, usually caused by python not being able to find libffi. (I have installed the library in the system (ubuntu 23.10), and linked it into the python root at `/home/me/ncs/toolchains/7795df4459/usr/lib/x86_64-linux-gnu/libffi.so.8.1.2`. But no luck)

    > Or could there be an issue with how you start the toolchain manager shell for instance?

    Well, this is default build obtained from vscode by following developer.nordicsemi.com/.../install_ncs.html and creating an application build from a cmake preset.

    As to building from shell, as of now, I have no idea how to do that. Didn't receive any instructions and am not familiar with zephyr/west building environtments. (Just copying the "build command" from vscode and pasting it into a shell (one that has `~/ncs/zephyr/zephyr-env.cmd` sourced and `cd`ed into project root) doesn't work.)

  • kat829 said:
    (Just copying the "build command" from vscode and pasting it into a shell (one that has `~/ncs/zephyr/zephyr-env.cmd` sourced and `cd`ed into project root) doesn't work.)

    You need to either build from nRF Connect for VS Code, or a nRF Connect shell in VS code, or start nRF Connect shell in another way (like from the toolchain manager or nrfutil). If you just use your normal shell without doing that, you will not have the environment variables set correctly, and that could possibly explain what you are seeing. For instance, to start a shell using nrfutil you can use this command to start a 2.5.0 toolchain (change version as needed):

    nrfutil toolchain-manager launch --shell --ncs-version v2.5.0

    If you still don't get it working, can you show in full detail how you build (either if it is from VS code or the command line, including how you start the shell)?

  • Thanks for pointing me in the right direction with the nrfutil shell!

    I have managed to reduce the problem to the following:

    nrfutil toolchain-manager launch --shell --ncs-version v2.5.0
    git clone https://github.com/IntergatedCircuits/hid-rp.git
    cd hid-rp
    git submodule init
    git submodule update
    cmake -S . -B build

    Now, when executing from the nrfutil shell, I get:

    Re-run cmake no build system arguments
    Traceback (most recent call last):
      File "/opt/hid-rp/modules/hid-usage-tables/src/hid/codegen/cpp.py", line 14, in <module>
        from hid.parser import parse_database
      File "/home/me/.local/lib/python3.11/site-packages/hid/__init__.py", line 2, in <module>
        import ctypes
      File "/home/me/ncs/toolchains/7795df4459/usr/local/lib/python3.8/ctypes/__init__.py", line 7, in <module>
        from _ctypes import Union, Structure, Array
    ModuleNotFoundError: No module named '_ctypes'
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /opt/hid-rp/build
    

    While executing from system shell, I get:

    The CXX compiler identification is GNU 13.2.0
    Detecting CXX compiler ABI info
    Detecting CXX compiler ABI info - done
    Check for working CXX compiler: /usr/bin/c++ - skipped
    Detecting CXX compile features
    Detecting CXX compile features - done
    Found Python: /usr/bin/python3 (found suitable version "3.11.6", minimum required is "3") found components: Interpreter
    Traceback (most recent call last):
    File "/opt/hid-rp/modules/hid-usage-tables/src/hid/codegen/cpp.py", line 14, in <module>
    from hid.parser import parse_database
    ModuleNotFoundError: No module named 'hid.parser'
    Configuring done (1.0s)

    (I assume that I have broken dependencies by reducing the problem to a minimum, so for the moment I assume the `hid.parser` error to be a success.)

    P.S. Why does a developer forum not support markdown? Grimacing

  • The thing that causes troubles (i.e., garbled python environment) is the following two lines in the CMakeLists.txt:

    find_package(Python 3 REQUIRED)
    ...
    execute_process(
        COMMAND python ${USAGE_TABLES_SRC_DIR}/src/hid/codegen/cpp.py ${USAGE_TABLES_OUTPUT_DIR}
        WORKING_DIRECTORY ${USAGE_TABLES_SRC_DIR}
    )

    Unsetting PYTHONHOME and PYTHONPATH inside the nrfutil shell helps.

    I still have no idea of what is the moral of the story though.

Related