簡體   English   中英

如何在 Python3 中使用 pyclbr 搜索 __main__ 模塊?

[英]How to search for a __main__ module using pyclbr in Python3?

我想獲取源代碼目錄: /tmp/rebound/rebound的模塊: __main__中的所有函數和類。 當我使用pyclbr.readmodule_ex API 時:

source_code_data = pyclbr.readmodule_ex(source_code_module, path=source_code_path)

我指定它的模塊和它的路徑:

DEBUG:root:Source code module: __main__, Source code path: ['/tmp/rebound/rebound/rebound']

然后我得到這個錯誤:

File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/util.py", line 69, in _find_spec_from_path
    raise ValueError('{}.__spec__ is None'.format(name))
ValueError: __main__.__spec__ is None

然后我嘗試使用不應該被公眾使用的 function: _readmodule

source_code_data = pyclbr._readmodule(source_code_module, source_code_path, )

但我無法決定參數的值應該是多少: inpackage

通過調試器跟蹤代碼后,我發現了一個錯誤:

def _find_spec_from_path(name, path=None):
    """Return the spec for the specified module.

    First, sys.modules is checked to see if the module was already imported. If
    so, then sys.modules[name].__spec__ is returned. If that happens to be
    set to None, then ValueError is raised. If the module is not in
    sys.modules, then sys.meta_path is searched for a suitable spec with the
    value of 'path' given to the finders. None is returned if no spec could
    be found.

    Dotted names do not have their parent packages implicitly imported. You will
    most likely need to explicitly import all parent packages in the proper
    order for a submodule to get the correct spec.

    """
    if name not in sys.modules:
        return _find_spec(name, path)
    else:
        module = sys.modules[name]
        if module is None:
            return None
        try:
            spec = module.__spec__
        except AttributeError:
            raise ValueError('{}.__spec__ is not set'.format(name)) from None
        else:
            if spec is None:
                raise ValueError('{}.__spec__ is None'.format(name))
            return spec

這是模塊中的 function: python3.8/importlib/util.py ,它將__main__評估為內置模塊,因為它落在else塊中。

如何區分要讀取的目標源代碼的__main__和內置的__main__ 換句話說,我如何讀取代碼庫的模塊__main__ :反彈?

長話短說:博士

嘗試:

source_code_data = pyclbr.readmodule_ex("rebound.__main__", path=source_code_path)

解釋

如您所知: _find_spec_from_path將在sys.modules中搜索name ,而__main__始終存在。

如果您檢查sys.modules.keys() ,您會注意到它包含點分隔的模塊名稱。 來自 Ipython shell 的示例:

 'IPython.display',
 'IPython.extensions',
 'IPython.extensions.storemagic',
 'IPython.lib',
 'IPython.lib.backgroundjobs',
 'IPython.lib.clipboard',
 'IPython.lib.display',
 'IPython.lib.pretty',
 'IPython.lib.security',
 'IPython.paths',

而且,如果您意識到自己正在尋找rebound.__main__而不是__main__ ,那就很明顯了。 為了進入if塊, name不能在sys.modules 最后要說的是_find_spec_from_path沒有錯誤。

# python3.8/importlib/util.py
def _find_spec_from_path(name, path=None):
    # ...
    if name not in sys.modules:
        return _find_spec(name, path)
    else:
    #...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM