Repository: spyder-ide/spyder

Issue:
Sitecustomize fails when  current directory has non-ascii chars
## Description

**What steps will reproduce the problem?**
1.  Start Spyder.
2.  Open any *.py file.
3.  Close Spyder.
4.  Open again.

**What is the expected output? What do you see instead?**

The internal python console shows the message "'import sitecustomize' failed"
and stops loading any further. No commands can be executed. After running
spyder --reset
it loads normally until the next startup after I had a file opened. 

**Please provide any additional information below**
## Version and main components
- Spyder Version: 2.3.8 
- Python Version: 2.7.12
- Qt Versions:  4.8.7, PyQt4 (API v2) 4.11.4 on Linux
## Optional dependencies

```
pyflakes >=0.5.0   :  1.1.0 (OK)
pep8 >=0.6         :  None (NOK)
pygments >=1.6     :  2.1 (OK)
IPython >=1.0      :  2.4.1 (OK)
zmq >=2.1.11       :  15.2.0 (OK)
pandas >=0.13.1    :  0.17.1 (OK)
sphinx >=0.6.6     :  1.3.6 (OK)
psutil >=0.3       :  3.4.2 (OK)
rope >=0.9.2       :  0.10.2 (OK)
jedi >=0.8.1;<0.9.0:  0.9.0 (NOK)
matplotlib >=1.0   :  1.5.1 (OK)
sympy >=0.7.3      :  None (NOK)
pylint >=0.25      :  1.5.2 (OK)

```



Discussion:
I found, this only happens, when the working directory at the time of closing spyder has special characters (e.g. 'ß') somewhere in the path.

Thanks for reporting. We'll fix this error after we release Spyder 3.0 :-)

Hi @Dschoni 

I test your problem on my Linux Deepin machine (15.03 64 bits)
1. Open spyder and open *.py file
   ![test 3407 before seleccion_009](https://cloud.githubusercontent.com/assets/5307894/19622079/13b5a98c-9867-11e6-8ca5-a6c3a08ca0b2.png)
2. reopen Spyder
   ![test 3407 after seleccion_009](https://cloud.githubusercontent.com/assets/5307894/19622080/210b3796-9867-11e6-8fed-7c525b7a7b8f.png)

## Spyder & Python Enviroment

> Spyder 3.0.1
> Python 2.7.12 :: Continuum Analytics, Inc.(Anaconda 4.2 64 bits)
> Linux wallas-pc 4.4.0-2-deepin-amd64 #1 SMP Deepin 4.4.6-4 (2016-07-01) x86_64 GNU/Linux

## Dependences

> jedi >=0.8.1     :  0.9.0 (OK)
> matplotlib >=1.0 :  1.5.3 (OK)
> nbconvert >=4.0  :  4.2.0 (OK)
> numpy >=1.7      :  1.11.2 (OK)
> pandas >=0.13.1  :  0.19.0 (OK)
> pep8 >=0.6       :  1.7.0 (OK)
> psutil >=0.3     :  4.3.1 (OK)
> pyflakes >=0.5.0 :  1.3.0 (OK)
> pygments >=2.0   :  2.1.3 (OK)
> pylint >=0.25    :  1.5.4 (OK)
> qtconsole >=4.2.0:  4.2.1 (OK)
> rope >=0.9.4     :  0.9.4 (OK)
> sphinx >=0.6.6   :  1.4.8 (OK)
> sympy >=0.7.3    :  1.0 (OK)

As you can see, you could work with spyder 3.0.1 and our future release (v3.1). Because It solves your issue. @ccordoba12 @goanpeca 

Thanks for the report and contribute with us, to make better Spyder;).

Well no.
The problem is still existing.

Create a folder with non-asci chars (e.g. 'äöüß').
Start spyder.
Set active working directory to that folder.
Close spyder.
Start spyder.
--> Import sitecustomize failed.
(Tested with spyder 3.0.1)

Am 2016-10-22 21:52, schrieb malliwi88:

> Hi @Dschoni [2]
> 
> I test your problem on my Linux Deepin machine (15.03 64 bits)
> 
>   *
> 
> Open spyder and open *.py file
>  [3]
>   *
> 
> reopen Spyder
>  [4]
> 
> SPYDER & PYTHON ENVIROMENT
> 
> > Spyder 3.0.1
> > Python 2.7.12 :: Continuum Analytics, Inc.(Anaconda 4.2 64 bits)
> > Linux wallas-pc 4.4.0-2-deepin-amd64 #1 [1] SMP Deepin 4.4.6-4
> > (2016-07-01) x86_64 GNU/Linux
> 
> DEPENDENCES
> 
> > jedi >=0.8.1 : 0.9.0 (OK)
> > matplotlib >=1.0 : 1.5.3 (OK)
> > nbconvert >=4.0 : 4.2.0 (OK)
> > numpy >=1.7 : 1.11.2 (OK)
> > pandas >=0.13.1 : 0.19.0 (OK)
> > pep8 >=0.6 : 1.7.0 (OK)
> > psutil >=0.3 : 4.3.1 (OK)
> > pyflakes >=0.5.0 : 1.3.0 (OK)
> > pygments >=2.0 : 2.1.3 (OK)
> > pylint >=0.25 : 1.5.4 (OK)
> > qtconsole >=4.2.0: 4.2.1 (OK)
> > rope >=0.9.4 : 0.9.4 (OK)
> > sphinx >=0.6.6 : 1.4.8 (OK)
> > sympy >=0.7.3 : 1.0 (OK)
> 
> As you can see, you could work with spyder 3.0.1 and our future
> release (v3.1). Because It solves your issue. @ccordoba12 [5]
> @goanpeca [6]
> 
> Thanks for the report and contribute with us, to make better Spyder;).
> 
> ## 
> 
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub [7], or mute the
> thread [8].
> 
>   *
> 
> ## Links:
> 
> [1] https://github.com/spyder-ide/spyder/issues/1
> [2] https://github.com/Dschoni
> [3]
> https://cloud.githubusercontent.com/assets/5307894/19622079/13b5a98c-9867-11e6-8ca5-a6c3a08ca0b2.png
> [4]
> https://cloud.githubusercontent.com/assets/5307894/19622080/210b3796-9867-11e6-8fed-7c525b7a7b8f.png
> [5] https://github.com/ccordoba12
> [6] https://github.com/goanpeca
> [7] 
> https://github.com/spyder-ide/spyder/issues/3407#issuecomment-255550041
> [8]
> https://github.com/notifications/unsubscribe-auth/AH3bazIqqUuRsHY6joMGk-xuYIU-7mM7ks5q2mmYgaJpZM4J10sI

