如何导入__init__.py中定义的类

问题:如何导入__init__.py中定义的类

我正在尝试组织一些供我自己使用的模块。我有这样的事情:

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

在中lib/__init__.py,如果要导入lib,我想定义一些要使用的类。但是,如果不将这些类分离到文件中并将其导入中,我似乎无法弄清楚__init__.py

与其说:

    lib/
      __init__.py
      settings.py
      helperclass.py
      foo/
        __init__.py
        someobject.py
      bar/
        __init__.py
        somethingelse.py

from lib.settings import Values
from lib.helperclass import Helper

我想要这样的东西:

    lib/
      __init__.py  #Helper defined in this file
      settings.py
      foo/
        __init__.py
        someobject.py
      bar/
        __init__.py
        somethingelse.py

from lib.settings import Values
from lib import Helper

有可能吗,还是我必须将类分成另一个文件?

编辑

好的,如果我从另一个脚本导入lib,则可以访问Helper类。如何从settings.py访问Helper类?

此处的示例描述了包装内参考。我引用“子模块经常需要互相引用”。就我而言,lib.settings.py需要Helper,而lib.foo.someobject需要访问Helper,那么我应该在哪里定义Helper类?

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
  bar/
    __init__.py
    somethingelse.py

In lib/__init__.py, I want to define some classes to be used if I import lib. However, I can’t seem to figure it out without separating the classes into files, and import them in__init__.py.

Rather than say:

    lib/
      __init__.py
      settings.py
      helperclass.py
      foo/
        __init__.py
        someobject.py
      bar/
        __init__.py
        somethingelse.py

from lib.settings import Values
from lib.helperclass import Helper

I want something like this:

    lib/
      __init__.py  #Helper defined in this file
      settings.py
      foo/
        __init__.py
        someobject.py
      bar/
        __init__.py
        somethingelse.py

from lib.settings import Values
from lib import Helper

Is it possible, or do I have to separate the class into another file?

EDIT

OK, if I import lib from another script, I can access the Helper class. How can I access the Helper class from settings.py?

The example here describes Intra-Package References. I quote “submodules often need to refer to each other”. In my case, the lib.settings.py needs the Helper and lib.foo.someobject need access to Helper, so where should I define the Helper class?


回答 0

  1. lib/的父目录必须是sys.path

  2. 您的“ lib/__init__.py”可能看起来像这样:

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

然后,以下示例应该起作用:

from lib.settings import Values
from lib import Helper

回答问题的编辑版本:

__init__.py定义包从外部的外观。如果需要使用Helperin,settings.pyHelper在另一个文件中定义,例如’ lib/helper.py‘。

。
| `-import_submodule.py
    `-lib
    |-__init__.py
    |-foo
    | |-__init__.py
    | `-someobject.py
    |-helper.py
    `-settings.py

2个目录,6个文件

命令:

$ python import_submodule.py

输出:

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
  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

回答 1

如果lib/__init__.py定义了Helper类,则可以在settings.py中使用:

from . import Helper

这是因为。是当前目录,从设置模块的角度来看,它是lib软件包的同义词。请注意,不必通过导出助手__all__

(已在Windows上运行的python 2.7.10确认。)

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.)


回答 2

您只需将它们放在__init__.py中。

因此,与test / classes.py是:

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

…并且测试/__init__.py为:

from classes import *

class Helper(object): pass

您可以导入测试并可以访问A,B和Helper

>>> import test
>>> test.A
<class 'test.classes.A'>
>>> test.B
<class 'test.classes.B'>
>>> test.Helper
<class 'test.Helper'>

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'>

回答 3

将类似的内容添加到 lib/__init__.py

from .helperclass import Helper

现在您可以直接导入:

from lib import Helper

Add something like this to lib/__init__.py

from .helperclass import Helper

now you can import it directly:

from lib import Helper


回答 4

编辑,因为我误解了这个问题:

只需Helper上课__init__.py。那完全是pythonic。来自Java之类的语言使人感到奇怪。

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.


回答 5

是的,有可能。您可能还想__all____init__.py文件中定义。这是您执行操作时将导入的模块列表

from lib import *

Yes, it is possible. You might also want to define __all__ in __init__.py files. It’s a list of modules that will be imported when you do

from lib import *

回答 6

也许这可以工作:

import __init__ as lib

Maybe this could work:

import __init__ as lib