Hi,
It is probably not a classic use of the python interpreter embedded in 3D Slicer but I would like to inform you about this strange behavior:
I use this interpreter to run a script which requires a file path as parameter.
If the path contains some special characters (like é or €) which requires utf-8 encoding then sys.argv is wrong: these characters are replaced by other ones.
Here is a small script which shows clearly the issue:
import sys
import os
param = sys.argv[1]
print('param = ' + param)
argvb = list(map(os.fsencode, sys.argv))
param_bytes = argvb[1]
print('param_bytes = ' + str(param_bytes))
As you guess you have to provide 1 parameter to run it:
/path/to/slicer/install/bin/PythonSlicer €
Result:
param = é
param_bytes = b’\xc3\x83\xc2\xa9’
Expected Result (running ubuntu embedded python3 interpreter)
param = é
param_bytes = b’\xc3\xa9’
We see \x83\xc2 is added to the bytes, and it is caused by a wrong double utf-8 encoding issue (the accepted answer works well):
Do you have any idea why the python interpreter of 3d slicer decodes parameters twice ?
Best regards