Skip to content

Raising any error always triggers an AttributeError #11941

Closed

Description

@AgilentGCMS

I've noticed that often when I make a mistake in my python code that would raise an error, that 'raise' in turn raises another error from ipython. E.g., I recently tried placing a legend on a plot in matplotlib with the line
leg = plot_ax.legend(loc='best', framealpha=1.0, fontsize=12, fontfamily='Source Sans Pro')
The error here is that fontfamily is not a valid keyword argument to legend. So it raised an error, as it should. However, that in turn triggered something else:

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-a9612f85a127>", line 1, in <module>
    caf.Comparison_plot('total', freq='monthly')
  File "/Users/sbasu1/Code/OCO2/LMT_Project/check_mass_balance.py", line 122, in Comparison_plot
    leg = plot_ax.legend(loc='best', framealpha=1.0, fontsize=12, fontfamily='Source Sans Pro')
  File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 406, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'fontfamily'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2040, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'TypeError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1101, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 319, in wrapped
    return f(*args, **kwargs)
  File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 353, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 1503, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 1461, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 708, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 754, in getmodule
    os.path.realpath(f)] = module.__name__
AttributeError: module has no attribute '__name__'
---------------------------------------------------------------------------

It's the second error that concerns me, AttributeError: 'TypeError' object has no attribute '_render_traceback_'. It has nothing to do with my faulty code, seems to be an internal ipython problem, and obfuscates the error I should care about (wrong keyword in my code).

What is causing this and how can I fix it? I'm using ipython 7.9.0 on python 3.8.0.

Thanks,
Sourish

