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

    1. <tfoot id='0961S'></tfoot>
    2. <legend id='0961S'><style id='0961S'><dir id='0961S'><q id='0961S'></q></dir></style></legend>

      <small id='0961S'></small><noframes id='0961S'>

    3. 列出作为 python 包一部分的所有模块?

      时间:2023-09-14
        <bdo id='AOCu5'></bdo><ul id='AOCu5'></ul>
        <tfoot id='AOCu5'></tfoot>
      • <legend id='AOCu5'><style id='AOCu5'><dir id='AOCu5'><q id='AOCu5'></q></dir></style></legend>

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

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

                <tbody id='AOCu5'></tbody>

              • 本文介绍了列出作为 python 包一部分的所有模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有没有一种简单的方法可以找到所有作为 python 包一部分的模块?我发现 这个旧讨论,其中并不是真的有定论,但在我推出自己的基于 os.listdir() 的解决方案之前,我希望有一个明确的答案.

                Is there a straightforward way to find all the modules that are part of a python package? I've found this old discussion, which is not really conclusive, but I'd love to have a definite answer before I roll out my own solution based on os.listdir().

                推荐答案

                是的,你想要基于 pkgutil 或类似的东西——这样你就可以同等对待所有的包,不管它们是否在鸡蛋里或 zips 左右(os.listdir 无济于事).

                Yes, you want something based on pkgutil or similar -- this way you can treat all packages alike regardless if they are in eggs or zips or so (where os.listdir won't help).

                import pkgutil
                
                # this is the package we are inspecting -- for example 'email' from stdlib
                import email
                
                package = email
                for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
                    print "Found submodule %s (is a package: %s)" % (modname, ispkg)
                

                如何也导入它们?你可以像往常一样使用 __import__:

                How to import them too? You can just use __import__ as normal:

                import pkgutil
                
                # this is the package we are inspecting -- for example 'email' from stdlib
                import email
                
                package = email
                prefix = package.__name__ + "."
                for importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix):
                    print "Found submodule %s (is a package: %s)" % (modname, ispkg)
                    module = __import__(modname, fromlist="dummy")
                    print "Imported", module
                

                这篇关于列出作为 python 包一部分的所有模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python 模块的绝对与显式相对导入 下一篇:如何将静态文件包含到 setuptools - python 包

                相关文章

                最新文章

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

                  <tfoot id='b3Rxi'></tfoot>
                1. <small id='b3Rxi'></small><noframes id='b3Rxi'>

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