The functions of the _ init _. py file in Python are described in detail,
The _ init _. py file is used to convert a folder into a Python module. The package of each module in Python contains the _ init _. py file.
The _ init _. py file is usually empty, but other functions can be added for it. When importing a package, we actually imported its _ init _. py file. In this way, we can import the required modules in batches in the _ init _. py file, instead of importing them one by one.
# package# __init__.pyimport reimport urllibimport sysimport os# a.pyimport package print(package.re, package.urllib, package.sys, package.os)
Note that you need to add the package name to access the reference file in the _ init _. py file.
_ Init _. py also has an important variable __all __, which is used to import all modules.
# __init__.py__all__ = ['os', 'sys', 're', 'urllib']# a.pyfrom package import *
In this case, the modules and packages registered in the _ init _. py file _ all _ list are imported into the current file.
We can understand that __init _. py mainly controls the import behavior of packages. To clearly understand the functions of the _ init _. py file, you need to learn more about the import Statement reference mechanism:
Objects that can be imported by the import Statement are of the following types:
• Module file (. py file)
• C or C ++ extensions (compiled as shared library or DLL files)
• Package (including multiple modules)
• Built-in modules (written in C and linked to the Python Interpreter)
When importing the module, the interpreter searches for the import file in the directory order in the sys. path list.
import sys>>> print(sys.path)# Linux:['', '/usr/local/lib/python3.4','/usr/local/lib/python3.4/plat-sunos5','/usr/local/lib/python3.4/lib-tk','/usr/local/lib/python3.4/lib-dynload','/usr/local/lib/python3.4/site-packages']# Windows:['', 'C:\\WINDOWS\\system32\\python34.zip', 'C:\\Documents and Settings\\weizhong', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34\\lib\\plat-win', 'C:\\Python34\\lib\\lib-tk', 'C:\\Python34\\Lib\\site-packages\\pythonwin', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages', 'C:\\Python34\\lib\\site-packages\\win32', 'C:\\Python34\\lib\\site-packages\\win32\\lib', 'C:\\Python34\\lib\\site-packages\\wx-2.6-msw-unicode']
The first empty string of the list element represents the current directory.
About the. pyc file and. pyo File
. The py file assembly is only executed when the import Statement is executed. when a py file is imported for the first time, it is compiled into bytecode and written to the same name. in the pyc file. Later, each import operation will be executed directly. pyc file (when. the modification time of The py file is changed, and a new one is generated. pyc file), when the interpreter uses the-O option, the same name will be used. pyo file, which removes assert, line breaks, and other debugging information. The file is smaller and runs faster. (If the-OO option is used, the generated. pyo file ignores the document information)
Import Module
The module is usually a separate. py file, which can be directly referenced by import. The file types of the module can be. py,. pyo,. pyc,. pyd,. so,. dll.
When importing a module, the interpreter does the following:
1. Create a New namespace with the name of the imported module. With this namespace, you can access the attributes and methods of the imported module.
2. Execute the source code file in the newly created namespace.
3. Create an object named the source code file. This object references the namespace of the module, so that you can access functions and variables in the module through this object.
The import statement can be used anywhere in the program. You can import the same module multiple times in the program, but the code in the module is only executed when the module is imported for the first time. The following import statement simply creates a reference to the module namespace.
The sys. modules dictionary stores the ing between the module names of all imported modules and the module objects.
Import package
Multiple associated modules form a package to facilitate maintenance and use, while avoiding namespace conflicts. Generally, the package structure can be as follows:
package|- subpackage1|- __init__.py|- a.py|- subpackage2|- __init__.py|- b.py
There are several import methods:
Import subpackage1.a # subpackage the module. a imports the global namespace. For example, subpackage1.a is used to access the attributes in. attrfrom subpackage1 import a # import module a to a global namespace. attr_afrom subpackage. a import attr_a # import the attributes of module a to the namespace directly. For example, when accessing the attributes of module a, you can use the from statement to directly import the module to the current namespace, the from statement does not reference the namespace of the imported object, but directly introduces the imported object to the current namespace.
The above section describes _ init _ in Python __. the role of the py file is explained in detail and I hope it will be helpful to you. If you have any questions, please leave a message for me and I will reply to you in a timely manner. Thank you very much for your support for the help House website!