The concept of modules in Python

"" " 
Module search order
sys.path View search order
" ""

"" "
programming languages, libraries, package, module is the same concept, is the code organization

package module Python source code files
package package modules grouped together package name and directory of the same name and its related files

improt module name to find the specified module is loaded and initialized it generates an object module
increases the object name and the associated step created in the local namespace scope where the import of
import top-level module its name added to the local term space and bind to its target module

"" "
Import os.path AS OSP
Import functools
Print (dir ())
Print (functools)
Print (functools.wraps)

from pathlib Import Path
Print (Path, the above mentioned id ( the Path))

Import pathlib AS PL
Print (the dir ())
Print (PL)
Print (pl.Path, ID (pl.Path))
Print ( "T2") 

Import test1

B = test1.A ()
b.show ()
Print (test1.A .__ dict__ magic)
"" "
All of the modules are loaded in the recording sys.modules, the storage of all the modules already mounted the dictionary


module runs
__name__, each module will define a __name__ special variable to store the name of the current module, if not specified, the
default file noun source code, if the package is limited naming

interpreter initialization time, will initialization sys.modules dictionary (save the module already loaded), create builtins (global functions
, constants) module __main__ module sys module has the module search path sys.path
Python scripting language


"" "
Print (__ name__)

" ""
module attribute
__file__ string, the source file path
__cached__ string, compiled bytecode file path
__spec__ the display specification module
__name__ module name
__package__ package when the module is the same __name__; otherwise, the module can be set to an empty string top
"" "
# Import m.x
# print(dir(m))
# print(type(m))
# print(m.x)
Print # (My)
# Print (m .__ cached__)
"" "m also file can be imported
absolute import congsys.path import * does not import underlined" ""

Guess you like

Origin www.cnblogs.com/ergePython/p/11455582.html