Repository: spyder-ide/spyder

Issue:
AttributeError when running code in the IPython console
## Description

**What steps will reproduce the problem?**

1. 
2. 
3. 

**What is the expected output? What do you see instead?**


**Please provide any additional information below**

  File "/home/lab326/anaconda3/lib/python3.5/site-packages/spyder/app/mainwindow.py", line 2489, in execute_in_external_console
    console.execute_code(lines)
  File "/home/lab326/anaconda3/lib/python3.5/site-packages/spyder/plugins/ipythonconsole.py", line 970, in execute_code
    sw.execute(to_text_string(lines))
  File "/home/lab326/anaconda3/lib/python3.5/site-packages/qtconsole/history_console_widget.py", line 47, in execute
    source, hidden, interactive)
  File "/home/lab326/anaconda3/lib/python3.5/site-packages/qtconsole/console_widget.py", line 632, in execute
    self._execute(source, hidden)
  File "/home/lab326/anaconda3/lib/python3.5/site-packages/qtconsole/frontend_widget.py", line 294, in _execute
    msg_id = self.kernel_client.execute(source, hidden)
AttributeError: 'NoneType' object has no attribute 'execute'


## Version and main components

* Spyder Version: 3.2.6 
* Python Version: 3.5.2
* Qt Versions: 5.9.3, PyQt5 5.9.2 on Linux

## Dependencies
```
pyflakes >=0.6.0 :  1.3.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pygments >=2.0   :  2.1.3 (OK)
pandas >=0.13.1  :  0.18.1 (OK)
numpy >=1.7      :  1.11.3 (OK)
sphinx >=0.6.6   :  1.4.6 (OK)
rope >=0.9.4     :  0.10.7 (OK)
jedi >=0.9.0     :  0.9.0 (OK)
psutil >=0.3     :  4.3.1 (OK)
nbconvert >=4.0  :  4.2.0 (OK)
sympy >=0.7.3    :  1.0 (OK)
cython >=0.21    :  0.24.1 (OK)
qtconsole >=4.2.0:  4.3.1 (OK)
IPython >=4.0    :  5.1.0 (OK)
pylint >=0.25    :  1.5.4 (OK)

```



Discussion:
Thanks for reporting. This error appears similar to #6035 , which was fixed by the user resetting preferences with ``spyder --reset`` at the command line. Please try that, along with the other steps in our [Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ), specifically the [Basic First Aid](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ#basic-first-aid-general-troubleshooting) and [Emergency CPR](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ#emergency-cpr-spyder-wont-launch) sections. If you still can't get it to work, please follow the procedure in the [Calling for Help](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ#calling-for-help-still-have-a-problem) section, including the requested information in a followup comment here. Otherwise, if we don't hear from you, this issue will be closed after one week (7 days).
I still don't understand why this error appears, but we can add a try/except around `sw.execute(to_text_string(lines))` to avoid it.

@dalthviz, please do that and only catch `AttributeError`.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole.py b/spyder/plugins/ipythonconsole.py
--- a/spyder/plugins/ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole.py
@@ -967,7 +967,12 @@ def execute_code(self, lines, current_client=True, clear_variables=False):
                     sw.reset_namespace(warning=False, silent=True)
                 elif current_client and clear_variables:
                     sw.reset_namespace(warning=False, silent=True)
-                sw.execute(to_text_string(lines))
+                # Needed to handle an error when kernel_client is none
+                # See issue 6308
+                try:
+                    sw.execute(to_text_string(lines))
+                except AttributeError:
+                    pass
             self.activateWindow()
             self.get_current_client().get_control().setFocus()
 


Test patch: