Package

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthieu Pichaud

    #1

    Package

    I have a problem organizing my programs in packages and subpackages.

    I use python.2.3.3
    I built a test structure to try to understand how it worked:

    /test
    /test/__init__.py (containing: __all__=['test1'])
    /test/test1/
    /test/test1/__init__.py (containing: __all__=['test2'])
    /test/test1/test2/
    /test/test1/test2/__init__.py (containing: __all__=['test3'])
    /test/test1/test2/test3.py (containing: print 'test3')

    Then I run:[color=blue][color=green][color=darkred]
    >>> from test import *
    >>> test1[/color][/color][/color]
    <module 'test.test1' from 'test/test1/__init__.py'>[color=blue][color=green][color=darkred]
    >>> test2[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'test2' is not defined


    So it seems that I am very limited in the number of subpackages I can
    create.
    Is it normal? Am I silly organizing my programs like that?

    Thanks for your help!

    Matth





  • bruno at modulix

    #2
    Re: Package

    Matthieu Pichaud wrote:[color=blue]
    > I have a problem organizing my programs in packages and subpackages.
    >
    > I use python.2.3.3
    > I built a test structure to try to understand how it worked:
    >
    > /test
    > /test/__init__.py (containing: __all__=['test1'])
    > /test/test1/
    > /test/test1/__init__.py (containing: __all__=['test2'])
    > /test/test1/test2/
    > /test/test1/test2/__init__.py (containing: __all__=['test3'])
    > /test/test1/test2/test3.py (containing: print 'test3')
    >
    > Then I run:[color=green][color=darkred]
    >>>> from test import *
    >>>> test1[/color][/color]
    > <module 'test.test1' from 'test/test1/__init__.py'>[color=green][color=darkred]
    >>>> test2[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > NameError: name 'test2' is not defined[/color]

    [color=blue]
    > So it seems that I am very limited in the number of subpackages I can
    > create.[/color]

    Not at all.
    [color=blue]
    > Is it normal?[/color]

    Yes : when you have nested namespaces, it won't magically become a flat
    namespace. There's a mostly clear documention on this in the official
    Python tutorial.
    [color=blue]
    > Am I silly organizing my programs like that?[/color]

    Dunno - it depends on the program. But still:
    python -c "import this" | grep nested

    HTH
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    Working...