How to import classes defined in __init__.py

前端 未结 7 517
你的背包
你的背包 2020-11-29 17:23

I am trying to organize some modules for my own use. I have something like this:

lib/
  __init__.py
  settings.py
  foo/
    __init__.py
    someobject.py
           


        
相关标签:
7条回答
  • 2020-11-29 17:34

    Edit, since i misunderstood the question:

    Just put the Helper class in __init__.py. Thats perfectly pythonic. It just feels strange coming from languages like Java.

    0 讨论(0)
  • 2020-11-29 17:36

    If lib/__init__.py defines the Helper class then in settings.py you can use:

    from . import Helper
    

    This works because . is the current directory, and acts as a synonym for the lib package from the point of view of the settings module. Note that it is not necessary to export Helper via __all__.

    (Confirmed with python 2.7.10, running on Windows.)

    0 讨论(0)
  • 2020-11-29 17:40
    1. 'lib/'s parent directory must be in sys.path.

    2. Your 'lib/__init__.py' might look like this:

      from . import settings # or just 'import settings' on old Python versions
      class Helper(object):
            pass
      

    Then the following example should work:

    from lib.settings import Values
    from lib import Helper
    

    Answer to the edited version of the question:

    __init__.py defines how your package looks from outside. If you need to use Helper in settings.py then define Helper in a different file e.g., 'lib/helper.py'.

    .
    |   `-- import_submodule.py
        `-- lib
        |-- __init__.py
        |-- foo
        |   |-- __init__.py
        |   `-- someobject.py
        |-- helper.py
        `-- settings.py
    
    2 directories, 6 files
    

    The command:

    $ python import_submodule.py
    

    Output:

    settings
    helper
    Helper in lib.settings
    someobject
    Helper in lib.foo.someobject
    
    # ./import_submodule.py
    import fnmatch, os
    from lib.settings import Values
    from lib import Helper
    
    print
    for root, dirs, files in os.walk('.'):
        for f in fnmatch.filter(files, '*.py'):
            print "# %s/%s" % (os.path.basename(root), f)
            print open(os.path.join(root, f)).read()
            print
    
    
    # lib/helper.py
    print 'helper'
    class Helper(object):
        def __init__(self, module_name):
            print "Helper in", module_name
    
    
    # lib/settings.py
    print "settings"
    import helper
    
    class Values(object):
        pass
    
    helper.Helper(__name__)
    
    
    # lib/__init__.py
    #from __future__ import absolute_import
    import settings, foo.someobject, helper
    
    Helper = helper.Helper
    
    
    # foo/someobject.py
    print "someobject"
    from .. import helper
    
    helper.Helper(__name__)
    
    
    # foo/__init__.py
    import someobject
    
    0 讨论(0)
  • 2020-11-29 17:44

    You just put them in __init__.py.

    So with test/classes.py being:

    class A(object): pass
    class B(object): pass
    

    ... and test/__init__.py being:

    from classes import *
    
    class Helper(object): pass
    

    You can import test and have access to A, B and Helper

    >>> import test
    >>> test.A
    <class 'test.classes.A'>
    >>> test.B
    <class 'test.classes.B'>
    >>> test.Helper
    <class 'test.Helper'>
    
    0 讨论(0)
  • 2020-11-29 17:51

    Maybe this could work:

    import __init__ as lib
    
    0 讨论(0)
  • 2020-11-29 17:57

    Add something like this to lib/__init__.py

    from .helperclass import Helper

    now you can import it directly:

    from lib import Helper

    0 讨论(0)
提交回复
热议问题