@dalthviz, please work on this one. For this you need to create a conda environment with a non-ascii name, install Spyder on it and start it.

Hi @Dschoni, sorry for the late response, can you try to test the problem with Spyder `3.0.2`?. Also, how are you setting up the active working directory, something like this?:

![image](https://cloud.githubusercontent.com/assets/16781833/20867995/0bc55586-ba1f-11e6-9a7a-dd4dd484be43.png)

All the information that you can give us would be greatly appreciated in order to recreate the error that you are having. :)

Hi Daniel,

I have tried reproducing the problem with spyder 3.0.2 and it seems to be (nearly) gone.

I get the following traceback when I change the working directory:


Traceback (most recent call last):
  File "/home/gu32hej/.local/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py", line 845, in set_current_client_working_directory
    shellwidget.set_cwd(directory)
  File "/home/gu32hej/.local/lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.py", line 87, in set_cwd
    "get_ipython().kernel.set_cwd(r'{}')".format(dirname))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd6' in position 14: ordinal not in range(128)


Nevertheless, Spyder DOES start when I close it and open it again!

So, solved for me.


________________________________
Von: Daniel Althviz Moré <notifications@github.com>
Gesendet: Sonntag, 4. Dezember 2016 22:32
An: spyder-ide/spyder
Cc: Schock, Jonathan; Mention
Betreff: Re: [spyder-ide/spyder] Sitecustomize fails when current directory has non-ascii chars (#3407)


Hi @Dschoni<https://github.com/Dschoni>, sorry for the late response, can you try to test the problem with Spyder 3.0.2?. Also, how are you setting up the active working directory, something like this?:

[image]<https://cloud.githubusercontent.com/assets/16781833/20867995/0bc55586-ba1f-11e6-9a7a-dd4dd484be43.png>

All the information that you can give us would be greatly appreciated in order to recreate the error that you are having. :)

-
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/spyder-ide/spyder/issues/3407#issuecomment-264733092>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AH3bawm5rpg0c8kKNxxedrmhcleviQVsks5rEzGGgaJpZM4J10sI>.

