print output redirection not working as in Python.
Port, board and/or hardware
raspberry pico2w
MicroPython version
MicroPython v1.26.0 on 2025-08-09; Raspberry Pi Pico 2 W with RP2350
Reproduction
# print redirection test
import sys
proper_stdout = sys.stdout # save original
log = open('out.txt', 'w')
sys.stdout = log # redirect stdout
print('hello')
print('wheres my print')
log.close()
sys.stdout = proper_stdout # revert redirect
print('back to the future')
print('so whats in the out.txt file?')
read_log = open('out.txt', 'r')
for line in read_log:
print(line)
read_log.close()
Expected behaviour
The sys.stdout would be redirected to the 'log' file (out.txt) and the following print two print statement contents would be directed to the out.txt file.
Observed behaviour
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
AttributeError: 'module' object has no attribute 'stdout'
line 5 is:
sys.stdout = log
Additional Information
This code snippet works as expected in Python.
Code of Conduct
Yes, I agree
Syntax Error when using multiline f-strings on RP2040 with MicroPython v1.23.0
Port, board and/or hardware
RP2040 Raspberry Pi Pico
MicroPython version
MicroPython Version: v1.23.0 (2024-06-02)
RP2040 Platform: Raspberry Pi Pico
Python Version: MicroPython (RP2040)
Firmware: v1.23.0
Hardware: Raspberry Pi Pico (RP2040)
Reproduction
msg = (
f'One '
f'Two '
f'Three'
)
print(msg)
Expected behaviour
The above code should print the concatenated string One Two Three without any syntax errors, similar to how it works in standard Python environments.
Observed behaviour
SyntaxError: invalid syntax
Additional Information
This issue persists even when re-typing the code, leading me to suspect it might be an issue with the way multiline f-strings are handled on this particular platform.
The issue can be resolved by concatenating the f-strings manually using +:
msg = (
f'One ' +
f'Two ' +
f'Three'
)
print(msg)
Workaround:
Using string concatenation (as shown above) works, but this is not as elegant and adds unnecessary complexity compared to expected behaviour.
What else I have tried:
Testing with other Python environments (such as standard Python 3.x) shows that the multiline f-string works as expected.
This issue does not occur with simpler, single-line f-strings.
Ensuring no invisible characters or incorrect syntax in the code.
Code of Conduct
Yes, I agree