[SciPy-user] help and lazy-import (?)

Pearu Peterson pearu at scipy.org
Mon Mar 1 10:45:51 CST 2004



On Mon, 1 Mar 2004, Arnd Baecker wrote:

> we have had a further look into this and the status is as follows
> 
> - setting  postpone_import = 0 in (for example)
>   scipy/xplt/info_xplt.py
>   gives
>  >>> import scipy
>  Traceback (most recent call last):
>  [...]
>  from basic import *
>   File "/opt/python/lib/python2.3/site-packages/scipy/special/basic.py",
> line 10, in ?
>     from scipy import factorial
>   ImportError: cannot import name factorial
> 
>   We thought that setting `postpone_import = 0` means that the import
>   of this module is not postponed so that the doc string should be
>   available from the first call to help("scipy.xplt").
>   Is this idea/approach just wrong ?

No, setting `postpone_import = 0` should have worked. I look into it..
Ahh, I see, it is a chicken and egg problem..

> - We found that for
>     >>> import scipy
>     >>> help(scipy.xplt)
>   the correct documentation is visible already on the first
>   invokation of help.
>   (In contrast to
>     >>> import scipy
>     >>> help("scipy.xplt")
>   )
> 
>   Using the source to ppimport.py this is clear as
>   in the first case the module is imported (in class _Helper)
>   before calling the original _pydoc.help.
>   However, for a string  there is no corresponding "ppimport"
>   attribute and it gets passed directly to _pydoc.help

Right.

>   Just to be sure that we understand things correctly:
>   Is it true that the actual import in the second case,
>   i.e. help("scipy.xplt"), is done when __repr__ from the class
>   _ModuleLoader is called via the "normal" pydoc?
>   And by this the doc string is available for the following
>   help("scipy.xplt"), right?
> 
>   To us it seems to boil down to the question if there is
>   a good way to import the corresponding module for the
>   request help("scipy.xplt") in a similar way as it is done
>   for help(scipy.xplt)?
>   (Changing __repr__  to also output the docstring sounds like a
>   horrible hack ..., i.e. there should be a better way)

I have fixed this issue in CVS using different and I think more robust 
approach: `import scipy_base` will redefine help.__class__.__call__ method 
to support ppimport'ed modules. Now `help` works as expected for all 
of the following usage cases:

  help(scipy.linalg)
  help("scipy.linalg")
  exec 'help("scipy.linalg")'
  etc

I have tested this on Python 2.3.3 only though.

Regards,
Pearu



More information about the SciPy-user mailing list