Repository: spyder-ide/spyder
Pull request: PR: Improve error message for loading spydata with missing dependencies
<!--- Make sure to read the Contributing Guidelines:                   --->
<!--- https://github.com/spyder-ide/spyder/blob/master/CONTRIBUTING.md --->
<!--- and follow PEP 8, PEP 257 and Spyder's code style:               --->
<!--- https://github.com/spyder-ide/spyder/wiki/Dev:-Coding-Style      --->

## Description of Changes


* [ ] Wrote at least one-line docstrings (for any new functions)
* [ ] Added unit test(s) covering the changes (if testable)
<!--- Remember that an image/animation is worth a thousand words! --->
* [x] Included a screenshot or animation (if affecting the UI, see [Licecap](https://www.cockos.com/licecap/))


<!--- Explain what you've done and why --->
Improve error message for loading spydata with missing dependencies
![Screen Shot 2020-06-17 at 11 37 42 PM](https://user-images.githubusercontent.com/18587879/84978835-8d286e00-b0f3-11ea-9f9b-7aab46004353.png)




### Issue(s) Resolved

<!--- List the issue(s) below, in the form "Fixes #1234"; one per line --->

Fixes #12799 


### Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the [Developer Certificate of Origin](https://developercertificate.org)
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

<!--- TYPE YOUR USER/NAME AFTER THE FOLLOWING: --->
I certify the above statement is true and correct:
juanis2112
<!--- Thanks for your help making Spyder better for everyone! --->


Problem statement:
Improve error message when loading spydata files
## Description

### What steps will reproduce the problem?

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

When trying to load previously saved data, this issue occurs. 

### Traceback
```python-traceback
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder/plugins/variableexplorer/widgets/namespacebrowser.py", line 207, in <lambda>
    triggered=lambda: self.import_data())
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder/plugins/variableexplorer/widgets/namespacebrowser.py", line 444, in import_data
    error_message = self.shellwidget.load_data(self.filename, ext)
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py", line 125, in load_data
    timeout=CALL_KERNEL_TIMEOUT).load_data(filename, ext)
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 542, in __call__
    call_dict, call_data, self._comm_id)
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 114, in _get_call_return_value
    call_dict, call_data, comm_id)
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 420, in _get_call_return_value
    return self._sync_error(reply['value'])
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 480, in _sync_error
    error_wrapper.raise_error()
  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 90, in raise_error
    raise self.etype(self)
Exception in comms call load_data:

  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 342, in _handle_remote_call
    buffer['call_kwargs'])

  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 353, in _remote_callback
    *call_args, **call_kwargs)

  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/console/kernel.py", line 249, in load_data
    data, error_message = load_func(filename)

  File "/Users/jacob/anaconda/envs/py3/lib/python3.7/site-packages/spyder_kernels/utils/iofuncs.py", line 406, in load_dictionary
    data = pickle.loads(fdesc.read())

ModuleNotFoundError: No module named 'geopandas'

```

## Versions

* Spyder version: 4.0.1 
* Python version: 3.7.0
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Darwin 18.7.0

### Dependencies

```
applaunchservices >=0.1.7    :  0.2.1 (OK)
atomicwrites >=1.2.0         :  1.3.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
cloudpickle >=0.5.0          :  1.3.0 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
intervaltree                 :  None (OK)
IPython >=4.0                :  7.13.0 (OK)
jedi =0.14.1                 :  0.14.1 (OK)
nbconvert >=4.0              :  5.6.1 (OK)
numpydoc >=0.6.0             :  0.9.2 (OK)
pexpect >=4.4.0              :  4.8.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
psutil >=0.3                 :  5.7.0 (OK)
pygments >=2.0               :  2.6.1 (OK)
pylint >=0.25                :  2.4.4 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.9 (OK)
zmq >=17                     :  18.1.1 (OK)
qdarkstyle >=2.7             :  2.8 (OK)
qtawesome >=0.5.7            :  0.7.0 (OK)
qtconsole >=4.6.0            :  4.7.1 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
rtree >=0.8.3                :  0.9.3 (OK)
sphinx >=0.6.6               :  2.4.0 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.8.1 (OK)
watchdog                     :  None (OK)
cython >=0.21                :  0.29.15 (OK)
matplotlib >=2.0.0           :  3.1.0 (OK)
numpy >=1.7                  :  1.18.1 (OK)
pandas >=0.13.1              :  0.25.0 (OK)
scipy >=0.17.0               :  1.4.1 (OK)
sympy >=0.7.3                :  1.5.1 (OK)
```

Hints from issue discussion:
It seems you don't have the `geopandas` module installed in your `py3` env. Can you confirm?
Yes.

I had to shift environments between projects. I did that and the issue was
resolved.

On Tue, May 19, 2020 at 2:35 PM Carlos Cordoba <notifications@github.com>
wrote:

> It seems you don't have the geopandas module installed in your py3 env.
> Can you confirm?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/spyder-ide/spyder/issues/12799#issuecomment-631037446>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ADBFKDLASKOEE2EVCOFSMO3RSLNRXANCNFSM4NFGHE2A>
> .
>


-- 
Regards,

Jacob Mathew,
Grad Student in Civil Engineering
University of Illinois at Urbana Champaign

Glad to know you sorted it out.

Cheers!
This is related to another issue @juanis2112 is working on right now. So I think:

1. We could also show in this case a message telling the user what's the problem and what they need to do to solve it, instead of displaying an ugly traceback.
2. She could also work on this.

Commit message: Improve error message for loading spydata with missing dependencies

Patch to review:
diff --git a/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py b/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
index 8637d782f90..847a56d7b04 100644
--- a/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
+++ b/spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
@@ -137,6 +137,14 @@ def load_data(self, filename, ext):
                 blocking=True,
                 timeout=CALL_KERNEL_TIMEOUT).load_data(
                     filename, ext, overwrite=overwrite)
+        except ImportError as msg:
+            module = str(msg).split("'")[1]
+            msg = _("Spyder is unable to open the variable or object "
+                    "you're trying to load because <tt>{module}</tt> is "
+                    "not installed. Please install "
+                    "this package in your working environment."
+                    "<br>").format(module=module)
+            return msg
         except TimeoutError:
             msg = _("Data is too big to be loaded")
             return msg


Code review comments:
diff_hunk: @@ -137,6 +137,14 @@ def load_data(self, filename, ext):
                 blocking=True,
                 timeout=CALL_KERNEL_TIMEOUT).load_data(
                     filename, ext, overwrite=overwrite)
+        except ImportError as msg:
+            module = str(msg).split("'")[1]
+            msg = _("Spyder is unable to open the variable or object "
path: spyder/plugins/ipythonconsole/widgets/namespacebrowser.py
text: @user1:
```suggestion
            msg = _("Spyder is unable to open the file "
```

In this case users are trying to open a file, not a variable or an object.