Repository: spyder-ide/spyder

Issue:
Error reading kernel connection file from a directory with spaces in it
Win 10 x64, Spyder 5.5.0, spyder-kernels 2.5 (as told to install by Spyder)

## Problem Description

Tried to connect to an evironment interpreter but even after installing spyder-kernels==2.5 it just cant connect to the kernel or in fact any kernel

### What steps reproduce the problem?

Open Spyder

### What is the expected output? What do you see instead?

For Spyder to run & connect to at least one of my interpreters


### Paste Traceback/Error Below (if applicable)
[SpyderKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\Nic'
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\depthai\lib\site‑packages\ipykernel\kernelapp.py", line 315, in init_connection_file
self.load_connection_file()
File "C:\Users\Nic Bwts\AppData\Roaming\Python\Python38\site‑packages\jupyter_client\connect.py", line 536, in load_connection_file
info = json.load(f)
File "C:\ProgramData\Anaconda3\envs\depthai\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\ProgramData\Anaconda3\envs\depthai\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\ProgramData\Anaconda3\envs\depthai\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\ProgramData\Anaconda3\envs\depthai\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 3 (char 2)

I have uninstalled all previous versions (as they were firing all sorts of dependency issues) & installed as per instructions on your site.




Discussion:
Can you try with the following environment?

    conda create -c conda-forge -n myenv spyder-kernels

    conda activate myenv
    conda config --env --add channels conda-forge
    conda config --env --set channel_priority strict

Now try to use this environment with the Spyder 5.5.0 stand-alone install. This environment is set to use conda-forge rather than the default Anaconda channel for packages (**). Now install the other packages you need.

If that does not work, does it work using the interpreter that comes with Spyder? Try resetting Spyder settings.

It looks like you are using Anaconda, so make sure you are not launching the Spyder that ships with Anaconda. 

(**) Anaconda (and the default channel) is great and all, but it is often outdated and is quite bloated. Generally a source of headaches for many users who wish to create custom envs. If you plan on using custom environments a lot I highly recommend switching to mambaforge (miniforge with mamba, a much faster conda variant that defaults to using conda-forge). If doing so, uninstall Anaconda and the environments you've created with it first. Then type mamba instead of conda for all commands, and you can skip the config calls I wrote above.

Wrote this on a phone so apologies for the formatting.
Hey @DrBwts, thanks for reporting. Besides following @rhkarls advice, please remove this directory using the Windows file explorer:

    C:\Users\Nic Bwts\AppData\Roaming\Python\Python38\site‑packages

because it seems it's interfering with your Anaconda installation.

> Tried to connect to an evironment interpreter but even after installing spyder-kernels==2.5

How did you install Spyder-kernels? Please describe the step by step process you used to do it.

> I know this isn't helpful & I know this is freeware but over the last year Spyder has become unusable due to installation problems like this. I jumped on to just test some code & I have been fighting with Spyder for the last 2 hours & it still doesn't run! If this is the new normal with Spyder, I can see why people are fleeing....

We haven't heard about major problems during the last couple of years, so this seems related to your personal setup.
@rhkarls I set up the environment as outlined, opened Spyder & selected the interepreter of that environment & got,

```
SpyderKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\Nic'
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\myenv\Lib\site‑packages\ipykernel\kernelapp.py", line 315, in init_connection_file
self.load_connection_file()
File "C:\ProgramData\Anaconda3\envs\myenv\Lib\site‑packages\jupyter_client\connect.py", line 534, in load_connection_file
info = json.load(f)
^^^^^^^^^^^^
File "C:\ProgramData\Anaconda3\envs\myenv\Lib\json\__init__.py", line 293, in load
return loads(fp.read(),
^^^^^^^^^^^^^^^^
File "C:\ProgramData\Anaconda3\envs\myenv\Lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\Anaconda3\envs\myenv\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\Anaconda3\envs\myenv\Lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 3 (char 2)
```

> does it work using the interpreter that comes with Spyder? Try resetting Spyder settings.

Yes

@ccordoba12  

> please remove this directory using the Windows file explorer:

Done as advised still no luck.

> How did you install Spyder-kernels? Please describe the step by step process you used to do it.

Activated enviromnent & used,

`pip install spyder-kernels==2.5.*`
 
 Then checked,
 
![image](https://github.com/spyder-ide/spyder/assets/22524334/6427573e-3d82-48a2-b204-7274b02078dc)

Apologies for initial snark....

I suspect that this is caused by the white-space in your Windows user name (path under C:\Users\). For example, I know that miniforge/mambaforge won't even allow users to install when there is a space in the path as this is known to cause problems for a lot of packages. I think it's not sufficient to place your environment in a different location, as the kernel connection file will still end up in the user folder. Perhaps there is a way to configure this and work around it and @ccordoba12 can weigh in on that? Another option is of course to create a new user or try to rename the old Windows user somehow. 

PS: it might seem that you are also mixing packages installed with pip and conda in the same environment? This is generally not a good idea since the different dependency solvers will fail at some point. Even mixing different conda channels can get messy. Another good reason to use `conda-forge` channel and `mambaforge` (or miniforge) when you need to create a lot of custom environments :)
> I suspect that this is caused by the white-space in your Windows user name

Yeah, I think @rhkarls is quite right, especially after reading the first line of the traceback you posted above:

    SpyderKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\Nic'

So, it seems the code to read your kernel connection file can't deal with spaces.

@blink1073, I thought Jupyter-client was able to deal with that case.
I would think so, but we don't test it explicitly.
I don't see anything special in the Jupyter-client code to deal with spaces in the kernel connection file. But I wonder why this problem hasn't been reported more widely before.

@DrBwts, unfortunately we don't have time to deal with this right now, but we'll try to submit a patch to Jupyter-client in the coming months. At least you know how to deal with it now.
OK thanks for the help.
Just to confirm that this is the issue. When set up with user accounts with no white space Spyder works as expected.
Great! Thanks for letting us know about it.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/widgets/main_widget.py b/spyder/plugins/ipythonconsole/widgets/main_widget.py
--- a/spyder/plugins/ipythonconsole/widgets/main_widget.py
+++ b/spyder/plugins/ipythonconsole/widgets/main_widget.py
@@ -1386,9 +1386,9 @@ def interpreter_versions(self):
             import subprocess
             versions = {}
             pyexec = self.get_conf('executable', section='main_interpreter')
-            py_cmd = u'%s -c "import sys; print(sys.version)"' % pyexec
+            py_cmd = '"%s" -c "import sys; print(sys.version)"' % pyexec
             ipy_cmd = (
-                u'%s -c "import IPython.core.release as r; print(r.version)"'
+                '%s -c "import IPython.core.release as r; print(r.version)"'
                 % pyexec
             )
             for cmd in [py_cmd, ipy_cmd]:


Test patch: