[AstroPy] missing modules in ipython notebook

Erik Bray embray at stsci.edu
Mon Aug 11 17:03:08 EDT 2014


On 08/11/2014 02:42 PM, Luke Zoltan Kelley wrote:
> using 'import astropy.io' does let me access 'io' --- but then not 'io.ascii' (for example).
> However, when I use 'import astropy.io' I also get access to 'astropy.table' (which I wasn't before).
>
> Why are modules loading in such a different way from how they do in ipython (or just python)?

Hi,

I'm not sure what you mean by "in IPython".  There's nothing special about the 
way imports work in IPython as opposed to anywhere else.  I think people tend to 
get more confused about it in IPython though due to its autocomplete features.

This is normal for Python packages--by default when do, say, "import astropy" 
Python does not automatically populate the "astropy." namespace with all 
submodules of "astropy" (it may not even know there *are* submodules until and 
unless you explicitly request them, for example, by running "import astropy.io").

However, if the astropy.__init__ module contains code like:

     from . import io
     from . import table
     from . import ... etc ...

*then* the astropy. namespace is populated with the names of those submodules. 
The reason most large packages don't do this (see for example SciPy) is that 
importing all those submodules can add a lot of extra overhead to just running 
"import astropy"--especially if the user only intended to use one or two submodules.

The reason "astropy.table" was populated when you ran "import astropy.io" is 
because something in the IO framework imported the table submodule itself, so 
that part of the astropy namespace became populated.

Erik

> On Aug 11, 2014, at 11:35 AM, JJ Kavelaars <jjk at uvic.ca> wrote:
>
>> try:
>>
>> import astropy.io
>>
>> help(astropy.io)
>>
>> The sub-packages are not loaded when you do import astropy
>>
>> JJ
>>
>> On Aug 11, 2014, at 11:23 AM, Luke Zoltan Kelley <lzkelley at gmail.com> wrote:
>>
>>> I'm using astropy 0.3.2 in an ipython notebook; my ipython is from canopy, version 2.1.0.  I can't access certain modules: astropy.io and astropy.table seem to be completely missing.  They don't appear in tab-completion, and if I try to access them I get something like:
>>>
>>> In [8]: astropy.io
>>> ERROR:astropy:AttributeError: 'module' object has no attribute 'io'
>>> ERROR : AttributeError: 'module' object has no attribute 'io' [IPython.core.interactiveshell]
>>> ---------------------------------------------------------------------------
>>> AttributeError
>>>                            Traceback (most recent call last)
>>> <ipython-input-8-d9b3d4bf20e3> in <module>()
>>> ----> 1 astropy.io
>>> AttributeError : 'module' object has no attribute 'io'
>>>
>>>
>>> Using the same version of astropy in a normal ipython session and the modules work fine.
>>>
>>> Has anyone experienced any similar problems?
>>>
>>> Thanks!
>>> Luke
>>> _______________________________________________
>>> AstroPy mailing list
>>> AstroPy at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/astropy
>>
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy at scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
>




More information about the AstroPy mailing list