Cannot flash using NCS 2.9 toolchain in VS Code

OS: Ubuntu 20.04.6 LTS

IDE: Visual Studio Code v1.97.2 and v1.93.0

nRF Connect for VS Code: v2025.1.127

SDK: NCS v2.9

Sample Application: Peripheral_LBS (not ble_peripheral_lbs_coex)

Hardware: nRF52833 and nRF54L15

OS Global python version: 3.8.10

ToolChain Manager: v1.5.3

The specific issue I am encountering seems to be a problem within the NCS v2.9 toolchain that only materializes when attempting to flash my application to a device. After some investigation, I believe the issue is the flashing script using my global python3.8 instead of the python3.12 back-end that should be included within the toolchain. The error output is posted below.

west flash: using runner nrfutil

-- runners.nrfutil: reset after flashing requested

-- runners.nrfutil: Flashing file: /home/kyle/Desktop/peripheral_lbs_1/build_3/merged.hex

Fatal Python error: init_sys_streams: can't initialize sys standard streams

Python runtime state: core initialized

Traceback (most recent call last):

File "/home/***/ncs/toolchains/b77d8c1312/usr/local/lib/python3.12/io.py", line 55, in <module>

ImportError: cannot import name 'text_encoding' from 'io' (unknown location)

-- runners.nrfutil: Board with serial number 1057761682 flashed successfully.

* Terminal will be reused by tasks, press any key to close it.

Background: This project was started a few weeks ago when the NCS v2.9 sdk was originally installed. This initial download of the sdk was able to build and flash without issue, however during development some sdk files were unintentionally modified. In order to revert those changes, the toolchain manager was used to remove and then re-install the sdk on Feb 19th. After the re-install any attempt to flash the application to a device resulted in the python import error.

Setup: Download the NCS 2.9 sdk and toolchain using the toolchain manager as well as the VS Code IDE on a device running Ubuntu 20.04LTS. Install the nRF Connect extension for VS Code and navigate to the nRF Connect tab. Select “Create a new application” and copy the Peripheral_LBS example as the base. Setup the build configuration to use the NCS 2.9 sdk and v2.9 toolchain with an nRF52833dk or nRF54l15dk as the board target. Build the application, which should run without issue, then attempt to flash the application to the device. On flash, the error should be seen.

Resolution Attempts: I have tried re-installing NCS 2.9, re-installing the NCS toolchain, re-installing VS Code, updating VS Code, changing the VS Code python interperter, as well as attempting to flash the application to a nRF54l15dk and a nRF52833dk with the same error encountered on each attempt. I have also attempted to setup NCS 2.9 and VS Code on a second computer also running Ubuntu 20.04 as a fresh install, but the same error was encountered on first flash attempt.

One debugging step that successfully flashed the application to the device was flashing the sample application to the nRF52833dk using the NCS 2.9 sdk and the NCS 2.7 toolchain.

  • Update: 
    What I got yesterday was that we have managed to reproduce the issue, but they did not have a workaround or root cause. They suspect there is some script call that uses default os python with something like 

    #!/usr/bin/pythonshebang) while PYTHONPATH is set to toolchain's libs which causes this error

    Regards

    Runar

  • I got a question from R&D if you could provide some more information like video/step on how you open the terminal. As this issue is both for command line and VScode they would like to see how you open the shell env for  CLI case first to run the west command. 

    Afterwards they wonder what happends if you remove west from python 3.8?

    Regards

    Runar

  • In order to open the terminal and run west commands, I select the Launch Profile option next to the new terminal action and open an nRF Connect terminal page. At that point the terminal reports the environmental python version is 3.12 with west 1.2.0 installed. Using this terminal instance I can run the builld and flashing west commands. The build command works without issue, exactly as the VS Code action, however the flash command fails with the same ImportError as stated above.

Related