Module:
Module Benefits: The first module can greatly improve the maintainability of the code, followed by the reduction of a large number of duplicate code, you can use the module to reuse some code. In addition, the module avoids collisions of function names and variable names.
In Python, a. py file is called a module.
Module Categories:
Python standard library (built-in library)
Third-party modules
Application Custom Modules
Import Module
Note: When the module is in import, all the code inside the module is executed and a new namespace is saved. Similar variables, repeated import of the same module, does not repeat the code inside the module.
So the import took two steps:
1. Create a new namespace
2. Execute the called module (the contents of the execution module)
Way:
Import os #第一种导入方式, followed by a module name for each import. import sysimport os,sys #第二种导入方式, import followed by module name, you can separate each module with commas.
The search path for the module:
The Python standard library (built-in library) is preferred when searching for modules---> Application custom Modules---> third-party modules
Use Sys.path to view paths
ImportSYSPrint(sys.path) execution result: D:\Python\Python36-32\python.exe e:/python/test/t.py['e:\\python\\test',' E:\\python ','D:\\python\\python36-32\\python36.zip','D:\\python\\python36-32\\dlls','D:\\python\\python36-32\\lib','d:\\python\\python36-32','c:\\users\\ldsly\\appdata\\roaming\\python\\python36\\site-packages','d:\\python\\python36-32\\lib\\site-packages'] #标红的路径是pycharm自己加上的project路径. This path is not actually present anywhere else. Process finished with exit code 0
To set an alias:
ImportSYS as ABC #使用as to set aliasesPrint(Abc.path) #使用方式跟原模块一样只是名字变了而已. Execution Result: D:\Python\Python36-32\python.exe e:/python/test/t.py['e:\\python\\test','E:\\python','D:\\python\\python36-32\\python36.zip','D:\\python\\python36-32\\dlls','D:\\python\\python36-32\\lib','d:\\python\\python36-32','c:\\users\\ldsly\\appdata\\roaming\\python\\python36\\site-packages','d:\\python\\python36-32\\lib\\site-packages']process finished with exit code 0
The invocation of the module:
Customize the functions and variables within the module NEW:defModule1 ():Print('From Module1') x= 100Note: The new module and the execution file are in the same directory. Execute file Contents:Importnew #正常导入模块new. Module1 () #正常调用 fromNewImportmodule1,x #导入模块的功能, if you want to import all the functions within the module can be directly write a * number (caution) Module1 () #直接执行函数功能 (function will go to its own module to find data, like two bottles of things, will not be related to the current profile data conflicts. Example below)Print(x) execution result: D:\Python\Python36-32\python.exe e:/python/test/t.py fromModule1 fromModule1100Process finished with exit code 0
Variables about the module and the variables that execute the file:
Custom Module content: X= #和执行文件内的变量x同名, different valuesdefModule1 ():Print('From Module1%s', X) executes the contents of the file: x= #变量x在 defined before importing the module fromNewImportModule1,xmodule1 ()Print(x) execution result: D:\Python\Python36-32\python.exe e:/python/test/t.py fromModule1%s 100100 #打印模块内的变量x数值Process finished with exit code 0 executes the file contents: fromNewImportmodule1,xx= #x在导入模块后定义Module1 ()Print(x) execution result: D:\Python\Python36-32\python.exe e:/python/test/t.py fromModule1%s 10099 #显示执行文件内x的数值Process finished with exit code 0
The import method does not have this problem.
Import New
x = 99
New.module1 ()
Execution Result:
D:\Python\Python36-32\python.exe e:/python/test/t.py
From Module1%s 100
Process finished with exit code 0
Package:
The method of organizing a module by directory, called a package. In Python, a package can be understood as a folder, but there must be a __init__ file in this folder.
The __init__ file was also executed when the package was called.
The establishment of the package use: The following to establish a directory, in the directory to establish the __init__.py file, this is a package, and then put the module into the package.
To invoke a module within a package in a program:
day16_ Practice. When the PY is a program entry. fromimport logfromimport userlog.log1 () user.user () execution Result: d:\ Python\python36-32\python.exe e:/python/day-16/day16_ exercise. py from logfrom Userprocess finished with exit code 0
The bin.py in the bin directory is the program entry. ImportSYSImportOsres= Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)) #获取程序根目录, the root directory is the parent directory of the bin directory. #__file__ gets the current file name Sys.path.append (res) #添加根目录路径到sys. Path, in order for import to find the location of the package when importing the module. Print(Sys.path) #打印显示sys. Path fromLoggerImportLog #from Package name Import module name <=== The format of the imported module fromOtherImportuserlog.log1 () #调用模块的功能user. User () Execution result: D:\Python\Python36-32\python.exe e:/python/day-16/bin/bin.py #下方 marked red for the added path ['E:\\python\\day-16\\bin','E:\\python','D:\\python\\python36-32\\python36.zip','D:\\python\\python36-32\\dlls','D:\\python\\python36-32\\lib','d:\\python\\python36-32','c:\\users\\ldsly\\appdata\\roaming\\python\\python36\\site-packages','d:\\python\\python36-32\\lib\\site-packages',' e:\\python\\day-16 '] fromLog fromUserprocess finished with exit code 0
The module that calls packages in the package: There is also a T4 package under logger.
ImportSYSImportOsres= Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)) Sys.path.append (res)#print (Sys.path) fromLoggerImportLog fromLogger.t4ImportT4 #from package name. Package Name Import module name. Call the T4 module inside the T4 package under the logger package fromOtherImportuserlog.log1 () User.user () t4.t4 () #执行t4模块功能执行结果:D: \python\python36-32\python.exe e:/python/day-16/bin/bin.py fromLog fromUser fromT4 #结果Process finished with exit code 0
Python Basics day-16[modules and packages]