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_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?
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.
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: