Repository: spyder-ide/spyder

Issue:
Spyder fails to launch because of Unicode errors in create_kernel_spec
## Spyder Launch Error

When I run spyder on command line, I got error message like below.

> Traceback (most recent call last):   File
> "/home/park/anaconda2/lib/python2.7/site-packages/spyder/app/mainwindow.py",
> line 2998, in main
>     mainwindow = run_spyder(app, options, args)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/app/mainwindow.py",
> line 2902, in run_spyder
>     main.setup()   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/app/mainwindow.py",
> line 1153, in setup
>     self.setup_layout(default=False)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/app/mainwindow.py",
> line 1414, in setup_layout
>     self.setup_default_layouts('default', settings)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/app/mainwindow.py",
> line 1593, in setup_default_layouts
>     widget.toggle_view(True)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py",
> line 677, in toggle_view
>     self.create_new_client(give_focus=False)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py",
> line 886, in create_new_client
>     self.connect_client_to_kernel(client)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py",
> line 903, in connect_client_to_kernel
>     km, kc = self.create_kernel_manager_and_kernel_client(connection_file)   File
> "/home/park/anaconda2/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py",
> line 1276, in create_kernel_manager_and_kernel_client
>     kernel_manager._kernel_spec = self.create_kernel_spec()   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py",
> line 1256, in create_kernel_spec
>     uv = to_text_string(v)   File "/home/park/anaconda2/lib/python2.7/site-packages/spyder/py3compat.py",
> line 136, in to_text_string
>     return unicode(obj) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 73: ordinal not in range(128)

**I have tried**
1. Reinstall using "pip install spyder"
2. Reinstall using "conda install spyder"
3. On the command line "spyder --reset" 

**Version Information**(
- Spyder Version: 3.0
- Python Version: 2.7.12
- Operating system: Ubuntu 14.04
- Anaconda Version : 4.2.0

How do I fix it. If you give me some ideas, I can really appreciate for this.

Thanks.



Discussion:
@eunsoo, as was asked on #3127, could you please include the output of running

```
printenv
```

in a terminal (i.e. in bash or zsh :-) For some reason, a non-ascii character (perhaps Hangul) has crept in. 

If you can switch to Python 3, that might turn out to help, as Python 3 is generally better with non-ascii characters.

@mariacamilaremolinagutierrez, please work on this one. To reproduce it, please create a conda environment with all Spyder dependencies in a directory with non-ascii chars (e.g. `/tmp/añño`), and run Spyder from it.

I think that would reproduce this problem, but if not, please let me know about it :-)

@ccordoba12 I was not able to reproduce the error, I was trying several configurations. But only until now I realized all this time I was using python 3.5, so I am going to install it again with python 2.7

Ok, thanks :-)

I was able to reproduce it by naming the conda environment with a non-ascii character:

![image](https://cloud.githubusercontent.com/assets/3608776/19870178/78c99b40-9f7d-11e6-905b-0cd8d98b65c9.png)

I will find out later today how to fix that

This means that Python itself doesn't even work when installed in a non-ascii dir on Unix systems, but that's not directly related to the original problem :-/

We'll discuss about this in our meeting, please work on your other issue instead :-)


Resolving patch:
diff --git a/spyder/plugins/ipythonconsole.py b/spyder/plugins/ipythonconsole.py
--- a/spyder/plugins/ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole.py
@@ -1238,13 +1238,24 @@ def create_kernel_spec(self):
         ]
 
         # Environment variables that we need to pass to our sitecustomize
+        umr_namelist = CONF.get('main_interpreter', 'umr/namelist')
+
+        if PY2:
+            original_list = umr_namelist.copy()
+            for umr_n in umr_namelist:
+                try:
+                    umr_n.encode('utf-8')
+                except UnicodeDecodeError:
+                    umr_namelist.remove(umr_n)
+            if original_list != umr_namelist:
+                CONF.set('main_interpreter', 'umr/namelist', umr_namelist)
+
         env_vars = {
             'IPYTHON_KERNEL': 'True',
             'EXTERNAL_INTERPRETER': not default_interpreter,
             'UMR_ENABLED': CONF.get('main_interpreter', 'umr/enabled'),
             'UMR_VERBOSE': CONF.get('main_interpreter', 'umr/verbose'),
-            'UMR_NAMELIST': ','.join(CONF.get('main_interpreter',
-                                              'umr/namelist'))
+            'UMR_NAMELIST': ','.join(umr_namelist)
         }
 
         # Add our PYTHONPATH to env_vars
diff --git a/spyder/plugins/maininterpreter.py b/spyder/plugins/maininterpreter.py
--- a/spyder/plugins/maininterpreter.py
+++ b/spyder/plugins/maininterpreter.py
@@ -20,7 +20,7 @@
 # Local imports
 from spyder.config.base import _
 from spyder.plugins.configdialog import GeneralConfigPage
-from spyder.py3compat import is_text_string, to_text_string
+from spyder.py3compat import PY2, is_text_string, to_text_string
 from spyder.utils import icon_manager as ima
 from spyder.utils.misc import get_python_executable
 from spyder.utils import programs
@@ -184,9 +184,23 @@ def set_umr_namelist(self):
             arguments = to_text_string(arguments)
             if arguments:
                 namelist = arguments.replace(' ', '').split(',')
-                fixed_namelist = [module_name for module_name in namelist
-                                  if programs.is_module_installed(module_name)]
-                invalid = ", ".join(set(namelist)-set(fixed_namelist))
+                fixed_namelist = []
+                non_ascii_namelist = []
+                for module_name in namelist:
+                    if PY2:
+                        if all(ord(c) < 128 for c in module_name):
+                            if programs.is_module_installed(module_name):
+                                fixed_namelist.append(module_name)
+                        else:
+                            QMessageBox.warning(self, _('Warning'),
+                            _("You are working with Python 2, this means that "
+                              "you can not import a module that contains non-"
+                              "ascii characters."), QMessageBox.Ok)
+                            non_ascii_namelist.append(module_name)
+                    elif programs.is_module_installed(module_name):
+                        fixed_namelist.append(module_name)
+                invalid = ", ".join(set(namelist)-set(fixed_namelist)-
+                                    set(non_ascii_namelist))
                 if invalid:
                     QMessageBox.warning(self, _('UMR'),
                                         _("The following modules are not "


Test patch: