FAQ
hi,

i have written a small project for myself all in seperate classes and
each of the classes lives in a seperate file. now i am looking for an
import structure something like import wx, and then have access to all
my classes just like wx.Button or wx.BoxSizer etc.

as of now i have a __init__.py file in the directory with:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

but i still have to import each class by it's own. im really looking
for something like import wx
and then get all my access right away under this new namespace.

thank you in advance

Search Discussions

  • Paul McGuire at Apr 30, 2007 at 3:00 pm

    On Apr 30, 9:56 am, spohle wrote:
    hi,

    i have written a small project for myself all in seperate classes and
    each of the classes lives in a seperate file. now i am looking for an
    import structure something like import wx, and then have access to all
    my classes just like wx.Button or wx.BoxSizer etc.

    as of now i have a __init__.py file in the directory with:
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)

    but i still have to import each class by it's own. im really looking
    for something like import wx
    and then get all my access right away under this new namespace.

    thank you in advance
    If it really is a small project, consider just putting all the classes
    into a single module, say spohlePkg.py. Then your users would import
    this module using "import spohlePkg", and would access the classes as
    "spohlePkg.ClassA", "spohlePkg.ClassB", etc.

    -- Paul
  • Spohle at Apr 30, 2007 at 3:02 pm

    On Apr 30, 8:00 am, Paul McGuire wrote:
    On Apr 30, 9:56 am, spohle wrote:


    hi,
    i have written a small project for myself all in seperate classes and
    each of the classes lives in a seperate file. now i am looking for an
    import structure something like import wx, and then have access to all
    my classes just like wx.Button or wx.BoxSizer etc.
    as of now i have a __init__.py file in the directory with:
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)
    but i still have to import each class by it's own. im really looking
    for something like import wx
    and then get all my access right away under this new namespace.
    thank you in advance
    If it really is a small project, consider just putting all the classes
    into a single module, say spohlePkg.py. Then your users would import
    this module using "import spohlePkg", and would access the classes as
    "spohlePkg.ClassA", "spohlePkg.ClassB", etc.

    -- Paul
    yeah i had that, but my classes grew really fast and i decided to
    split them up. but you're right that in one file that would solve my
    problem. still hoping to find a way for the seperate files.
  • Hamilton, William at Apr 30, 2007 at 3:16 pm

    -----Original Message-----
    From: python-list-bounces+whamil1=entergy.com at python.org
    [mailto:python-
    list-bounces+whamil1=entergy.com at python.org] On Behalf Of spohle
    Sent: Monday, April 30, 2007 10:03 AM
    To: python-list at python.org
    Subject: Re: import structures
    On Apr 30, 8:00 am, Paul McGuire wrote:
    On Apr 30, 9:56 am, spohle wrote:


    hi,
    i have written a small project for myself all in seperate classes
    and
    each of the classes lives in a seperate file. now i am looking for
    an
    import structure something like import wx, and then have access to
    all
    my classes just like wx.Button or wx.BoxSizer etc.
    as of now i have a __init__.py file in the directory with:
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)
    but i still have to import each class by it's own. im really
    looking
    for something like import wx
    and then get all my access right away under this new namespace.
    thank you in advance
    If it really is a small project, consider just putting all the
    classes
    into a single module, say spohlePkg.py. Then your users would
    import
    this module using "import spohlePkg", and would access the classes
    as
    "spohlePkg.ClassA", "spohlePkg.ClassB", etc.

    -- Paul
    yeah i had that, but my classes grew really fast and i decided to
    split them up. but you're right that in one file that would solve my
    problem. still hoping to find a way for the seperate files.
    If you've got modules a, b, and c, you can create a wrapper module d
    that imports from each of those.

    from a import *
    from b import *
    from c import *


    Then, import d and use it as the module name. So if a had a SomeThing
    class, you could do this:

    import d
    x = d.SomeThing()



    ---
    -Bill Hamilton
  • Spohle at Apr 30, 2007 at 3:24 pm

    On Apr 30, 8:16 am, "Hamilton, William " wrote:
    -----Original Message-----
    From: python-list-bounces+whamil1=entergy.... at python.org
    [mailto:python-
    list-bounces+whamil1=entergy.... at python.org] On Behalf Of spohle
    Sent: Monday, April 30, 2007 10:03 AM
    To: python-l... at python.org
    Subject: Re: import structures
    On Apr 30, 8:00 am, Paul McGuire wrote:
    On Apr 30, 9:56 am, spohle wrote:

    hi,
    i have written a small project for myself all in seperate classes
    and
    each of the classes lives in a seperate file. now i am looking for
    an
    import structure something like import wx, and then have access to
    all
    my classes just like wx.Button or wx.BoxSizer etc.
    as of now i have a __init__.py file in the directory with:
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)
    but i still have to import each class by it's own. im really
    looking
    for something like import wx
    and then get all my access right away under this new namespace.
    thank you in advance
    If it really is a small project, consider just putting all the
    classes
    into a single module, say spohlePkg.py. Then your users would
    import
    this module using "import spohlePkg", and would access the classes
    as
    "spohlePkg.ClassA", "spohlePkg.ClassB", etc.
    -- Paul
    yeah i had that, but my classes grew really fast and i decided to
    split them up. but you're right that in one file that would solve my
    problem. still hoping to find a way for the seperate files.
    If you've got modules a, b, and c, you can create a wrapper module d
    that imports from each of those.

    from a import *
    from b import *
    from c import *

    Then, import d and use it as the module name. So if a had a SomeThing
    class, you could do this:

    import d
    x = d.SomeThing()

    ---
    -Bill Hamilton

    that doesn't seem to work for me. the from a import * will only give
    me a not d.a
  • Hamilton, William at Apr 30, 2007 at 3:41 pm

    -----Original Message-----
    From: python-list-bounces+whamil1=entergy.com at python.org
    [mailto:python-
    list-bounces+whamil1=entergy.com at python.org] On Behalf Of spohle
    Sent: Monday, April 30, 2007 10:25 AM
    To: python-list at python.org
    Subject: Re: import structures
    On Apr 30, 8:16 am, "Hamilton, William " wrote:


    If you've got modules a, b, and c, you can create a wrapper module d
    that imports from each of those.

    from a import *
    from b import *
    from c import *

    Then, import d and use it as the module name. So if a had a
    SomeThing
    class, you could do this:

    import d
    x = d.SomeThing()

    ---
    -Bill Hamilton

    that doesn't seem to work for me. the from a import * will only give
    me a not d.a

    "from blah import *" puts everything in blah into the current module's
    namespace (or so I understand it). This is different from "import
    blah": with the latter, you have to use "x = blah.SomeThing()". With
    the former, you can simply say "x = SomeThing()".

    So, if a has a class SomeThing, and you import it into d using "from a
    import *", in d you can use SomeThing's methods directly. If you then
    use "import d" in your main script, you can create a SomeThing instance
    with
    "x = d.SomeThing()".

    ---
    -Bill Hamilton
  • Duncan Booth at Apr 30, 2007 at 3:35 pm

    spohle wrote:

    as of now i have a __init__.py file in the directory with:
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)

    but i still have to import each class by it's own. im really looking
    for something like import wx
    and then get all my access right away under this new namespace.
    You just need to make your __init__.py import anything you want to be
    available directly under the package.

    So if the folder pkg contains __init__.py, class1.py, class2.py all you
    have to do is make pkg/__init__.py contain something like:

    from class1 import Class1
    from class2 import Class2

    And then when using the package you can do:

    from pkg import Class1, Class2

    or

    import pkg
    ...
    something = pkg.Class1()

    as you prefer.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 30, '07 at 2:56p
activeApr 30, '07 at 3:41p
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase