Repository: spyder-ide/spyder

Issue:
IPython consoles can't start if Spyder temp directory is missing
## Description

**What steps will reproduce the problem?**

1.  Accidentally ran some peripheral code that was included in the main file and had an import statement that didn't work. This stopped the import statement working in the main code.

2. Tried to restart the ipython console by clicking the cross on the tab (the tab was named 'Console x/A' where x is the console number). 

3.  After some time the console then outputted the following error

**What is the expected output? What do you see instead?**

Didn't I expected the console to not be able to import the module in one of the pieces of code but then import it in the other one. I fixed this by restarting spyder

**Please provide any additional information below**

  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 1240, in close_client
    self.create_new_client()
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 977, in create_new_client
    self.connect_client_to_kernel(client)
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 998, in connect_client_to_kernel
    stderr_file)
  File "/usr/local/lib/python3.5/dist-packages/spyder/plugins/ipythonconsole.py", line 1386, in create_kernel_manager_and_kernel_client
    stderr = codecs.open(stderr_file, 'w', encoding='utf-8')
  File "/usr/lib/python3.5/codecs.py", line 895, in open
    file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/spyder-ellismle/kernel-2e03abdb4eec.stderr'


## Version and main components

* Spyder Version: 3.2.1 
* Python Version: 3.5.2
* Qt Versions: 5.8.0, PyQt5 5.8.2 on Linux

## Dependencies
```
pyflakes >=0.6.0 :  1.6.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pygments >=2.0   :  2.2.0 (OK)
pandas >=0.13.1  :  0.20.3 (OK)
numpy >=1.7      :  1.13.1 (OK)
sphinx >=0.6.6   :  1.6.3 (OK)
rope >=0.9.4     :  0.10.5 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
psutil >=0.3     :  5.2.2 (OK)
nbconvert >=4.0  :  5.2.1 (OK)
sympy >=0.7.3    :  0.7.6.1 (OK)
cython >=0.21    :  None (NOK)
qtconsole >=4.2.0:  4.3.1 (OK)
IPython >=4.0    :  6.1.0 (OK)
pylint >=0.25    :  1.7.2 (OK)

```



Discussion:
Thanks for reporting. I don't understand very well how you ended up with this error, but we'll add an extra validation to prevent it.

@andfoy, please work on this one. The idea is to verify if the Spyder temp directory exists before trying to open the stderr file associated with a console. If it doesn't exist (for whatever odd reason) we need to create it again.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole.py b/spyder/plugins/ipythonconsole.py
--- a/spyder/plugins/ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole.py
@@ -586,11 +586,16 @@ class IPythonConsole(SpyderPluginWidget):
     CONF_SECTION = 'ipython_console'
     CONFIGWIDGET_CLASS = IPythonConsoleConfigPage
     DISABLE_ACTIONS_WHEN_HIDDEN = False
-    
+
     # Signals
     focus_changed = Signal()
     edit_goto = Signal((str, int, str), (str, int, str, bool))
 
+    # Error messages
+    permission_error_msg = _("The directory {} is not writable and it is "
+                             "required to create IPython consoles. Please "
+                             "make it writable.")
+
     def __init__(self, parent, testing=False, test_dir=TEMPDIR):
         """Ipython Console constructor."""
         if PYQT5:
@@ -953,9 +958,7 @@ def create_new_client(self, give_focus=True, filename=''):
         self.add_tab(client, name=client.get_name(), filename=filename)
 
         if cf is None:
-            error_msg = _("The directory {} is not writable and it is "
-                          "required to create IPython consoles. Please make "
-                          "it writable.").format(jupyter_runtime_dir())
+            error_msg = self.permission_error_msg.format(jupyter_runtime_dir())
             client.show_kernel_error(error_msg)
             return
 
@@ -998,7 +1001,13 @@ def create_client_for_kernel(self):
     def connect_client_to_kernel(self, client):
         """Connect a client to its kernel"""
         connection_file = client.connection_file
-        stderr_file = client.stderr_file
+        try:
+            stderr_file = client.stderr_file
+        except PermissionError:
+            error_msg = self.permission_error_msg.format(TEMPDIR)
+            client.show_kernel_error(error_msg)
+            return
+
         km, kc = self.create_kernel_manager_and_kernel_client(connection_file,
                                                               stderr_file)
         # An error occurred if this is True
diff --git a/spyder/widgets/ipythonconsole/client.py b/spyder/widgets/ipythonconsole/client.py
--- a/spyder/widgets/ipythonconsole/client.py
+++ b/spyder/widgets/ipythonconsole/client.py
@@ -167,6 +167,8 @@ def stderr_file(self):
             if self.stderr_dir is not None:
                 stderr_file = osp.join(self.stderr_dir, stderr_file)
             else:
+                if not osp.isdir(TEMPDIR):
+                    os.makedirs(TEMPDIR)
                 stderr_file = osp.join(TEMPDIR, stderr_file)
             return stderr_file
 


Test patch: