nasty import behavior

Use case is I have a module that does digital design. If run as main, it
has a little test that does a plot. The plot is supposed to use a gui and
display.

But if not run as main, there is no plot, and maybe no display.

I had:

if __name__ == "__main__":
  from matplotlib.pyplot import *

but this gives a warning about import * not at module level, so I changed to

from matplotlib.pyplot import *
if __name__ == "__main__":

Then if imported from another module, this crashes my program when run non-
interactively.

I didn't expect merely importing matplotlib to try to open the display. I'm
not changing backends in my rc file, because there are plenty of times when
I want matplotlib to use my favorite gui.

Use case is I have a module that does digital design. If run as main, it
has a little test that does a plot. The plot is supposed to use a gui and
display.

But if not run as main, there is no plot, and maybe no display.

I had:

if __name__ == "__main__":
from matplotlib.pyplot import *

This is the right way.

but this gives a warning about import * not at module level, so I changed to

Not to be overly blunt, but the way to solve this warning is to use:

import matplotlib.pyplot as plt

not move the import and complain about matplotlib's import behavior.

Ryan

···

On Thu, Jul 8, 2010 at 9:53 AM, Neal Becker <ndbecker2@...149...> wrote:

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma