Python interpreter arguments containing special characters


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 €

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

Thanks for reporting. The issue is known, it is caused by a bug in the application launcher. I’ve submitted a fix some time ago:

It would be great if you could work with @jcfr to get the fix integrated.

Until then you can use a workaround: start bash using the launcher and then start Slicer from that terminal.

./Slicer --launch bash
bin/SlicerApp-real --python-code "print('é')"
1 Like

Thank you Andras for this quick answer, I will discuss it with @jcfr

Another quick question: why is there no completion in this interpreter ?