首页 > 代码库 > python 目录下的__init__.py

python 目录下的__init__.py

1 一个目录要成为一个package必须有__init__.py文件

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 as string, from unintentionally hiding valid modules that occur later (deeper) 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.

2 __init__.py文件中的内容

2.1 该文件可以是空的

2.2 该文件中也可以定义类和函数

假如目录名是AAA,那么要使用该package中的类和函数,直接import AAA即可。

另外,要用该目录下的module BBB,直接from AAA import BBB即可。

 

python 目录下的__init__.py