friendly-traceback / friendly

Friendly-traceback's version used in most situations
https://friendly-traceback.github.io/docs/index.html
MIT License
41 stars 5 forks source link

Importing into Jupyter notebook raises error #2

Closed psychemedia closed 3 years ago

psychemedia commented 3 years ago

Hi

This is a really useful looking project for supporting novice coders.

I just tried installing the current version from PyPi (0.4.1) and running in a Jupyter notebook (classic and in JupyterLab) but I get an error on the initial import:

%pip install friendly
from friendly.jupyter import *

Generates the error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-612f8b1979f1> in <module>
----> 1 from friendly.jupyter import *
      2 
      3 
      4 

~/.local/lib/python3.8/site-packages/friendly/jupyter.py in <module>
      1 from friendly_traceback import session  # noqa
----> 2 from .ipython import *  # noqa
      3 from .ipython import helpers
      4 from friendly import rich_formatters
      5 

~/.local/lib/python3.8/site-packages/friendly/ipython.py in <module>
     34 # so as to shorten name of code block "files" for SyntaxError
     35 # cases - since SyntaxErrors do not generate frames.
---> 36 frames = inspect.getouterframes(inspect.currentframe())
     37 session.ipython_frame = None
     38 for frame_info in frames:

/usr/lib/python3.8/inspect.py in getouterframes(frame, context)
   1489     framelist = []
   1490     while frame:
-> 1491         frameinfo = (frame,) + getframeinfo(frame, context)
   1492         framelist.append(FrameInfo(*frameinfo))
   1493         frame = frame.f_back

/usr/lib/python3.8/inspect.py in getframeinfo(frame, context)
   1463         start = lineno - 1 - context//2
   1464         try:
-> 1465             lines, lnum = findsource(frame)
   1466         except OSError:
   1467             lines = index = None

/usr/lib/python3.8/inspect.py in findsource(object)
    838         pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
    839         while lnum > 0:
--> 840             if pat.match(lines[lnum]): break
    841             lnum = lnum - 1
    842         return lines, lnum

IndexError: list index out of range

In passing, I note that from friendly.jupyter import * is a really dodgy (and bad practice) thing to inflict onto novices. They donlt really have any idea what's in the namespace or how it works anyway, so importing who knows what via * could create all sorts of inexplicable errors and misunderstandings for them.

aroberge commented 3 years ago

Thanks for the report, I will look at it right away.

I understand about the * import suggestion. I have tried to explain why here: https://friendly-traceback.github.io/docs/import_all.html

aroberge commented 3 years ago

@psychemedia Could you tell me which version of Jupyter notebook/lab you are using? I cannot reproduce the error here, but I am using the latest version of jupyter:

> jupyter --version
jupyter core     : 4.7.1
jupyter-notebook : 6.4.0
qtconsole        : not installed
ipython          : 7.25.0
ipykernel        : 6.0.1
jupyter client   : 6.1.12
jupyter lab      : 3.0.16
nbconvert        : 6.1.0
ipywidgets       : not installed
nbformat         : 5.1.3
traitlets        : 5.0.5

Looking at the traceback you included, I see that code blocks "file names" are of the form "" which indicates to me that this is an older version than the one I am using. I really should support older versions as well...

aroberge commented 3 years ago

Version 0.4.2 uploaded to PyPI should work.

psychemedia commented 3 years ago

Seems to import okay with that latest version, thanks (though reports an older version?)

from friendly.jupyter import *
> friendly_traceback 0.4.0; friendly 0.4.1.

Re: import *: I just noticed your explanation in the docs:-) I think it would be useful to add just a minimal import statement that shows how to load in just what's required to support the richer traceback messages. Eg at the moment, I'm simply interested in the richer traceback/error message part of the package, but not any of the interactive commands which is perhaps more just the friendly-traceback components?

aroberge commented 3 years ago

Thanks.

The reported version number not being updated is weird: it should be automatically extracted from the code. Here's what I see here: image

However, I think I saw the old version number like you did when I ran it first. Anyway, as long as it works, it should be fine.

Regarding your other comment:

My recommendation would be to do

from friendly.jupyter import Friendly

so that, if ever you needed, you could type in Friendly.why(), etc.

image

I would possibly also import the functions light and dark as they can provide nicer output, depending on the theme used by Jupyter.

Alternatively, if you really aren't interested in anything but the absolute minimum, the following should work.

from friendly import jupyter

image

I really need to document all of this ...

Thanks again for your report and comments, and for confirming that it seems to work correctly for you.

psychemedia commented 3 years ago

Hi, thanks... from friendly import jupyter looks interesting.

Just in passing, not sure if you've seen this PR to IPython that adds the cell run number to error messages: https://github.com/ipython/ipython/pull/13043

aroberge commented 3 years ago

I had not see that. I wonder if it was motivated by a suggestion I made: https://github.com/ipython/ipykernel/issues/716

(also https://github.com/ipython/ipython/issues/12755).

With this change, friendly-traceback loses one of its advantages! ;-) ;-)

psychemedia commented 3 years ago

For teaching novices, I think helpful error messages could be really useful.

Another related example I'm aware of is the pseudo text-from-code generator and narrative error reports used in BlockPy: https://blog.ouseful.info/2016/02/18/blockpy-python-blockly-environment/

aroberge commented 3 years ago

Thanks for the latest link; I'm reopening this issue as a reminder to look at this in more details later.

psychemedia commented 3 years ago

RE: blockpy - this appears to to be the helpful bit: https://pedal-edu.github.io/pedal/

aroberge commented 3 years ago

@psychemedia I have created a new issue, #5, that might be of interest to you. Feel free to add your comments.

aroberge commented 3 years ago

This issue was kept open for reference to some other projects. One of these (pedal) was already linked in an old issue from the previous friendly-traceback [1]. This information properly belongs to friendly-traceback and not friendly (this project); it has now been added as an issue to friendly-traceback [2].

One of the projects mentioned is Pedal. That project had mentioned using Friendly-traceback in one of its own issues [3] which I commented upon.

With the new issue [2] created, there is no need to keep this issue open anymore, since everything has been taken care of.

[1] https://github.com/aroberge/friendly/issues/100

[2] https://github.com/friendly-traceback/friendly-traceback/issues/30

[3] https://github.com/pedal-edu/pedal/issues/79