The role of the __init__.py file in Python study notes

1 init folder icon

In PyCharm, __init__.pythe directory with this file is considered a Python package directory, which is displayed differently from the icon of a normal directory.
As shown in the figure below, there is no data directory __init__.py, and its icon is a folder icon; and there is an _init__.py file under the model, and its icon is a package. So __init__.pyit is the identity of the package in Python
insert image description here

2 init.py import package method

__init__.pyOne of the main functions of the file is to turn the folder into a Python module. There are __init__.pyfiles in the package of each module in Python.

When python interprets the package, it will create an index for all the files under this module and put them in the __init__.pyfiles under the current path. If there is no file index, even if the specified package is imported, the interpreter still cannot find the files under this module. If there are no files in a folder __init__.py, it will not be considered a module.

2.1 Single import

# package
# __init__.py
import re
import urllib
import sys
import os
# a.py
import package 
print(package.re, package.urllib, package.sys, package.os)

__init__.pyNote that the reference file in the access file here needs to add the package name.

2.1 Batch import (define __all__ for fuzzy import)

When we import a package in python, we actually import its __init__.py file, so that we can import the modules we need in batches in the __init__.py file instead of importing them one by one.

__init__.pyImportant variables in use __all__,import the module in its entirety.

# __init__.py
__all__ = ['os', 'sys', 're', 'urllib']
# a.py
from package import *

This will import the modules and packages registered in the __all__ list in the __init__.py file into the current file.

3 instances

Suppose the directory structure of our module package is as follows

- mypackage
- - subpackage_1
- - - test11.py
- - - test12.py
- - subpackage_2
- - - test21.py
- - - test22.py
- - subpackage_3
- - - test31.py
- - - test32.py

direct import

If we use the most direct import method, copy the entire file to the project directory, and then import it directly:

from mypackage.subpackage_1 import test11
from mypackage.subpackage_1 import test12
from mypackage.subpackage_2 import test21
from mypackage.subpackage_2 import test22
from mypackage.subpackage_3 import test31
from mypackage.subpackage_3 import test32

In this case, it will be troublesome when searching, and __init__.py will simplify it.

init.py import

Add __init__.py program in mypackage/, there are three ways to write.

from mypackage.subpackage_1 import test11
from mypackage import *
from mypackage.subpackage_1 import *

4 Other frequently asked questions

4.1 Advantages of using __init__.py files

①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 the __init__.py file to control the variables and content when the module is imported, which is convenient for programmers to control. (simplification)

4.2 If there is no directory with __init__.py file, will there be an error when using import xx.xx?

In lower versions (such as 3.2 and below), directories without __init__.py files cannot use import xx.xx to import related modules, but later versions can.

4.3 According to different import methods of imported modules and main programs

(1) If the imported module and the main program are in the same directory , just import it directly

(2) If the imported module is in the subdirectory of the directory where the main program is located , you can add a blank __init__.py file in the subdirectory, which makes the python interpreter treat the entire subdirectory as a module, and then directly It can be imported through "import subdirectory. module".

(3) If the imported module is in the parent directory of the directory where the main program is located , it needs to be solved by modifying the path. There are two methods:
①Through sys.path.append

import sys
sys.path.append('父目录的路径')  # ’需要引用模块的地址'

to change. The python interpreter looks for modules through sys.path. sys.path is a list list, which contains the environment variable paths that have been added to the system.

[Note]: This method is modified at runtime, and the script will become invalid after running.

②Modify the environment variable directly: in windows, it is "set variable='path'" For example: set PYTHONPATH='C:\test...' Check whether the setting is successful, use echo %PYTHONPATH%, and enter the python interpreter to check sys. path, you will find that there is already a newly added path. This method is permanent, and it will always be valid after one setting. In linux, it is "export variable = 'path'", and the view is "echo $variable"

Guess you like

Origin blog.csdn.net/weixin_45928096/article/details/126984304