Package-specific import hooks in Python

后端 未结 1 1837
感动是毒
感动是毒 2020-12-13 15:39

I\'m working on creating a Python module that maps API provided by a different language/framework into Python. Ideally, I would like this to be presented as a single root pa

1条回答
  •  时光说笑
    2020-12-13 16:14

    In general, I think your approach looks fine. I wouldn't worry about it being "global", since the whole point is to specify which paths should be handled by you. Moving this test inside the import logic would just needlessly complicate it, so it's left to the implementer of the hook to decide.

    Just one small concern, maybe you could use sys.path_hooks? It appears to be a bit less "powerful" than sys.meta_path

    sys.path_hooks is a list of callables, which will be checked in sequence to determine if they can handle a given path item. The callable is called with one argument, the path item. The callable must raise ImportError if it is unable to handle the path item, and return an importer object if it can handle the path item.

    0 讨论(0)
提交回复
热议问题