# Problems with encoding using non-Slicer Python environment

**URL:** https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820
**Category:** Support
**Created:** [October 17, 2019, 6:52pm UTC](https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820 "2019-10-17T18:52:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dloopz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dloopz/32/7646_2.png) [@dloopz](https://discourse.slicer.org/u/dloopz)
#### Post date: [October 17, 2019, 6:52pm UTC](https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820/1 "2019-10-17T18:52:53Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/Developers/FAQ/Python_Scripting#How_to_run_Python_script_using_a_non-Slicer_Python_environment) ( “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

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [October 17, 2019, 7:03pm UTC](https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820/2 "2019-10-17T19:03:15Z")

</div>

It seems that Python subprocess expects ascii input and/or output. This seems to cause issues quite often (see for example [here](https://stackoverflow.com/questions/2595448/unicode-filename-to-python-subprocess-call)).

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.

---

<div class="post-metadata">

### Author: ![dloopz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dloopz/32/7646_2.png) [@dloopz](https://discourse.slicer.org/u/dloopz)
#### Post date: [October 18, 2019, 2:12pm UTC](https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820/3 "2019-10-18T14:12:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dloopz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dloopz/32/7646_2.png) [@dloopz](https://discourse.slicer.org/u/dloopz)
#### Post date: [October 25, 2019, 12:47pm UTC](https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820/4 "2019-10-25T12:47:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [October 27, 2019, 5:18pm UTC](https://discourse.slicer.org/t/problems-with-encoding-using-non-slicer-python-environment/8820/5 "2019-10-27T17:18:51Z")

</div>

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