## module
> Definition of a python files whose names .py. This file is called a module.
>
> Module typically define some code similar content categories, functions, etc., is provided to be used after the introduction of other programs.
#### System Module
> System module is a python script program, provided solely to our own programs. When they are installed python environment that existed when needed can be used to import using the import into the program.
>
> import logging,json,time。。。。
#### custom module
> Is to create yourself a python script to define classes or methods for use after import other scripts.
For example, the following definition of a file My.py
```python
# My.py
# Create a class
class MyException():
pass
#-Defined functions
def func():
print ( 'I is a function module func')
# Define the variable
love = 'iloveyou'
```
In the definition of a python script file can be used as the introduction of the above modules
main.py
```python
# Main.py If you need some time in the current script function has been defined, the corresponding module can be selected, using the introduced
# Use the time module system
import time
print(time.time())
# Use custom exception handling module
import My
# Class module defined using
obj = My.MyException()
print(obj)
Using the function module #
My.func()
# Use variables defined in a module
print(My.love)
When you want to use the content # module, in addition to import module may also be introduced into the specified content in the specified module
introduced from My import love # My love variable module
from My import love as lv # variable import love My modules, from the individual name
print(love)
print (lv)
```
#### test code module
```python
# Custom module, usually only to define classes or functions, variables, etc., do not call
# If you perform in a custom module, you want to write some test code in the current module as the main program,
# As import module does not perform other procedures, the test code can be written in the following code block
if __name__ == '__main__':
print ( 'this position to write code to trigger only when the current script is run directly')
# Special variable __name__
# __Name__ This variable is introduced in the current script as a module other program is __name__ value is the name of the current module
# When the current script is run directly by the python as main parsed, __ name__ value is '__main__'
name = __name__
print(name)
```
## pack
> Packages can be understood as a folder which contains a plurality of files python.
Package structure ###:
```python
'''
package / # package (folder)
Initialization file ├── __init__.py # package
├── a.py # package module
├── b.py
└── ps / # subpackages
├── __init__.py
├── c.py
└── d.py
'''
```
Use package ###
```python
# 1 directly to the package module as introduced, the content may be used as defined in __init__.py
# This usage is not recommended
import package
package.funcpa()
#2. You may import all of the content module
# Note that this content is __all__ this variable is defined by the specified module __init__.py file
# When benefits can be directly import the specified module so, and use, direct use of the specified module name to
from package import *
a.funca ()
b.funcb()
# 3. Import module package designation of designated
from package import a
a.funca ()
# 4. Introducing the designated content from the specified module specified package
from package.b import funcb
funcb ()
# 5. Import module packet from the specified subpackages
from package.ps import c
c.funcc()
# 6. Introducing the specified content from the specified sub-module packet specified packet
from package.ps.d import funcd
funcd()
```
## ways to import classification
### absolute imports
```
# Absolute way to import will use [the search path] to find and import the specified package or module
import module
import package
import package Module
from module import content
import module package from
from the package. content import module
```
### relative imports
** Note: Relative introduced only in the non-main program module, the module does not need to run directly file **
```
# Relative imports
from. package name / import module module name / content
.. package names from / import module module name / content
The representative of the current
.. on behalf of a
```
### search path
> Introducing path when the module or package, the program finds the
```python
'''
The main search path
1. The current program file import module resides
2. python extensions directory in C: / Users / username / AppData / local /.../ Python37 / lib
Other third-party modules positions specified 3. python interpreter / lib / sitepackages
'''
# View the search path package or module in the current script
import sys
print(sys.path)
'''
[
'',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'
]
'''
# You can define your own path, be added to the search path
sys.path.append('/Users/yc/Desktop')
```
Single entry program ##
> Single entry procedure refers to the entire program is run through a main file, other program modules are packaged in a bag or
```python
# Single entry file is the only file as a program is executed directly, the other is as a module or package is introduced into a single inlet to execute
'''
ATM/
| ---- main.py # current program's main entrance file, single entry file, run the file only direct
| ---- package / # main program module package
| ---- | ----- __init__.py # package initialization file
| ---- | ----- View.py # view function module
| ---- | ----- Controller.py # controller module
| ---- | ----- Card.py # bank card module
| ---- | ----- User.py # user module
| ---- databases / # data storage folder
|---- |---- user.txt
|---- |---- user_id_card.txt
main entrance the main program file, as the main program will be run directly, you must use an absolute main.py file import mode
'''
```
Mastery learning method, as will bend to overtake!
Learning to ape: the achievements of their own just a boutique!