Repository: spyder-ide/spyder

Issue:
IPython console failing when custom interpreter is removed
```  
File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/__init__.py", line 511, in <lambda>
    toggled=lambda checked: self.toggle_view(checked),
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 695, in toggle_view
    self.create_new_client(give_focus=False)
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 974, in create_new_client
    self.connect_client_to_kernel(client)
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 995, in connect_client_to_kernel
    stderr_file)
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 1384, in create_kernel_manager_and_kernel_client
    kernel_manager.start_kernel(stderr=stderr)
  File "/usr/local/lib/python3.5/dist-packages/jupyter_client/manager.py", line 257, in start_kernel
    **kw)
  File "/usr/local/lib/python3.5/dist-packages/jupyter_client/manager.py", line 203, in _launch_kernel
    return launch_kernel(kernel_cmd, **kw)
  File "/usr/local/lib/python3.5/dist-packages/jupyter_client/launcher.py", line 128, in launch_kernel
    proc = Popen(cmd, **kwargs)
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '/var/www/html/PaintDetective/PaintDetective/penv/bin/python3.5'
```

This keeps happening each time I open spyder.


Discussion:
I had to go to;

```
Tools -> Reset spyder to factory defaults

```

And now it is working again.
Thanks for reporting. I think we should handle this more gracefully. 
No problem, initially I thought I was doing something wrong, so didn't report it for a few days!
@dalthviz, please work on this one too. We need to verify that if users have a custom Python interpreter selected, that it exists; and if not, use the default interpreter instead and set it in our Preferences.

We need to do that here:

https://github.com/spyder-ide/spyder/blob/3.x/spyder/utils/ipython/kernelspec.py#L45

And please also add a test for this that sets an inexistent interpreter using CONF, then creates an instance of SpyderKernelSpec and finally verifies that it's argv attribute contains the default interpreter.

Resolving patch:
diff --git a/spyder/utils/ipython/kernelspec.py b/spyder/utils/ipython/kernelspec.py
--- a/spyder/utils/ipython/kernelspec.py
+++ b/spyder/utils/ipython/kernelspec.py
@@ -16,6 +16,7 @@
 from spyder.config.base import get_module_source_path
 from spyder.config.main import CONF
 from spyder.utils.encoding import to_unicode_from_fs
+from spyder.utils.programs import is_python_interpreter
 from spyder.py3compat import PY2, iteritems, to_text_string, to_binary_string
 from spyder.utils.misc import (add_pathlist_to_PYTHONPATH,
                                get_python_executable)
@@ -43,6 +44,11 @@ def argv(self):
             # to the kernel sys.path
             os.environ.pop('VIRTUAL_ENV', None)
             pyexec = CONF.get('main_interpreter', 'executable')
+            if not is_python_interpreter(pyexec):
+                pyexec = get_python_executable()
+                CONF.set('main_interpreter', 'executable', '')
+                CONF.set('main_interpreter', 'default', True)
+                CONF.set('main_interpreter', 'custom', False)
 
         # Fixes Issue #3427
         if os.name == 'nt':


Test patch:
diff --git a/spyder/utils/ipython/tests/test_spyder_kernel.py b/spyder/utils/ipython/tests/test_spyder_kernel.py
--- a/spyder/utils/ipython/tests/test_spyder_kernel.py
+++ b/spyder/utils/ipython/tests/test_spyder_kernel.py
@@ -9,7 +9,6 @@
 """
 
 import os
-
 import pytest
 
 from spyder.config.main import CONF
@@ -18,6 +17,23 @@
 from spyder.utils.ipython.kernelspec import SpyderKernelSpec
 
 
+def test_python_interpreter(tmpdir):
+    """Test the validation of the python interpreter."""
+    # Set a non existing python interpreter
+    interpreter = str(tmpdir.mkdir('interpreter').join('python'))
+    CONF.set('main_interpreter', 'default', False)
+    CONF.set('main_interpreter', 'custom', True)
+    CONF.set('main_interpreter', 'executable', interpreter)
+
+    # Create a kernel spec
+    kernel_spec = SpyderKernelSpec()
+
+    # Assert that the python interprerter is the default one
+    assert interpreter not in kernel_spec.argv
+    assert CONF.get('main_interpreter', 'default')
+    assert not CONF.get('main_interpreter', 'custom')
+
+
 @pytest.mark.skipif(os.name != 'nt' or not PY2,
                     reason="It only makes sense on Windows and Python 2")
 def test_env_vars():