Sorry, no. It's back again. I tried with different bundled versions in Anaconda. With a plain Spyder install. 
The error message in terminal is still the same:

'import sitecustomize' failed; use -v for traceback 

Hi again @Dschoni, it's really strange that it worked for you  (at least to close and open Spyder), and then it stopped working. Searching a little bit I think that maybe your problem is about the configuration of  your firewall like in this [post](http://stackoverflow.com/q/17258634). From there are some steps that you can follow:

1. Disable the firewall.
2. Reset Spyder and apply default settings.
3. Run Spyder again.
4. Enable the firewall.

Can you see if following these steps make Spyder work again? I will open a new issue for the new error that you post when you ran Spyder `3.0.2`. Also if you can use the `-v` option to print the traceback and paste it here would be great.
?Hi Daniel,


Opening and closing Spyder works reliable. However, the import sitecustomize fails, with the aforementioned traceback.

The error is clearly "ordinal not in range" when closing spyder while in a working directory with non-ascii characters.

This leaves spyder in a "kind-of-working" state, but the internal python console doesn't work.

It is no matter of a firewall, as we are on Ubuntu without any firewall.

I'll send the full stack trace as soon as possible.

________________________________
Von: Daniel Althviz Moré <notifications@github.com>
Gesendet: Freitag, 9. Dezember 2016 00:25
An: spyder-ide/spyder
Cc: Schock, Jonathan; Mention
Betreff: Re: [spyder-ide/spyder] Sitecustomize fails when current directory has non-ascii chars (#3407)


Hi again @Dschoni<https://github.com/Dschoni>, it's really strange that it worked for you (at least to close and open Spyder), and then it stopped working. Searching a little bit I think that maybe your problem is about the configuration of your firewall like in this post<http://stackoverflow.com/q/17258634>. From there are some steps that you can follow:

  1.  Disable the firewall.
  2.  Reset Spyder and apply default settings.
  3.  Run Spyder again.
  4.  Enable the firewall.

Can you see if following these steps make Spyder work again? I will open a new issue for the new error that you post when you ran Spyder 3.0.2. Also if you can use the -v option to print the traceback and paste it here would be great.

-
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://github.com/spyder-ide/spyder/issues/3407#issuecomment-265886585>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AH3ba-SjpClQJrqvOHBjtiCMU8FC-sguks5rGJHggaJpZM4J10sI>.

@Dschoni, to allow printing the sitecustomize traceback, please start Spyder like this in a terminal

    SPYDER_DEBUG=3 spyder
Then the failure should be printed in our Python and IPython consoles.
So, finally back at work, here is the more exact error description: 

I open Spyder, I change to a working directory (via GUI) that has special characters (in my case, the german version of 'Desktop', which is 'Arbeitsfläche'). As soon, as I do that, the debug shell throws the following exception:

Traceback (most recent call last):
  File "/home/user/.local/lib/python2.7/site-packages/spyder/plugins/ipythonconsole.py", line 845, in set_current_client_working_directory
    shellwidget.set_cwd(directory)
  File "/home/user/.local/lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.py", line 87, in set_cwd
    "get_ipython().kernel.set_cwd(r'{}')".format(dirname))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 23: ordinal not in range(128)

When I close Spyder in this state and restart it (without resetting), the debug shell shows:

Traceback (most recent call last):
  File "/home/user/anaconda2/lib/python2.7/site.py", line 498, in execsitecustomize
    import sitecustomize
  File "/home/user/.local/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 241, in <module>
    import matplotlib
  File "/home/user/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 1138, in <module>
    rcParams = rc_params()
  File "/home/user/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 971, in rc_params
    fname = matplotlib_fname()
  File "/home/user/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 786, in matplotlib_fname
    cwd = os.getcwdu()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128)

leading to a failing import of sitecustomize and therefore unstable internal console. Resetting solves the problem (as I guess, it resets the current working directory to a default, without special characters).


Resolving patch:
diff --git a/spyder/utils/site/sitecustomize.py b/spyder/utils/site/sitecustomize.py
--- a/spyder/utils/site/sitecustomize.py
+++ b/spyder/utils/site/sitecustomize.py
@@ -239,7 +239,7 @@ def _getfilesystemencoding_wrapper():
 #==============================================================================
 try:
     import matplotlib
-except ImportError:
+except (ImportError, UnicodeDecodeError):
     matplotlib = None   # analysis:ignore
 
 


Test patch: