What is the difference between `sys.meta_path` and `sys.path_hooks` importer objects?

前端 未结 2 1469
半阙折子戏
半阙折子戏 2021-02-09 04:21

Using importlib, what is the difference between \"Meta Path Finder\" (found by traversing over sys.meta_path) and Path Entry Finder\" (found by traversing over sys.

2条回答
  •  时光说笑
    2021-02-09 05:02

    sys.path_hooks returns a finder factory.

    Path hooks are called as part of sys.path (or package.__path__ ) processing

    as we read in PEP 302 relevant part which you should read to do what you want.

    Coming to speak of, we use a custom hook in my code but I would not recommend you to copy it verbatim (I am really not sure about the hocus pocus we do with init files)

    However the process is a bit like in there - the find_module method returns self or None depending on what you want to accept as a module and the load_module method proceeds to load that by compiling the code and assigning it an entry into sys.modules. By replacing those parts you can pretty much load whatever you want.

    Related:

    • Package-specific import hooks in Python
    • Python sys.path_hooks Examples

提交回复
热议问题