access to submodules

Discussion in 'Python' started by TG, Jul 19, 2006.

  1. TG

    TG Guest

    hi.

    This is my first try on modules.

    I've got :

    tom/
    __init__.py
    core.py
    ui.py
    data.py

    then, when I'm in my ipython shell :

    ?> from tom import *


    this works, it loads core, ui and data
    but when I do this :


    ?> import tom

    ?> tom.core
    AttributeError: 'module' object has no attribute 'core'


    Well, i guess I missed something, but I don't see what ...
     
    TG, Jul 19, 2006
    #1
    1. Advertising

  2. TG

    TG Guest

    I've just found this :

    If I add :

    "import core, data, ui" inside my "tom/__init__.py" file, it will
    work. But this line does not seems to exist in other files (after
    having a look at several files inside /usr/lib/python2.4).
     
    TG, Jul 19, 2006
    #2
    1. Advertising

  3. >> > from tom import *

    You CAN do this, but it's a bad habit.

    Try:

    >>import tom


    Then call by tom.function()

    rd
     
    BartlebyScrivener, Jul 19, 2006
    #3
  4. TG

    TG Guest

    I know this is a bad habit ... I was just doing it to show what is
    disturbing me.

    Obviously the "star" syntax finds the submodules because they are
    loaded, but when I properly load the module alone with "import tom",
    the "dot" syntax does not find "tom.core".

    BartlebyScrivener wrote:
    > >> > from tom import *

    >
    > You CAN do this, but it's a bad habit.
    >
    > Try:
    >
    > >>import tom

    >
    > Then call by tom.function()
    >
    > rd
     
    TG, Jul 19, 2006
    #4
  5. http://tinyurl.com/6yz2g

    If you do

    from tom import *

    then you no longer need tom, you imported all of his FUNCTIONS (never
    heard of submodule).

    rd
     
    BartlebyScrivener, Jul 19, 2006
    #5
  6. TG

    TG Guest

    BartlebyScrivener wrote:
    > then you no longer need tom, you imported all of his FUNCTIONS (never
    > heard of submodule).


    my mistake, I was using the wrong name

    tom/ <-- package
    __init__.py
    core.py <
    data.py < these are modules contained in tom/
    ui.py <


    if I import tom, it is supposed to load functions defined in
    tom/__init__.py and make all the modules inside accessible through the
    "dot" syntax.

    Therefore, this is supposed to work :

    ?> import tom
    ?> help(tom.core)

    AttributeError: 'module' object has no attribute 'core'

    But if I use the baaaaad star syntax

    ?> from tom import *
    ?> help(core)

    this will work. So that means star loaded functions of __init__.py AND
    modules contained, whereas dot syntax does not give me access to
    modules inside. The issue is not about using or not using the import *
    ....
     
    TG, Jul 19, 2006
    #6
  7. TG

    Peter Otten Guest

    TG wrote:

    > if I import tom, it is supposed to load functions defined in
    > tom/__init__.py and make all the modules inside accessible through the
    > "dot" syntax.
    >
    > Therefore, this is supposed to work :
    >
    > ?> import tom
    > ?> help(tom.core)
    >
    > AttributeError: 'module' object has no attribute 'core'
    >
    > But if I use the baaaaad star syntax
    >
    > ?> from tom import *
    > ?> help(core)
    >
    > this will work.


    No, it won't. Try again with a fresh interpreter (no prior imports)

    >>> from tom import *
    >>> core

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'core' is not defined
    >>> import tom
    >>> tom.core

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'module' object has no attribute 'core'

    Only after an explicit import of tom.core...

    >>> from tom import core
    >>> del core
    >>> from tom import *
    >>> core

    <module 'tom.core' from 'tom/core.py'>

    is core added as an attribute to tom and will therefore be copied into the
    namespace of a module doing a star-import.

    Peter
     
    Peter Otten, Jul 19, 2006
    #7
  8. TG

    TG Guest

    okay,

    so only when I have inside __init__.py

    __all__ = ["core"]

    this works

    ?> from tom import *
    ?> help(core)

    but (in a brand new interpretor)

    ?> import tom
    ?> help(tom.core)

    AttributeError: 'module' object has no attribute 'core'

    got it. But ...

    ?> import numpy
    ?> help(numpy.core)

    this will work, even if core is a subpackage of numpy. and i don't have
    to explicitly import numpy.core.

    I must be missing something ... :-/
     
    TG, Jul 19, 2006
    #8
  9. TG

    Peter Otten Guest

    TG wrote:

    > okay,
    >
    > so only when I have inside __init__.py
    >
    > __all__ = ["core"]
    >
    > this works
    >
    > ?> from tom import *
    > ?> help(core)
    >
    > but (in a brand new interpretor)
    >
    > ?> import tom
    > ?> help(tom.core)
    >
    > AttributeError: 'module' object has no attribute 'core'
    >
    > got it. But ...
    >
    > ?> import numpy
    > ?> help(numpy.core)
    >
    > this will work, even if core is a subpackage of numpy. and i don't have
    > to explicitly import numpy.core.
    >
    > I must be missing something ... :-/


    numpy does the equivalent of

    import core

    in its __init__.py

    """
    Help on method __call__ in module numpy._import_tools:

    __call__(self, *packages, **options) unbound
    numpy._import_tools.PackageLoader method
    Load one or more packages into parent package top-level namespace.

    Usage:

    This function is intended to shorten the need to import many of
    subpackages, say of scipy, constantly with statements such as

    import scipy.linalg, scipy.fftpack, scipy.etc...

    Instead, you can say:

    import scipy
    scipy.pkgload('linalg','fftpack',...)
    """

    Peter
     
    Peter Otten, Jul 19, 2006
    #9
  10. On 19 Jul 2006 08:39:33 -0700, "TG" <> declaimed the
    following in comp.lang.python:

    >
    > I must be missing something ... :-/


    Like... maybe the fact that the numpy __init__.py file has lots of
    executable code to explicitly load the packages?

    from _import_tools import PackageLoader
    pkgload = PackageLoader()
    pkgload('testing','core','lib','linalg','dft','random','f2py',
    verbose=NUMPY_IMPORT_VERBOSE,postpone=False)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Jul 19, 2006
    #10
  11. TG

    TG Guest

    okay, thanks everyone. this is much clearer now.
     
    TG, Jul 20, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Aune

    Python submodules and name imports

    Frank Aune, Aug 23, 2007, in forum: Python
    Replies:
    1
    Views:
    833
    =?iso-8859-1?B?UOFkcmFpZw==?=
    Aug 23, 2007
  2. Donn Ingle
    Replies:
    7
    Views:
    281
    Gabriel Genellina
    Dec 19, 2007
  3. Torsten Mohr

    Noddy with submodules?

    Torsten Mohr, Sep 7, 2009, in forum: Python
    Replies:
    1
    Views:
    317
    Gabriel Genellina
    Sep 8, 2009
  4. Massi

    Automatic import of submodules

    Massi, Nov 25, 2011, in forum: Python
    Replies:
    4
    Views:
    274
    Shambhu Rajak
    Nov 28, 2011
  5. Replies:
    1
    Views:
    128
    Peter Otten
    Jan 11, 2013
Loading...

Share This Page