English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The role of the __init__.py file is to turn a folder into a Python module, and every module in a package in Python has a __init__.py file.
The __init__.py file is usually empty, but we can also add other functions to it. When we import a package, we are actually importing its __init__.py file. This allows us to batch import the modules we need in the __init__.py file, and no longer need to import them one by one.
# package # __init__.py import re import urllib import sys import os # a.py import package print(package.re, package.urllib, package.sys, package.os)
Note that when accessing the referenced files in the __init__.py file, you need to add the package name.
__init__.py also contains an important variable, __all__, which is used to import all modules.
# __init__.py __all__ = ['os', 'sys', 're', 'urllib'] # a.py from package import *
In this case, the modules and packages registered in the __all__ list of the __init__.py file will be imported into the current file.
It can be understood that __init__.py mainly controls the import behavior of packages. To clearly understand the role of the __init__.py file, it is necessary to have a detailed understanding of the import statement reference mechanism:
The objects that can be imported with an import statement are of the following types:
• module files (.py files)
• C or C++; extensions (compiled as shared libraries or DLL files)
• packages (containing multiple modules)
• built-in modules (written in C and linked to the Python interpreter)
The interpreter searches for import files in the order of directories in the sys.path list when importing modules.
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.zip34\\DLLs34\\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'
Among which the first element of the list is an empty string, representing the current directory.
About .pyc files and .pyo files
The assembly of .py files is only performed when the import statement is executed. When a .py file is imported for the first time, it is assembled into byte code and written to the同名.pyc file. Subsequent import operations will directly execute the .pyc file (when the modification time of the .py file changes, new .pyc files will be generated), and the interpreter uses-When using the O option, the same-named .pyo file will be used, which removes assertions (assert), line numbers, and other debugging information, making the file smaller and faster to run. (Use-OO option, the generated .pyo file will ignore document information)
Import module
Modules are usually separate .py files and can be directly referenced using import. The file types that can be used as module files include .py, .pyo, .pyc, .pyd, .so, .dll
When importing a module, the interpreter performs the following tasks:
1Create a new namespace with the name of the imported module, and you can access the properties and methods of the imported module through this namespace.
2Execute the source code file in the newly created namespace.
3Create an object named the source code file, which refers to the module's namespace, so that you can access the functions and variables in the module through this object.
The import statement can be used at any location in the program, and 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. Subsequent import statements simply create a reference to the module's namespace.
The sys.modules dictionary saves the mapping from the module name to the module object of all imported modules.
Importing a package
Multiple related modules are organized into a package for easy maintenance and use, while also minimizing namespace conflicts. Generally, the structure of a package can be as follows:
package |- subpackage1 |- __init__.py |- a.py |- subpackage2 |- __init__.py |- b.py
There are several ways to import:
import subpackage1.a # Import module subpackage.a into the global namespace, for example, access a attribute with subpackage1.a.attr from subpackage1 import a # Import module a into the global namespace, for example, access a attribute with a.attr_a from subpackage.a import attr_a # Directly import the attribute of module a into the namespace, for example, access a attribute directly with attr_a The from statement can directly import a module into the current namespace. The from statement does not refer to the namespace of the imported object, but directly introduces the imported object into the current namespace.
The above is the detailed explanation of the role of the __init__.py file in Python introduced by the editor for everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Here, I also want to express my heartfelt thanks to everyone for supporting the Nahan Tutorial website!
Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)