python - 在python 3.6以后,我们还需要使用__init__.py文件来让python把一个文件夹当作一个包吗?

标签 python python-3.x

我读到不再需要在最新版本的 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:

  1. 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.
  2. 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.
  3. 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/

相关文章:

python - 组合多个 boolean 值并检查 python 中是否有任何 boolean 值为真

python - 如何创建条件列表?

python | docker | Django | django.core.exceptions.ImproperlyConfigured 错误

python 3.6 gmail api——发送带附件的电子邮件

python - 在 Pandas 数据框中相互获取最近点

python - 如何使用 ftplib 正确下载文件以便为 Windows 添加换行符

python - 重新采样数据时为什么它没有在 csv 文件中连续重新采样

java - 免费(最好是开源)语音到文本库的选项

python-3.x - Python Google Sheets API 通过一次批量更新来更新值和工作表属性

python - 导入错误 : No Module named 'driver' in pyttsx