<small id='1nDpH'></small><noframes id='1nDpH'>

    • <bdo id='1nDpH'></bdo><ul id='1nDpH'></ul>
  • <legend id='1nDpH'><style id='1nDpH'><dir id='1nDpH'><q id='1nDpH'></q></dir></style></legend>
  • <tfoot id='1nDpH'></tfoot>

    1. <i id='1nDpH'><tr id='1nDpH'><dt id='1nDpH'><q id='1nDpH'><span id='1nDpH'><b id='1nDpH'><form id='1nDpH'><ins id='1nDpH'></ins><ul id='1nDpH'></ul><sub id='1nDpH'></sub></form><legend id='1nDpH'></legend><bdo id='1nDpH'><pre id='1nDpH'><center id='1nDpH'></center></pre></bdo></b><th id='1nDpH'></th></span></q></dt></tr></i><div id='1nDpH'><tfoot id='1nDpH'></tfoot><dl id='1nDpH'><fieldset id='1nDpH'></fieldset></dl></div>

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

        时间:2023-09-14

      1. <i id='X8205'><tr id='X8205'><dt id='X8205'><q id='X8205'><span id='X8205'><b id='X8205'><form id='X8205'><ins id='X8205'></ins><ul id='X8205'></ul><sub id='X8205'></sub></form><legend id='X8205'></legend><bdo id='X8205'><pre id='X8205'><center id='X8205'></center></pre></bdo></b><th id='X8205'></th></span></q></dt></tr></i><div id='X8205'><tfoot id='X8205'></tfoot><dl id='X8205'><fieldset id='X8205'></fieldset></dl></div>
      2. <legend id='X8205'><style id='X8205'><dir id='X8205'><q id='X8205'></q></dir></style></legend>

        <small id='X8205'></small><noframes id='X8205'>

        • <bdo id='X8205'></bdo><ul id='X8205'></ul>
              <tbody id='X8205'></tbody>

              <tfoot id='X8205'></tfoot>

                  本文介绍了如何导入在 __init__.py 中定义的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  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
                  

                  这篇关于如何导入在 __init__.py 中定义的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 错误:AttributeError:“模块"对象没有属性 下一篇:点安装.仅创建 dist-info 而不是包

                  相关文章

                  最新文章

                  • <bdo id='CeMHj'></bdo><ul id='CeMHj'></ul>

                  <small id='CeMHj'></small><noframes id='CeMHj'>

                    <i id='CeMHj'><tr id='CeMHj'><dt id='CeMHj'><q id='CeMHj'><span id='CeMHj'><b id='CeMHj'><form id='CeMHj'><ins id='CeMHj'></ins><ul id='CeMHj'></ul><sub id='CeMHj'></sub></form><legend id='CeMHj'></legend><bdo id='CeMHj'><pre id='CeMHj'><center id='CeMHj'></center></pre></bdo></b><th id='CeMHj'></th></span></q></dt></tr></i><div id='CeMHj'><tfoot id='CeMHj'></tfoot><dl id='CeMHj'><fieldset id='CeMHj'></fieldset></dl></div>

                    1. <legend id='CeMHj'><style id='CeMHj'><dir id='CeMHj'><q id='CeMHj'></q></dir></style></legend><tfoot id='CeMHj'></tfoot>