Repository: spyder-ide/spyder

Issue:
Improve error message when trying to view a dataframe and Pandas is not installed next to Spyder
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [ yes] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ yes] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ ] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [ ] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [ ] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
After opening spyder in virtualenv, 
checked pip freeze and lists pandas , numpy , sklearn everything needed for data analysis
the i wrote a  code and ran it.
it worked well., but when i am trying to open same variable in variable explorer window, it showing error

![image](https://user-images.githubusercontent.com/16902073/80786088-2204f580-8ba0-11ea-9f87-81471fffcb3a.png)




### What steps reproduce the problem?

1. install spyder using pip 
2. open spyder in virtualenv
3. ran the code and check in variable explorer

### What is the expected output? What do you see instead?
IPython console is working fine and giving perferct results, 
only variable explorer is not able to open variables that are coming form using pandas library
i tired defining numpy array and list , i could able to open them as below
![image](https://user-images.githubusercontent.com/16902073/80786256-9fc90100-8ba0-11ea-912f-689ff6aae282.png)

but when i am trying open pandas dataframe variable , variable explorer is throwing below error
![image](https://user-images.githubusercontent.com/16902073/80786283-ba9b7580-8ba0-11ea-9bce-2ce64510bbef.png)



### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

PASTE TRACEBACK HERE

```

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.1.2
* Python version: 3.8
* Qt version: 
* PyQt version: 
* Operating System name/version: Windows 10

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```

# Mandatory:
atomicwrites >=1.2.0           :  1.4.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.4.0 (OK)
diff_match_patch >=20181111    :  20181111 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.13.0 (OK)
jedi =0.15.2                   :  0.15.2 (OK)
nbconvert >=4.0                :  5.6.1 (OK)
numpydoc >=0.6.0               :  0.9.2 (OK)
paramiko >=2.4.0               :  2.7.1 (OK)
parso =0.5.2                   :  0.5.2 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.0 (OK)
pygments >=2.0                 :  2.6.1 (OK)
pylint >=0.25                  :  2.5.0 (OK)
pyls >=0.31.9;<0.32.0          :  0.31.10 (OK)
qdarkstyle >=2.8               :  2.8.1 (OK)
qtawesome >=0.5.7              :  0.7.1 (OK)
qtconsole >=4.6.0              :  4.7.3 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
sphinx >=0.6.6                 :  3.0.3 (OK)
spyder_kernels >=1.9.0;<1.10.0 :  1.9.0 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  19.0.0 (OK)

# Optional:
cython >=0.21                  :  None (OK)
matplotlib >=2.0.0             :  None (OK)
numpy >=1.7                    :  1.18.2 (OK)
pandas >=0.13.1                :  None (OK)
scipy >=0.17.0                 :  None (OK)
sympy >=0.7.3                  :  None (OK)

```



Discussion:
I can confirm that this occurs for bootstrap startup in a clean conda environment using the latest 4.x commit.

It seems to occur only if the internal Spyder python environment does not have the package installed, even when using an external interpreter that does have the package installed. In other words, I think that the variable explorer is accessing the wrong kernel.
Thanks for the report @waageesh. Even if the external kernel has pandas installed, I think internally we still need to have pandas install to process results.

You can see that the optional pandas dependency is NOT installed.

![image](https://user-images.githubusercontent.com/3627835/80931013-f2f7ab00-8d7c-11ea-9140-aa877f392cf1.png)

I think if you install pandas on your Spyder environment, it should work jut fine. Let us know.

---

>I can confirm that this occurs for bootstrap startup in a clean conda environment using the latest 4.x commit.

Thanks for checking @mrclary, see above comment :-) 
@goanpeca, do you mean that this is _not_ a bug? It seems to me that the Variable Explorer ought to access the iPython Console's kernel, not Spyder's kernel (unless it is the same as the Console's).
>do you mean that this is not a bug? 

Yes, exactly, since Spyder still requires pandas on the Spyder side (data frame editor) to be able to view a pandas dataframe.

https://github.com/spyder-ide/spyder/blob/4.x/spyder/plugins/variableexplorer/widgets/dataframeeditor.py#L47


Ahh, I see. Clicking on an item in the Variable Explorer is supposed to activate an object _editor_. I was thinking it just gave one an expanded view of the object.

We should probably highlight this in user documentation somewhere, particularly for those that use a minimal environment from which to launch Spyder but use a separate environment for their Consoles. Note, this also applies to numpy arrays.
>We should probably highlight this in user documentation somewhere, particularly for those that use a minimal environment from which to launch Spyder but use a separate environment for their Consoles. Note, this also applies to numpy arrays.

Indeed, we might need better messaging or reconsider moving numpy and pandas from optional to mandatory dependencies?

Thoughts @spyder-ide/core-developers ?
I think the error message should explain the situation. Something like `optional dependency <package> not found in spyder environment`

I would also be ok with them being mandatory.
Yes, indeed!
@dalthviz, please explain @juanis2112 what's necessary to solve this issue.
I encountered the same bug and it took me a while before I understood what went wrong and to find this issue eventually. I use the **Variable Explorer** mostly to explore DataFrames so I would expect this feature to work out-of-the-box, thus I'm +1 on having Pandas (hence Numpy) as a hard dependency here.

Note that even if `pandas` isn't installed the DataFrame shows up in the *Variable Explorer*, it's even able to parse it somehow without `pandas` as the column names are displayed.
![image](https://user-images.githubusercontent.com/35924738/81470983-6c185780-91ee-11ea-9c3e-e910cce216d3.png)

Are there other types of object that could lead to the same error besides the Numpy and Pandas ones?

I'll update the wiki (see #12107) according to your decision here.
@maximlt indeed, the issues is installing pandas means also numpy and scipy which are big libraries, but given the need is the road we will probably need to take.
It sure would be nice, someday, if its even possible, to provide a way for Spyder to edit Variable Explorer objects without the need for those packages to be installed in Spyder's runtime environment. In other words, use the iPython Console's kernel. That would solve these issues and reduce the "required" footprint of Spyder, especially for a standalone application.
>It sure would be nice, someday, if its even possible, to provide a way for Spyder to edit Variable Explorer objects without the need for those packages to be installed in Spyder's runtime environment. In other words, use the iPython Console's kernel. That would solve these issues and reduce the "required" footprint of Spyder, especially for a standalone application.

Yeah we actually need to do that to be able to support other objects in other languages in other Kernels, so something we definitely need to address.
> In other words, use the iPython Console's kernel

Then Qt would need to be installed in the kernel, which is way bigger and harder to install correctly than Pandas. Besides, that workflow won't work for remote kernels. So I don't see much future in that idea.
> Besides, that workflow won't work for remote kernels. So I don't see much future in that idea.

I guess what @mrclary means is that we could serialize some information of the object in the kernel and pass that in some format to the frontend, and then we could display that. This would be simple for small objects but not very simple for larger ones.
Oh yeah, that's in our roadmap for Spyder 5 (sorry for the misunderstanding).

Resolving patch:
diff --git a/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py b/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
--- a/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
+++ b/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
@@ -97,6 +97,7 @@ def show_warning(self, index):
 
     def createEditor(self, parent, option, index, object_explorer=False):
         """Overriding method createEditor"""
+        val_type = index.sibling(index.row(), 1).data()
         self.sig_open_editor.emit()
         if index.column() < 3:
             return None
@@ -112,6 +113,32 @@ def createEditor(self, parent, option, index, object_explorer=False):
             value = self.get_value(index)
             if value is None:
                 return None
+        except ImportError as msg:
+            self.sig_editor_shown.emit()
+            module = str(msg).split("'")[1]
+            if module in ['pandas', 'numpy']:
+                if module == 'numpy':
+                    val_type = 'array'
+                else:
+                    val_type = 'dataframe, series'
+                QMessageBox.critical(
+                    self.parent(), _("Error"),
+                    _("Spyder is unable to show the {val_type} or object "
+                      "you're trying to view because <tt>{module}</tt> was "
+                      "not installed alongside Spyder. Please install "
+                      "this package in your Spyder environment."
+                      "<br>").format(val_type=val_type, module=module))
+                return
+            else:
+                QMessageBox.critical(
+                    self.parent(), _("Error"),
+                    _("Spyder is unable to show the variable you're "
+                      "trying to view because the module "
+                      "<tt>{module}</tt> was not found in your  "
+                      "Spyder environment. Please install "
+                      "this package in your Spyder environment."
+                      "<br>").format(module=module))
+                return
         except Exception as msg:
             QMessageBox.critical(
                 self.parent(), _("Error"),


Test patch: