Preface
One 、__init__.py File info
Two 、__init__.py Use cases of files
2.1、 No, __init__.py File directory , Use import xx.xx Will you make a mistake ?
2.2、 stay __init__.py Variables or logic defined in the file will be executed at the time of import
2.3、 stay __init__.py Define variables or methods in the file , It can be imported directly
2.4、 stay __init__.py Use... In the document __all__ Method
summary
Prefacestay Python In Engineering , We can often see with “__init__.py” File directory , stay PyCharm in , The directory with this file is called Python The package directory , The display is different from the icon of the directory . As shown in the figure below , dir_example Is an empty directory , The icon is a folder icon , and init_example There are _init__.py file , Its icon is a package .
What is the function of this file , How do we usually use it ?
One 、__init__.py File infoPython Two types of packages are defined , Regular packages and namespace packages . A regular package is a traditional package , Because they exist in Python 3.2 And earlier versions . A regular package is usually implemented as a package that contains __init__.py File directory . When a regular package is imported , This __init__.py The file is implicitly executed , The objects it defines are bound to the package namespace .
This is a Python In the official documents __init__.py Description of the document , In fact, its meaning is very simple , That is to say Python Files are organized as different modules according to directories , This directory is known as Python Package directory ,Python Relevant modules will be imported by searching the files in this directory . But not all directories will be added by search , Only if the directory contains __init__.py When you file , This directory will be Python Think of it as a package directory , Then search and add the files inside . This allows the programmer to control which directories can be used Python Import into package .
When a directory contains __init__.py When you file ,Python Before introducing this module , This file will be executed first . therefore , This file can also be used by programmers to control variables that define the package level .
in summary ,__init__.py The main function of the document is 2 individual :
When the directory contains this file ,Python Will treat it as a package directory , And then you can use import xx.xx To import files or modules in the directory .
Use __init__.py The file can control the variables and contents when the module is imported , Easy for the programmer to control .
Let's look at a few examples .
Two 、__init__.py Use cases of files 2.1、 No, __init__.py File directory , Use import xx.xx Will you make a mistake ?Look at the version , In earlier versions ( Such as 3.2 And the following ), No, __init__.py The directory of the file cannot be used import xx.xx Import related modules , But later versions can .
2.2、 stay __init__.py Variables or logic defined in the file will be executed at the time of importfor example , We have the following directory structure :
init_example/ a_pkg/
__init__.py
a.py
init_test.py
namely init_example There are under the project init_test.py Document and a_pkg Catalog ,a_pkg Directory is __init__.py Documents and a a.py file .
We are a_pkg In the catalog __init__.py File defines :
print("Hey, I am a __init__.py file")
that , If we were init_test.py The contents in are as follows :
from a_pkg import *
perform init_test.py We can get the following results :
Hey, I am a __init__.py file
Because in from a_pkg import * When , Will execute first a_pkg In the catalog __init__.py file .
2.3、 stay __init__.py Define variables or methods in the file , It can be imported directlyOr the directory above , If we were a_pkg In the catalog a.py The definition is as follows :
def a_method(): print("I am a_pkg!")
stay a_pkg In the catalog __init__.py File defines :
from a_pkg.a import a_method
that , We are init_test.py Introduction in a_pkg You can introduce a_method Method :
from init_example.a_pkg import a_method a_method()
Will print the following :
2.4、 stay __init__.py Use... In the document __all__ MethodI am a_pkg!
__all__ The method can be found in __init__.py File defines some packages or variables , Can be used in
from init_example.a_pkg import *
Time definition * The content of expression .
For example, the above content , But we are a_pkg In the catalog __init__.py File defines :
__all__ = ["a"]
that , We are init_test.py The following statement can be used to introduce a modular :
from a_pkg import *a.a_method()
At this time, the following contents will still be printed :
summaryI am a_pkg!
This is about Python In bag __init__.py This is the end of the article on the role and usage of files , More about Python package __init__.py Please search the previous articles of the software development network or continue to browse the relevant articles below. I hope you will support the software development network in the future !