Namespace vs regular package

前端 未结 3 1634
灰色年华
灰色年华 2020-12-01 05:40

What\'s the difference between a namespace Python package (no __init__.py) and a regular Python package (has an __init__.py), especially when

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 06:04

    Reading link from Aaron, and PEP420, it appears that the fundamental difference between a namespace package and a regular package, beside the obvious difference that a regular package may contain various initialization code in __init__.py, is that a namespace package is a virtual package whose contents can be distributed in various places along Python's lookup path.

    For example, given

    a/foo/bar.py
    b/foo/baz.py
    

    If both b and a are in Python's path, you can import foo.bar and foo.baz freely.

    Of course, this begs the question that, if __init__.py is not needed, then all other things being equal, is it better to make a regular package or a namespace package, but is a little off-topic.

提交回复
热议问题