Repository: spyder-ide/spyder

Issue:
UnicodeDecodeError when changing PYTHONPATH in Python 2
 
<!---   *** BEFORE SUBMITTING: PASTE CLIPBOARD HERE TO COMPLETE YOUR REPORT ***   ---!>
<!--- **PLEASE READ:** Before submitting here, please carefully consult our *Troubleshooting Guide*: https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ and search the issues page for your error/problem, as most posted bugs are duplicates or easy fixes.

If you don't find anything, please provide a detailed step-by-step description (in English) of the problem and what led up to it below. Issue reports without a clear way to reproduce them will be closed. Thanks! --->

## Problem Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

after reseting my spyder with "spyder --reset" command and add new path with PYTHONPATH manager. my path is "C:\User\AADL\code\actools\database"

i dont know why this is happening

with adding any path, trigger this problem

### Traceback
```python-traceback
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\plugins\__init__.py", line 514, in <lambda>
    toggled=lambda checked: self.toggle_view(checked),
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\plugins\ipythonconsole.py", line 725, in toggle_view
    self.create_new_client(give_focus=False)
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1046, in create_new_client
    self.connect_client_to_kernel(client, is_cython=is_cython)
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1074, in connect_client_to_kernel
    is_cython=is_cython)
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1498, in create_kernel_manager_and_kernel_client
    if not kernel_spec.env.get('PYTHONPATH'):
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\utils\ipython\kernelspec.py", line 91, in env
    drop_env=(not default_interpreter))
  File "C:\Users\AADL\Anaconda3\envs\python2_14\lib\site-packages\spyder\utils\misc.py", line 256, in add_pathlist_to_PYTHONPATH
    pypath =  {'PYTHONPATH': pathstr + os.pathsep + old_pypath,
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 59: ordinal not in range(128)
```

## Package Versions

* Spyder version: 3.2.8 
* Python version: 2.7.14
* Qt version: 5.6.2
* PyQt5 version: 5.6
* Operating system: Windows 10

### Dependencies

```
pyflakes >=0.5.0  :  1.6.0 (OK)
pycodestyle >=2.3 :  2.3.1 (OK)
pygments >=2.0    :  2.2.0 (OK)
pandas >=0.13.1   :  None (NOK)
numpy >=1.7       :  1.14.0 (OK)
sphinx >=0.6.6    :  1.7.2 (OK)
rope >=0.9.4      :  0.10.7 (OK)
jedi >=0.9.0      :  0.11.1 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
sympy >=0.7.3     :  None (NOK)
cython >=0.21     :  None (NOK)
qtconsole >=4.2.0 :  4.3.1 (OK)
IPython >=4.0;<6.0:  5.4.1 (OK)
pylint >=0.25     :  1.8.2 (OK)
```



Discussion:
What's your current PYTHONPATH? You can get it by evaluating in a console:

```python
import os
os.environ['PYTHONPATH']
```
![image](https://user-images.githubusercontent.com/34735152/40822230-4240e5b0-65a6-11e8-99d9-b1138c997e7f.png)

after ""os.environ['PYTHONPATH'] "" i got this output but in the image there is no such path
'C:\\Users\\AADL\\Desktop\\code\\actools;D:\\mukyeom\\LAB\\# 03 PAV \xea\xb2\xbd\xec\xa7\x84?\x80??code\\actools;D:\\mukyeom\\LAB\\datafusion program\\datafusion'
Although the entry you're trying to add doesn't have Unicode chars, the second entry certainly has them, and that's the problem. Maybe you're unable to have non-ascii paths in your PYHTONPATH in Python 2
now i know this is Unicode problem....
but after that error any path cannot be add to PYTHONPATH even desktop folder itself.

Perhaps you need to reset your PYTHONPATH using regedit.
can you teach me how to do that? 
@dalthviz, please take a look at this one.
@muuky maybe [this](https://superuser.com/questions/949560/how-do-i-set-system-environment-variables-in-windows-10) could help to manage your enviromental variables (including `PYTHONPATH`). 
@dalthviz, we need to avoid users to make PYTHONPATH entries with unicode characters in our dialog. I think that would be the solution to this problem.

Resolving patch:
diff --git a/spyder/widgets/pathmanager.py b/spyder/widgets/pathmanager.py
--- a/spyder/widgets/pathmanager.py
+++ b/spyder/widgets/pathmanager.py
@@ -24,6 +24,7 @@
 from spyder.utils.misc import getcwd_or_home
 from spyder.utils import icon_manager as ima
 from spyder.utils.qthelpers import create_toolbutton
+from spyder.py3compat import PY2
 
 
 class PathManager(QDialog):
@@ -259,16 +260,30 @@ def add_path(self):
                                          self.last_path)
         self.redirect_stdio.emit(True)
         if directory:
+            is_unicode = False
+            if PY2:
+                try:
+                    directory.decode('ascii')
+                except UnicodeEncodeError:
+                    is_unicode = True
+                if is_unicode:
+                    QMessageBox.warning(self, _("Add path"),
+                                        _("You are using Python 2 and the path"
+                                          " selected has Unicode characters. "
+                                          "The new path will not be added."),
+                                        QMessageBox.Ok)
+                    return
             directory = osp.abspath(directory)
             self.last_path = directory
             if directory in self.pathlist:
                 item = self.listwidget.findItems(directory, Qt.MatchExactly)[0]
                 item.setCheckState(Qt.Checked)
-                answer = QMessageBox.question(self, _("Add path"),
-                    _("This directory is already included in Spyder path "
-                            "list.<br>Do you want to move it to the top of "
-                            "the list?"),
-                    QMessageBox.Yes | QMessageBox.No)
+                answer = QMessageBox.question(
+                            self, _("Add path"),
+                            _("This directory is already included in Spyder "
+                              "path list.<br>Do you want to move it to the "
+                              "top of the list?"),
+                            QMessageBox.Yes | QMessageBox.No)
                 if answer == QMessageBox.Yes:
                     self.pathlist.remove(directory)
                 else:


Test patch: