[Numpy-discussion] matplotlib

Fernando Perez Fernando.Perez at colorado.edu
Mon Feb 13 11:06:01 EST 2006

John Hunter wrote:
>>>>>>"Bill" == Bill Baxter <wbaxter at gmail.com> writes:
>     Bill> from numpy import * was the only line missing, called before
>     Bill> the rest.  It seems to work fine if I use from pylab import
>     Bill> * instead of import pylab as g
>     Bill> And actually if I do both in this order: import pylab as g
>     Bill> from pylab import *
>     Bill> Seems as if there's some
>     Bill> initialization code that only gets run with the 'from pylab
>     Bill> import *' version.
> As far as I know that is a python impossibility, unless perhaps you do
> some deep dark magic that is beyond my grasp.  pylab doesn't know how
> it is imported.

Actually, a little creative use of sys._getframe() can tell you that, in some 
instances (if the import was done via pure python and the source can be found 
via inspect, it will fail for extension code and if inspect runs into 
trouble).  If you _really_ want, you can also use dis.dis() on the frame above 
you and analyze the bytecode.

But I seriously doubt matplotlib goes to such unpleasant extremes in this case :)



ps - for the morbidly curious, here's how to do this:

planck[import_tricks]> cat all.py
from trick import *

planck[import_tricks]> cat mod.py
import trick

planck[import_tricks]> cat trick.py
import sys,dis

f = sys._getframe(1)
print f.f_code
print dis.dis(f.f_code)

planck[import_tricks]> python all.py
<code object ? at 0x4006c7a0, file "all.py", line 1>
   1           0 LOAD_CONST               0 (('*',))
               3 IMPORT_NAME              0 (trick)
               6 IMPORT_STAR
               7 LOAD_CONST               1 (None)
              10 RETURN_VALUE

planck[import_tricks]> python mod.py
<code object ? at 0x4006c7a0, file "mod.py", line 1>
   1           0 LOAD_CONST               0 (None)
               3 IMPORT_NAME              0 (trick)
               6 STORE_NAME               0 (trick)
               9 LOAD_CONST               0 (None)
              12 RETURN_VALUE


Since the code object has a file path and line number, you could fetch that 
and look at the source directly instead of dealing with the bytecode.

More information about the NumPy-Discussion mailing list