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

        <tfoot id='yp60L'></tfoot>

        何时或为什么在 Python 中使用相对导入

        时间:2023-09-14

      3. <tfoot id='y6b4O'></tfoot>

                <tbody id='y6b4O'></tbody>

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

              1. <legend id='y6b4O'><style id='y6b4O'><dir id='y6b4O'><q id='y6b4O'></q></dir></style></legend>
                  <bdo id='y6b4O'></bdo><ul id='y6b4O'></ul>
                • <i id='y6b4O'><tr id='y6b4O'><dt id='y6b4O'><q id='y6b4O'><span id='y6b4O'><b id='y6b4O'><form id='y6b4O'><ins id='y6b4O'></ins><ul id='y6b4O'></ul><sub id='y6b4O'></sub></form><legend id='y6b4O'></legend><bdo id='y6b4O'><pre id='y6b4O'><center id='y6b4O'></center></pre></bdo></b><th id='y6b4O'></th></span></q></dt></tr></i><div id='y6b4O'><tfoot id='y6b4O'></tfoot><dl id='y6b4O'><fieldset id='y6b4O'></fieldset></dl></div>
                  本文介绍了何时或为什么在 Python 中使用相对导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否有任何关于何时在 Python 中使用相对导入的规则或指南?我一直在使用它们,就像在 Flask Web 框架中一样.在搜索这个主题时,我只看到有关如何使用相对导入的文章,而没有看到为什么.

                  Is there any rules or guidelines concerning when to use relative imports in Python? I see them in use all the time like in the Flask web framework. When searching for this topic, I only see articles on how to use relative imports, but not why.

                  那么使用有什么特别的好处:

                  So is there some special benefit to using:

                  from . import x
                  

                  而不是:

                  from package import x
                  

                  此外,我注意到相关的 SO 帖子 提到不鼓励相对导入.然而人们仍在继续使用它们.

                  Moreover, I noticed that a related SO post mentions that relative imports are discouraged. Yet people still continue to use them.

                  推荐答案

                  查看 PEP 328 关于相对进口的部分

                  理由似乎是这样写的:

                  展示了几个用例,其中最重要的是能够重新排列大型包的结构,而无需编辑子包.此外,包内的模块在没有相对导入的情况下无法轻松导入自身.

                  Several use cases were presented, the most important of which is being able to rearrange the structure of large packages without having to edit sub-packages. In addition, a module inside a package can't easily import itself without relative imports.

                  这篇关于何时或为什么在 Python 中使用相对导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 文件 - 相互导入 下一篇:无法在 Windows 上安装文本

                  相关文章

                  最新文章

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

                    <bdo id='XQsrI'></bdo><ul id='XQsrI'></ul>
                • <legend id='XQsrI'><style id='XQsrI'><dir id='XQsrI'><q id='XQsrI'></q></dir></style></legend>

                    1. <small id='XQsrI'></small><noframes id='XQsrI'>