Problems with encoding using non-Slicer Python environment

Hi!

I’m developing an extension for Slicer with a segmentation module. In my “Module.py” file I want to make a call to a python3.py file of my own (outside Slicer-Python environment) using conda environment. I tried different ways to make the call, until I find the last question of the article ( “How to run Python script using a non-Slicer Python environment”) and noticed that I had the same problem described there. But now I have a problem with encodings: hhis is the piece of my code:

command_line = ["/home/dloopz/miniconda3/bin/python", "--version"]
from subprocess import check_output
command_result = check_output(command_line, env=slicer.util.startupEnvironment())
print(command_result)

and this is the output:

command_result = check_output(command_line, env=slicer.util.startupEnvironment())
  File "/home/dloopz/SoftsLaburo/Slicer-4.10.2-linux-amd64/lib/Python/lib/python2.7/subprocess.py", line 212, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/home/dloopz/SoftsLaburo/Slicer-4.10.2-linux-amd64/lib/Python/lib/python2.7/subprocess.py", line 390, in __init__
    errread, errwrite)
  File "/home/dloopz/SoftsLaburo/Slicer-4.10.2-linux-amd64/lib/Python/lib/python2.7/subprocess.py", line 1024, in _execute_child
    raise child_exception
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 9: ordinal not in range(128)

The problems I had before were similar, but with the “init.py” file of Slicer.
Any idea of how to solve this?

Best regards,
Lucca Dellazoppa

It seems that Python subprocess expects ascii input and/or output. This seems to cause issues quite often (see for example here).

One solution is to only use ascii file and folder names and command-line arguments; or you may implement some of the workarounds suggested in various Python forums.

Slicer-4.10 used Python 2, but Preview Release uses Python 3. If this problem was fixed in Python 3 then you may consider switching to using a Slicer Preview Release.

Thanks Andras, I’ll look around to the works you mentioned and try to fix it. If I find a solution I post it here.

Lucca

1 Like

Hi again!

I can’t find how to solve the problem yet, and need to use the last version of Slicer. I noticed that reinstalling Slicer allows me to run external python3 files, in my miniconda env, but after a day or two, it crashes again. Always the same error, no matter what command_line I use:

command_result = check_output(command_line, env=slicer.util.startupEnvironment())
  File "/home/dloopz/SoftsLaburo/Slicer-4.10.2-linux-amd64/lib/Python/lib/python2.7/subprocess.py", line 212, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/home/dloopz/SoftsLaburo/Slicer-4.10.2-linux-amd64/lib/Python/lib/python2.7/subprocess.py", line 390, in __init__
    errread, errwrite)
  File "/home/dloopz/SoftsLaburo/Slicer-4.10.2-linux-amd64/lib/Python/lib/python2.7/subprocess.py", line 1024, in _execute_child
    raise child_exception
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 9: ordinal not in range(128)

I don’t even know where is that character u’\xf3’ in position 9, if my command_line was, for example:

command_line = ["/home/dloopz/miniconda3/bin/python", "--version"]

Any advice?

Thanks again!
Lucca

Try recent Slicer preview releases. They use Python3, which probably does not have this Python2 error anymore.