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

2022-01-13 00:00:00 python packages

问题描述

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

lib/__init__.py设置.py富/__init__.py一些对象.py酒吧/__init__.py别的东西.py

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

而不是说:

 库/__init__.py设置.py助手类.py富/__init__.py一些对象.py酒吧/__init__.py别的东西.py从 lib.settings 导入值从 lib.helperclass 导入助手

我想要这样的东西:

 库/__init__.py #Helper 在这个文件中定义设置.py富/__init__.py一些对象.py酒吧/__init__.py别的东西.py从 lib.settings 导入值从库导入助手

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

编辑

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

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

解决方案

  1. 'lib/ 的父目录必须在 sys.path 中.

  2. 您的lib/__init__.py"可能如下所示:

    来自 .导入设置 # 或者只是在旧 Python 版本上导入设置"类助手(对象):经过

那么下面的例子应该可以工作:

从 lib.settings 导入值从库导入助手

对问题的编辑版本的回答:

__init__.py 定义了你的包从外部看起来如何.如果您需要在 settings.py 中使用 Helper,则在不同的文件中定义 Helper,例如 'lib/helper.py'.

<上一页>.|`-- import_submodule.py`--库|-- __init__.py|-- 富||-- __init__.py|`--someobject.py|-- 助手.py`-- 设置.py2个目录,6个文件

命令:

$ python import_submodule.py

输出:

设置帮手lib.settings 中的助手某物lib.foo.someobject 中的助手# ./import_submodule.py导入 fnmatch,操作系统从 lib.settings 导入值从库导入助手打印对于 os.walk('.') 中的根目录、目录和文件:对于 fnmatch.filter(files, '*.py') 中的 f:print "# %s/%s" % (os.path.basename(root), f)打印打开(os.path.join(root, f)).read()打印# lib/helper.py打印帮手"类助手(对象):def __init__(self, module_name):print "Helper in", module_name# lib/settings.py打印设置"导入助手类值(对象):经过helper.Helper(__name__)# lib/__init__.py#from __future__ 导入 absolute_import导入设置、foo.someobject、助手助手 = helper.Helper# foo/someobject.py打印某物"从 .. 导入助手helper.Helper(__name__)# foo/__init__.py导入某个对象

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?

解决方案

  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

相关文章