Repository: spyder-ide/spyder

Issue:
Cannot import notebook variables
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

cannot import notebook variables into the variables explorer

### Traceback
```python-traceback
  File "/home/mhizzani/anaconda3/lib/python3.6/site-packages/spyder/widgets/ipythonconsole/client.py", line 571, in show_env
    self.dialog_manager.show(RemoteEnvDialog(env))
  File "/home/mhizzani/anaconda3/lib/python3.6/site-packages/spyder/utils/environ.py", line 45, in __init__
    self.setup(envdict2listdict(environ),
  File "/home/mhizzani/anaconda3/lib/python3.6/site-packages/spyder/utils/environ.py", line 26, in envdict2listdict
    for key in envdict:
TypeError: 'NoneType' object is not iterable
WARNING:spyder.widgets.github.backend:failed to send bug report on github. response={'code': 401, 'json': {'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}}
WARNING:spyder.widgets.github.backend:failed to send bug report on github. response={'code': 401, 'json': {'message': 'Bad credentials', 'documentation_url': 'https://developer.github.com/v3'}}
```

## Versions

* Spyder version: 3.3.1 
* Python version: 3.6.6
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Linux 4.15.0-32-generic

### Dependencies

```
pyflakes >=0.6.0  :  2.0.0 (OK)
pycodestyle >=2.3 :  2.4.0 (OK)
pygments >=2.0    :  2.2.0 (OK)
sphinx >=0.6.6    :  1.7.6 (OK)
rope >=0.9.4      :  0.11.0 (OK)
jedi >=0.9.0      :  0.12.1 (OK)
psutil >=0.3      :  5.4.6 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
pandas >=0.13.1   :  0.23.4 (OK)
numpy >=1.7       :  1.15.0 (OK)
sympy >=0.7.3     :  1.2 (OK)
cython >=0.21     :  0.28.5 (OK)
qtconsole >=4.2.0 :  4.3.1 (OK)
IPython >=4.0     :  6.5.0 (OK)
matplotlib >=2.0.0:  2.2.3 (OK)
pylint >=0.25     :  2.1.1 (OK)
```



Discussion:
> cannot import notebook variables into the variables explorer

Please explain what you mean by this. How are importing notebook variables in our variable explorer?
When open a notebook inside spyder, the variables of the notebook kernel do not appears in "variables explorer" window, and when choose "show kernel variables" from the console an error prompt appears.
> show kernel variables

There's no option called like that in Spyder. Please post a screenshot to see what you're doing. Thanks!
You can see in (the variables do not appear in "Variables explorer", and when choose the option(green box) and error occurred  

![screenshot from 2018-08-23 22-54-01](https://user-images.githubusercontent.com/30414163/44548751-a5ade400-a727-11e8-85e4-cdb19eb0d06d.png)

![screenshot from 2018-08-23 22-56-59](https://user-images.githubusercontent.com/30414163/44548833-e3127180-a727-11e8-99ad-3a5773ac1121.png)

Thanks for the screenshots. I understand now what you're trying to do and the meantime you found an error in Spyder.

To see the notebook variables in the Variable Explorer you need to

1. Select the option called *Open console* from the Notebook pane options menu:

![seleccion_010](https://user-images.githubusercontent.com/365293/44558408-94ee7580-a709-11e8-8045-bb5f1ab79038.png)

2. After that, you'll see a new console is opened in the IPython console pane with the same of the notebook.

3. For now you need to press *Enter* in that console to keep updated the Variable Explorer after you run new cells in your notebook

![seleccion_011](https://user-images.githubusercontent.com/365293/44558508-3f669880-a70a-11e8-8eb8-e3a61c86ae70.png)

----

We know this is very cumbersome and we're working to improve the process in the future, so that you don't need to press Enter in the console and you don't even need to connect a console to the notebook for the Variable Explorer to work.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole.py b/spyder/plugins/ipythonconsole.py
--- a/spyder/plugins/ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole.py
@@ -1436,8 +1436,8 @@ def restart(self):
             except Exception as e:
                 QMessageBox.warning(self, _('Warning'),
                     _("It was not possible to restart the IPython console "
-                      "when switching to this project. "
-                      "The error was {0}").format(e), QMessageBox.Ok)
+                      "when switching to this project. The error was<br><br>"
+                      "<tt>{0}</tt>").format(e), QMessageBox.Ok)
             self.close_client(client=client, force=True)
         self.create_new_client(give_focus=False)
         self.create_new_client_if_empty = True
diff --git a/spyder/utils/environ.py b/spyder/utils/environ.py
--- a/spyder/utils/environ.py
+++ b/spyder/utils/environ.py
@@ -42,11 +42,23 @@ class RemoteEnvDialog(CollectionsEditor):
 
     def __init__(self, environ, parent=None):
         super(RemoteEnvDialog, self).__init__(parent)
-        self.setup(envdict2listdict(environ),
-                   title=_("Environment variables"),
-                   width=700,
-                   readonly=True,
-                   icon=ima.icon('environ'))
+        try:
+            self.setup(
+                envdict2listdict(environ),
+                title=_("Environment variables"),
+                width=700,
+                readonly=True,
+                icon=ima.icon('environ')
+            )
+        except Exception as e:
+            QMessageBox.warning(
+                parent,
+                _("Warning"),
+                _("An error occurred while trying to show your "
+                  "environment variables. The error was<br><br>"
+                  "<tt>{0}</tt>").format(e),
+                QMessageBox.Ok
+            )
 
 
 class EnvDialog(RemoteEnvDialog):
@@ -91,7 +103,7 @@ def set_user_env(reg, parent=None):
                                   SMTO_ABORTIFHUNG)
             SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
                                "Environment", SMTO_ABORTIFHUNG, 5000)
-        except ImportError:
+        except Exception:
             QMessageBox.warning(parent, _("Warning"),
                         _("Module <b>pywin32 was not found</b>.<br>"
                           "Please restart this Windows <i>session</i> "
@@ -122,7 +134,7 @@ def accept(self):
             set_user_env(listdict2envdict(self.get_value()), parent=self)
             QDialog.accept(self)
 
-except ImportError:
+except Exception:
     pass
 
 def main():


Test patch: