unifying many packages under one name

Discussion in 'Python' started by Eric S. Johansson, Aug 24, 2007.

  1. I have a collection of packages and I want to put them under single unifying
    name. my goal is to reduce namespace pollution and make all these packages
    accessible as 'import vvv.aaa'. In more detail, if I have packages 'aaa' and
    'bbb', what do I do to put those packages under unifying name such as 'vvv'?
    the only way I can see to do it is to create 'vvv' as a directory with its own
    __init__.py but I'm not sure that were work right either.


    thanks.
    Eric S. Johansson, Aug 24, 2007
    #1
    1. Advertising

  2. Eric S. Johansson

    Peter Otten Guest

    Eric S. Johansson wrote:

    > I have a collection of packages and I want to put them under single
    > unifying
    > name. my goal is to reduce namespace pollution and make all these
    > packages accessible as 'import vvv.aaa'. In more detail, if I have
    > packages 'aaa' and 'bbb', what do I do to put those packages under
    > unifying name such as 'vvv'? the only way I can see to do it is to create
    > 'vvv' as a directory with its own __init__.py but I'm not sure that were
    > work right either.


    Of course you could make a module vvv.py with lots of import statements

    import aaa
    import bbb
    ....
    import zzz

    but then all submodules are imported eagerly with

    import vvv

    And then there's the hack

    import sys

    class Importer(object):
    def __getattr__(self, name):
    module = __import__(name) # you can do anything here
    setattr(self, name, module)
    return module

    sys.modules["vvv"] = Importer()

    Peter
    Peter Otten, Aug 24, 2007
    #2
    1. Advertising

  3. Eric S. Johansson

    James Stroud Guest

    Eric S. Johansson wrote:
    > I have a collection of packages and I want to put them under single
    > unifying name. my goal is to reduce namespace pollution and make all
    > these packages accessible as 'import vvv.aaa'. In more detail, if I have
    > packages 'aaa' and 'bbb', what do I do to put those packages under
    > unifying name such as 'vvv'? the only way I can see to do it is to
    > create 'vvv' as a directory with its own __init__.py but I'm not sure
    > that were work right either.
    >
    >
    > thanks.
    >


    I don't see why it would be a problem:


    py> import os
    py> for afile in os.listdir('pypack'):
    .... if not afile.endswith('pyc'):
    .... print '# %s' % afile
    .... print open(os.path.join('pypack', afile)).read().strip()
    .... print '# end of file\n\n'
    ....
    # __init__.py
    from mod1 import x
    from mod2 import y
    # end of file


    # mod1.py
    x = 14
    # end of file


    # mod2.py
    y = 42
    # end of file


    py> import pypack
    py> dir(pypack)

    ['__builtins__',
    '__doc__',
    '__file__',
    '__name__',
    '__path__',
    'mod1',
    'mod2',
    'x',
    'y']
    py> pypack.mod1
    <module 'pypack.mod1' from 'pypack/mod1.py'>


    James
    James Stroud, Aug 24, 2007
    #3
    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. dee
    Replies:
    2
    Views:
    398
  2. Rim
    Replies:
    1
    Views:
    675
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Jun 30, 2003
  3. Replies:
    3
    Views:
    317
  4. Joel VanderWerf

    unifying hash and proc syntax

    Joel VanderWerf, Mar 22, 2005, in forum: Ruby
    Replies:
    3
    Views:
    103
    Csaba Henk
    Mar 22, 2005
  5. Amir Kadic

    many packages in one file

    Amir Kadic, May 8, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    110
    Malcolm Dew-Jones
    May 9, 2004
Loading...

Share This Page