FW: traceback when import matplotlib.pyplot twice

Hi All,

I am working on a platform that makes it difficult to provide repro cases… but I am hoping someone can shed some light on what I am seeing… Essentially, I have a program imports another script that:
import matplotlib.pyplot as PLT… when it runs more than once then I get the following traceback:

Error in atexit._run_exitfuncs:

Traceback (most recent call last):

File “C:\Python27\lib\atexit.py”, line 24, in _run_exitfuncs

func(*targs, **kargs)

File “C:\Python27\lib\site-packages\matplotlib_pylab_helpers.py”, line 87, in

destroy_all

for manager in list(Gcf.figs.values()):

AttributeError: ‘NoneType’ object has no attribute ‘figs’

Error in sys.exitfunc:

Traceback (most recent call last):

File “C:\Python27\lib\atexit.py”, line 24, in _run_exitfuncs

func(*targs, **kargs)

File “C:\Python27\lib\site-packages\matplotlib_pylab_helpers.py”, line 87, in

destroy_all

for manager in list(Gcf.figs.values()):

AttributeError: ‘NoneType’ object has no attribute ‘figs’

This occurs because in this case the PLT functionality is NOT being used on the main script. If I edit the _pylab _helpers.py file and check to see if Gcf is None… if it is NOT then do the “destroy_all” logic… else it passes [code
change below]… then the error disappears. I can make it disappear for many cases by creating a “dummy” figure so that Gcf gets created and therefore has some garbage to collect.

Any information you can give me would be greatly appreciated. TY,

MJ

image001.png

_pylab_helpers.py (3.61 KB)

Which version of matplotlib and which backend?

Also, don’t use == or != when comparing to None. Use “is” and “is not”.

Ben Root

Thanks for pointer on the None…. Just got the future warning in my code when comparing a numpy array param in a class of mine that defaults to None. That should
be solved differently… but I have plenty of other params that default to None and I will change them accordingly….

Back to the traceback… it occurs on matplotlib 1.3 AND 1.4

TY,

MJ

Something is going really sideways on your system as `Gcf` is the
class that the static method is a member of. If you have the method
to call it, then you have the class defined.

My guess is there is some race condition is the cleanup-at-exit code
where the system is cleaning up the class object before this function
is getting run.

I suspect a better fix is to change all of the staticmethods -> classmethods

Tom

···

On Fri, Oct 10, 2014 at 1:38 PM, Mark Janikas <mjanikas@...3375...> wrote:

Thanks for pointer on the None…. Just got the future warning in my code when
comparing a numpy array param in a class of mine that defaults to None.
That should be solved differently… but I have plenty of other params that
default to None and I will change them accordingly….

Back to the traceback… it occurs on matplotlib 1.3 AND 1.4

TY,

MJ

From: ben.v.root@...287... [mailto:ben.v.root@…287…] On Behalf Of
Benjamin Root
Sent: Wednesday, October 8, 2014 6:20 PM
To: Mark Janikas
Cc: Matplotlib Users
Subject: Re: [Matplotlib-users] FW: traceback when import matplotlib.pyplot
twice

Which version of matplotlib and which backend?

Also, don't use == or != when comparing to None. Use "is" and "is not".

Ben Root

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://p.sf.net/sfu/Zoho
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Thomas Caswell
tcaswell@...287...

+1

···

On 10 October 2014 19:10, Thomas Caswell <tcaswell@...287...> wrote:

I suspect a better fix is to change all of the staticmethods ->
classmethods

See MNT : slight refactoring of Gcf by tacaswell · Pull Request #3638 · matplotlib/matplotlib · GitHub

That might help to make sure that things tear them selves down in the
right order.

Tom

···

On Sat, Oct 11, 2014 at 10:12 AM, Phil Elson <pelson.pub@...287...> wrote:

On 10 October 2014 19:10, Thomas Caswell <tcaswell@...287...> wrote:

I suspect a better fix is to change all of the staticmethods ->
classmethods

+1

--
Thomas Caswell
tcaswell@...287...

When I replaced the file I got the 1st error below. As you had pointed out earlier… this is strange. It only occurs when you run it more than once… strange indeed… it is like a manager is being created and deleted but perhaps
the reference hasn’t… so you get a no-op on the subsequent import… but are then left with a manager that is None….?? Thanks for the tip BTW… I replaced all my None comparisons to “is” and “is not”. I went ahead and added the comparisons to your changes
and it works… but now you need to assure that you do not gc.collect unless you had at least one Non-None manager or else you get the 2nd traceback. The code that works on my end is the last image… not sure if it passes your regression tests…??..??

Thanks so much for all of your help… please let me know if there is anything else I can do to help.

MJ

image001.png

image002.png

image003.png

Hi,

···

On Wed, Oct 15, 2014 at 2:56 PM, Mark Janikas <mjanikas@...3375...> wrote:

When I replaced the file I got the 1st error below. As you had pointed out earlier… this is strange. It only occurs when you run it more than once… strange indeed… it is like a manager is being created and deleted but perhaps the reference hasn’t

Just to echo what Tom said, the error suggests that ``gc.collect()``
is being called after the gc module is most of the way through being
torn down, so that the ``collect`` function has already been
deleted...

Cheers,

Matthew

Thanks for the reiteration/clarification... Do you see any reason why a fail-safe check similar to what I proposed would be ill advised?

MJ

···

-----Original Message-----
From: Matthew Brett [mailto:matthew.brett@…287…]
Sent: Wednesday, October 15, 2014 3:44 PM
To: Mark Janikas
Cc: Thomas Caswell; Phil Elson; Benjamin Root; Matplotlib Users
Subject: Re: [Matplotlib-users] FW: traceback when import matplotlib.pyplot twice

Hi,

On Wed, Oct 15, 2014 at 2:56 PM, Mark Janikas <mjanikas@...3375...> wrote:

When I replaced the file I got the 1st error below. As you had pointed out earlier… this is strange. It only occurs when you run it more than once… strange indeed… it is like a manager is being created and deleted but perhaps the reference hasn’t

Just to echo what Tom said, the error suggests that ``gc.collect()`` is being called after the gc module is most of the way through being torn down, so that the ``collect`` function has already been deleted...

Cheers,

Matthew