Text of discourse.matplotlib.org › t › fw-traceback-when-import-matplotlib-pyplot-twice › 1… as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout.

Open the live page

FW: traceback when import matplotlib.pyplot twice - matplotlib-users - Matplotlib

FW: traceback when import matplotlib.pyplot twice Community matplotlib-users Mark_Janikas October 8, 2014, 11:16pm 1 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 507×168 3.39 KB _pylab_helpers.py (3.61 KB) Benjamin_Root October 9, 2014, 1:19am 2 Which version of matplotlib and which backend?

Also, don’t use == or != when comparing to None. Use “is” and “is not”. Ben Root Mark_Janikas October 10, 2014, 5:38pm 3 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 tacaswell October 10, 2014, 6:10pm 4 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 < [email protected] ...> 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: [email protected] ... [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 [email protected] matplotlib-users List Signup and Options -- Thomas Caswell [email protected] ...

Phil_Elson1 October 11, 2014, 2:12pm 5 +1 ··· On 10 October 2014 19:10, Thomas Caswell < [email protected] ...> wrote: I suspect a better fix is to change all of the staticmethods -> classmethods tacaswell October 11, 2014, 9:18pm 6 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 < [email protected] ...> wrote: On 10 October 2014 19:10, Thomas Caswell < [email protected] ...> wrote: I suspect a better fix is to change all of the staticmethods -> classmethods +1 -- Thomas Caswell [email protected] ... Mark_Janikas October 15, 2014, 9:56pm 7 When I replaced the file I got the 1 st 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 2 nd 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 664×328 27.7 KB image002.png 745×135 1.73 KB image003.png 448×214 1.92 KB _Matthew_Brett1 October 15, 2014, 10:44pm 8 Hi, ··· On Wed, Oct 15, 2014 at 2:56 PM, Mark Janikas < [email protected] ...> 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 Mark_Janikas October 15, 2014, 10:52pm 9 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 < [email protected] ...> 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