__init__.py
The function of the file is to change the folder into a Python modular ,Python In the package of each module in , There are __init__.py file .
Usually __init__.py
The file is empty , But we can add other functions to it . When we import a package , It's actually imported __init__.py
file . So we can __init__.py
Batch import the modules we need in the file , Instead of having to import 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)
Pay attention to the visit here __init__.py
References in documents , Need to add package name .
__init__.py
There's also an important variable in ,__all__
, It is used to import all modules .
# __init__.py
__all__ = ['os', 'sys', 're', 'urllib']
# a.py
from package import *
At this time, you will register in __init__.py
In file __all__
The modules and packages in the list are imported into the current file .
You can see that ,__init__.py
It mainly controls the import behavior of the package . Want to understand clearly __init__.py
Role of documents , We need to know more about import Statement reference mechanism :
Can be import The objects imported by the statement are of the following types :
When importing a module , The interpreter follows sys.path Find the imported files in the order of directories in the list .
''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
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']
among list The first element is an empty string representing the current directory .
About .pyc file And .pyo file
.py Compilation of documents , Only in import When the statement is executed , When .py When the file is first imported , It will be compiled into byte code , And write the bytecode to the with the same name .pyc In file . Later, each import operation will be executed directly .pyc file ( When .py The modification time of the file has changed , This will generate a new .pyc file ), Use... In the interpreter -O Option , A with the same name will be used .pyo file , This file removes assertions (assert)、 Broken line number and other debugging information , A smaller , Run faster .( Use -OO Options , Generated .pyo The file ignores the document information )
The import module
Modules are usually separate .py file , It can be used import Direct reference , The file types that can be used as modules are .py、.pyo、.pyc、.pyd、.so、.dll
When importing the module , The interpreter does the following :
The name of the imported module creates a new namespace , Through this namespace, you can access the properties and methods of the import module .
Execute the source code file in the newly created namespace .
Create an object called a source code file , This object references the namespace of the module , In this way, functions and variables in the module can be accessed through this object
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 . hinder import Statement simply creates a reference to the module namespace .
sys.modules The dictionary holds the mapping from the module name of all imported modules to the module object .
Import package
Multiple associated modules make up a package , To facilitate maintenance and use , At the same time, it can avoid namespace conflicts . Generally speaking , The structure of the package can be like this :
package
|- subpackage1
|- __init__.py
|- a.py
|- subpackage2
|- __init__.py
|- b.py
There are several ways to import :
import subpackage1.a # Put the module subpackage.a Import global namespace , For example, to access a In this paper, we use subpackage1.a.attr
from subpackage1 import a # Put the module a Import global namespace , For example, to access a In this paper, we use a.attr_a
from subpackage.a import attr_a # Put the module a The properties of are imported directly into the namespace , For example, to access a In this paper, we use the attr_a
Use from Statement can directly import the module into the current namespace ,from Statement does not reference the namespace of the imported object , Instead, the imported object is introduced directly into the current namespace .