• <legend id='c6wGt'><style id='c6wGt'><dir id='c6wGt'><q id='c6wGt'></q></dir></style></legend>

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

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

      <tfoot id='c6wGt'></tfoot>

        Python 命名空间“导入 X"和“从 X 导入"

        时间:2023-09-10
          <bdo id='nE5Es'></bdo><ul id='nE5Es'></ul>

              <tbody id='nE5Es'></tbody>

            1. <legend id='nE5Es'><style id='nE5Es'><dir id='nE5Es'><q id='nE5Es'></q></dir></style></legend>
                • <small id='nE5Es'></small><noframes id='nE5Es'>

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

                  本文介绍了Python 命名空间“导入 X"和“从 X 导入"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  以下代码有效:

                  from sqlalchemy.ext.declarative import declarative_base
                  Base = declarative_base()
                  

                  但是,以下内容:

                  import sqlalchemy
                  Base = sqlalchemy.ext.declarative.declarative_base()
                  

                  抛出:模块sqlalchemy"没有属性ext".怎么可能?

                  推荐答案

                  sqlalchemy 是一个包,导入一个包并不会自动加载它的子模块.当你这样做时

                  sqlalchemy is a package, and importing a package doesn't automatically load its submodules. When you do

                  import sqlalchemy
                  Base = sqlalchemy.ext.declarative.declarative_base()
                  

                  导入系统不会加载 sqlalchemy.ext 子模块,因为您没有要求它.

                  The import system doesn't load the sqlalchemy.ext submodule, because you didn't ask for it.

                  当你这样做时

                  from sqlalchemy.ext.declarative import declarative_base
                  Base = declarative_base()
                  

                  您已明确要求加载 sqlalchemy.extsqlalchemy.ext.declarative,因此访问有效.

                  You've explicitly requested that sqlalchemy.ext and sqlalchemy.ext.declarative get loaded, so the access works.

                  你也可以这样做

                  import sqlalchemy.ext.declarative
                  Base = sqlalchemy.ext.declarative.declarative_base()
                  

                  这也将加载 sqlalchemy.extsqlalchemy.ext.declarative.

                  有些包会在 __init__.py 中自动加载其子模块,因此您不必显式导入子模块.不过,这不是您应该依赖的东西.

                  Some packages automatically load their submodules inside their __init__.py, so you don't have to import submodules explicitly. This isn't something you should rely on, though.

                  这篇关于Python 命名空间“导入 X"和“从 X 导入"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:exec() 方法中自定义命名空间的子类化 python 字典 下一篇:python __all__ 模块级变量有什么用?

                  相关文章

                  最新文章

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

                      <legend id='q5Cip'><style id='q5Cip'><dir id='q5Cip'><q id='q5Cip'></q></dir></style></legend><tfoot id='q5Cip'></tfoot>
                        <bdo id='q5Cip'></bdo><ul id='q5Cip'></ul>

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