简体   繁体   English

如何在python中显示一个包的子包

[英]How to display the subpackages of a package in python

I have some third party library called a and from code examples I learned it has a subpackage b1 , ie,我有一些名为a第三方库,从代码示例中我了解到它有一个子包b1 ,即

from a import b1

Is it possible to see all the subpackages of a ?是否可以查看a所有子包? Package a is not pure python and it is not obvious from the file structure to tell the subpackages.a不是纯python,从文件结构上看不出子包。 I tried dir but it only shows attributes of a我试过dir但它只显示a属性

import a
dir(a)

If the package author provided an explict index of the package modules, then the convention is to define a list named __all__ that contains this index.如果包作者提供了包模块的显式索引,则约定是定义一个名为__all__的列表,其中包含该索引。 So you could do something like the following to see all the submodules / subpackages of an imported package (example prints all json submodules as determined by the package author):因此,您可以执行以下操作来查看导入包的所有子模块/子包(示例打印由包作者确定的所有 json 子模块):

import json

subs = json.__all__
print(subs)

# OUTPUT
# ['dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder']

If the package author did not provide an index of the package modules, then it will be much more difficult to sort it out.如果包作者没有提供包模块的索引,那么整理起来会困难得多。 One approach would be to use dir and then sort or filter the attributes by type in an effort to narrow down to a set likely to be submodules / subpackages.一种方法是使用dir然后按类型对属性进行排序或过滤,以缩小可能是子模块/子包的集合。 Here is an example that might be useful.这是一个可能有用的示例。

import json

def sort_by_type(t):
    return t[0].__name__

attrs = [(type(getattr(json, attr)), attr) for attr in dir(json)]
attrs.sort(key=sort_by_type)
for t, n in attrs:
    print(t, n)

# OUTPUT
# <class 'json.decoder.JSONDecoder'> _default_decoder
# <class 'json.encoder.JSONEncoder'> _default_encoder
# <class '_frozen_importlib.ModuleSpec'> __spec__
# <class '_frozen_importlib_external.SourceFileLoader'> __loader__
# <class 'dict'> __builtins__
# <class 'function'> detect_encoding
# <class 'function'> dump
# <class 'function'> dumps
# <class 'function'> load
# <class 'function'> loads
# <class 'list'> __all__
# <class 'list'> __path__
# <class 'module'> codecs
# <class 'module'> decoder
# <class 'module'> encoder
# <class 'module'> scanner
# <class 'str'> __author__
# <class 'str'> __cached__
# <class 'str'> __doc__
# <class 'str'> __file__
# <class 'str'> __name__
# <class 'str'> __package__
# <class 'str'> __version__
# <class 'type'> JSONDecodeError
# <class 'type'> JSONDecoder
# <class 'type'> JSONEncoder

Use can use __all__ if available to list all the possible items that the python package contains.如果可用,可以使用__all__列出 python 包包含的所有可能项。

Try the following code:试试下面的代码:

for i in sklearn.__all__:   
   try:
      y = eval('sklearn.{}'.format(i))
      for j in y.__all__:
      if 'package you want to find' in j.lower():
         print(i,' - ',j)   
   except:
     pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM