我读到不再需要在最新版本的 python 中添加 __init__.py
文件来将文件夹视为包。然而,python 官方文档并没有这样说 - 例如下面仍然显示了使用 __init__.py
文件的示例和文档。
The
__init__.py
files are required to make Python treat directories containing the file as packages.
https://docs.python.org/3/tutorial/modules.html#packages
我们还需要使用 __init__.py
文件让 python 将文件夹视为包吗?添加/删除此文件有什么优点/缺点吗?
最佳答案
这是真的,但仅适用于 namespace 包。
There are currently three different approaches to creating namespace packages:
- Use native namespace packages. This type of namespace package is defined in PEP 420 and is available in Python 3.3 and later. This is recommended if packages in your namespace only ever need to support Python 3 and installation via pip.
- Use pkgutil-style namespace packages. This is recommended for new packages that need to support Python 2 and 3 and installation via both pip and python setup.py install.
- Use pkg_resources-style namespace packages. This method is recommended if you need compatibility with packages already using this method or if your package needs to be zip-safe.
也许你提到了 native namespace packages .
关于python - 在python 3.6以后,我们还需要使用__init__.py文件来让python把一个文件夹当作一个包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432869/