[Tutor] Still Trying to Understand GAE

Kent Johnson kent37 at tds.net
Wed Sep 16 14:16:35 CEST 2009


On Sun, Sep 13, 2009 at 9:59 AM, admin at gg-lab.net <admin at gg-lab.net> wrote:
> Hi All,
>
> i've started earning python sone months ago (on Google App Engine
> unfortunately).
>
> I have some doubts reagrding "import", and have asked a similar
> question here months ago, but without finding a solution.
>
> So:
>
> with import i can import modules or single functions. And this is ok.
> Then: as i have understood from all the books i readm in each package
> directory i have the __init__.py file that decides what import with
> it. In other words if my package skel is like:
>
> /gg/
> /gg/sub1/
> /gg/sub1/file.py
> /gg/sub2/
> /gg/sub2/file.py
>
> and i use "import gg", nothing is imported. To import sub1 and sub2, i can:
>
> - Put in /gg/ a __init__.py file that tells to import them
> - Use "from gg import sub1"
>
> Ok now the $1 Billion question: google app engine has the same schema
> than my "gg" package, an empty __init__.py file, but if i use "import
> google" it also imports all subdirectories. And i can't understand
> wiƬhy it does so.

In general,
  import foo
does not import subpackages of foo unless they are specifically
imported in foo/__init__.py, so dir(foo) will not show the
subpackages.

However if you
  import foo
  import foo.bar
then dir(foo) will include 'bar'. Here is an example from the std lib:

In [1]: import distutils

In [2]: dir(distutils)
Out[2]:
['__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__',
 '__revision__',
 '__version__']

In [3]: import distutils.cmd

In [4]: dir(distutils)
Out[4]:
['__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__',
 '__revision__',
 '__version__',
 'archive_util',
 'cmd',
 'dep_util',
 'dir_util',
 'errors',
 'file_util',
 'log',
 'spawn',
 'util']

My guess is that the startup for GAE is importing the subpackages so
they then appear as imported modules. To access your sub-package, just
import it normally.

Kent


More information about the Tutor mailing list