What is the purpose of the __init__.py?
本问题已经有最佳答案,请猛点这里访问。
创建python包时,我被告知创建一个名为init.py的空白文件。我不明白的是为什么我需要创建这个文件。
它向python发出一个信号,表明文件夹是一个包,而不仅仅是一个文件夹。它还包含初始化代码,当包被
有关更多信息,请参阅相关文档。最相关的摘录:
The
__init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such asstring , from unintentionally
hiding valid modules that occur later on the module search path. In
the simplest case,__init__.py can just be an empty file, but it can
also execute initialization code for the package or set the__all__
variable, described later.