Activity

  1. Carreau commented on Nov 12, 2019

    @Carreau
    Member

    That is super strange; can you share a full session that trigger the bug ? Do you have customization ?

    I cannot reproduce on 3.7;

    In [1]: import matplotlib.pyplot as plt
    
    In [2]: fig, ax = plt.subplots()
    
    In [3]: ax.legend(loc='best', framealpha=1.0, fontsize=12, fontfamily='Source Sans Pro')
    No handles with labels found to put in legend.
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-3-2c395eaef425> in <module>
    ----> 1 ax.legend(loc='best', framealpha=1.0, fontsize=12, fontfamily='Source Sans Pro')
    
    ~/miniconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in legend(self, *args, **kwargs)
        404         if len(extra_args):
        405             raise TypeError('legend only accepts two non-keyword arguments')
    --> 406         self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
        407         self.legend_._remove_method = self._remove_legend
        408         return self.legend_
    
    TypeError: __init__() got an unexpected keyword argument 'fontfamily'
    
    In [4]:
    

    And some of the exceptions you see should be caught:

    try:
        # Exception classes can customise their traceback - we
        # use this in IPython.parallel for exceptions occurring
        # in the engines. This should return a list of strings.
        stb = value._render_traceback_()
    except Exception:
        stb = self.InteractiveTB.structured_traceback(etype,
                            value, tb, tb_offset=tb_offset)
    

    I'll try on 3.8 maybe something changed in it ?

  2. AgilentGCMS commented on Nov 13, 2019

    @AgilentGCMS
    Author

    I'll see if I can reproduce it in a few lines. It happens occasionally, which is the worst type of bug :-(

  3. Carreau commented on Nov 13, 2019

    @Carreau
    Member

    Ok, so I can "force" the error:

    In [2]: import matplotlib.pyplot as plt
    
    In [3]: plt.__name__
    Out[3]: 'matplotlib.pyplot'
    
    In [4]: del plt.__name__
    
    In [5]: plt.__name__
    ERROR:root:Internal Python error in the inspect module.
    Below is the traceback from this internal error.
    
    Traceback (most recent call last):
      File "/Users/mbussonnier/dev/ipython/IPython/core/interactiveshell.py", line 3326, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-5-3da4f7a79bee>", line 1, in <module>
        plt.__name__
    AttributeError: module has no attribute '__name__'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/mbussonnier/dev/ipython/IPython/core/interactiveshell.py", line 2040, in showtraceback
        stb = value._render_traceback_()
    AttributeError: 'AttributeError' object has no attribute '_render_traceback_'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/mbussonnier/dev/ipython/IPython/core/ultratb.py", line 1101, in get_records
        return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
      File "/Users/mbussonnier/dev/ipython/IPython/core/ultratb.py", line 319, in wrapped
        return f(*args, **kwargs)
      File "/Users/mbussonnier/dev/ipython/IPython/core/ultratb.py", line 353, in _fixed_getinnerframes
        records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
      File "/Users/mbussonnier/miniconda3/lib/python3.7/inspect.py", line 1502, in getinnerframes
        frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
      File "/Users/mbussonnier/miniconda3/lib/python3.7/inspect.py", line 1460, in getframeinfo
        filename = getsourcefile(frame) or getfile(frame)
      File "/Users/mbussonnier/miniconda3/lib/python3.7/inspect.py", line 696, in getsourcefile
        if getattr(getmodule(object, filename), '__loader__', None) is not None:
      File "/Users/mbussonnier/miniconda3/lib/python3.7/inspect.py", line 742, in getmodule
        os.path.realpath(f)] = module.__name__
    AttributeError: module has no attribute '__name__'
    ---------------------------------------------------------------------------
    
    In [6]:
    

    I"m just curous about what type of module could not have __name__ any change you use multiprocessing ?

  4. AgilentGCMS commented on Nov 13, 2019

    @AgilentGCMS
    Author

    I don't have multiprocessing. The only customization I have is that in .ipython/profile_default/startup/00-load_modules.py I load the following modules:

    from netCDF4 import Dataset
    import numpy as np
    from datetime import datetime, timedelta
    

    In trying to reproduce this second exception, I realized that the same erroneous line of code doesn't always trigger it. E.g., right now one of my scripts triggered this error:

    ERROR:root:Internal Python error in the inspect module.
    Below is the traceback from this internal error.
    
    Traceback (most recent call last):
      File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-7-32adfb935099>", line 1, in <module>
        ana.Plot_signal_noise('nam temp', 'LN', 30)
      File "/Users/sbasu1/Code/OCO2/LMT_Project/analyze_spread.py", line 128, in Plot_signal_noise
        plt.fill_between(bin_midpoints, xco2_av_xmodel-xco2_std_xmodel, xco2_av_xmodel+xco2_std_smodel, ec='b', lw=0.5, fc='#B3B3FF', zorder=0)
    NameError: name 'xco2_std_smodel' is not defined
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2040, in showtraceback
        stb = value._render_traceback_()
    AttributeError: 'NameError' object has no attribute '_render_traceback_'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1101, in get_records
        return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
      File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 319, in wrapped
        return f(*args, **kwargs)
      File "/Users/sbasu1/packages/python3/lib/python3.8/site-packages/IPython/core/ultratb.py", line 353, in _fixed_getinnerframes
        records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
      File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 1503, in getinnerframes
        frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
      File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 1461, in getframeinfo
        filename = getsourcefile(frame) or getfile(frame)
      File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 708, in getsourcefile
        if getattr(getmodule(object, filename), '__loader__', None) is not None:
      File "/Users/sbasu1/packages/python3/lib/python3.8/inspect.py", line 754, in getmodule
        os.path.realpath(f)] = module.__name__
    AttributeError: module has no attribute '__name__'
    ---------------------------------------------------------------------------
    

    The line in /Users/sbasu1/Code/OCO2/LMT_Project/analyze_spread.py that triggered the first error had a typo (my bad), since indeed xco2_std_smodel is not defined within the script, xco2_std_xmodel is. However, if I cut and paste that offending line in a fresh ipython console, where none of the names is defined, I get instead:

    In [1]: import matplotlib.pyplot as plt
    
    In [2]: plt.fill_between(bin_midpoints, xco2_av_xmodel-xco2_std_xmodel, xco2_av_xmodel+xco2_std_smodel, ec='b', lw=0.5, fc='#B3B3FF', zorder=0)
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-2-a5fa00e01dab> in <module>
    ----> 1 plt.fill_between(bin_midpoints, xco2_av_xmodel-xco2_std_xmodel, xco2_av_xmodel+xco2_std_smodel, ec='b', lw=0.5, fc='#B3B3FF', zorder=0)
    
    NameError: name 'bin_midpoints' is not defined
    

    i.e., a standard NameError, not the added exception.

  5. Carreau commented on Nov 16, 2019

    @Carreau
    Member

    If you see the error; can you run the following ?

    import sys
    for k,mod in sys.modules.items():
        if not hasattr(mod, '__name__'):
            print(mod,'(',k,')' , 'has not name')
    

    That might shed light on what actually has no __name__.

  6. AgilentGCMS commented on Nov 17, 2019

    @AgilentGCMS
    Author

    So, the error happened again, again due to a typo. Here's the output from your code block:

    In [3]: import sys
       ...: for k,mod in sys.modules.items():
       ...:     if not hasattr(mod, '__name__'):
       ...:         print(mod,'(',k,')' , 'has not name')
       ...:
    <module '?' from '/Users/sbasu1/Code/OCO2/LMT_Project/analyze_spread.py'> ( __mp_main__ ) has not name
    

    That script analyze_spread.py is the actual script I'm running.

  7. Carreau commented on Nov 22, 2019

    @Carreau
    Member

    Can you try #11959 ?

  8. Carreau commented on Nov 24, 2019

    @Carreau
    Member

    closed by #11959

  9. added this to the 7.10 milestone on Nov 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions