The base module
It is a series of functional module assembly, and a function is a function of the aggregate, and therefore the module can be seen as a function of a pile of aggregate. Py inside a file you can put a bunch of functions, so a py file can be seen as a module. If this file is named py module.py
, the module name is module
.
- From the ordinary version of noodles to function version, in fact, we are doing:
- The package code, a function similar lines 2-40
- Let the different functions of the code to open an independent
- Why use modules?
- It can greatly enhance the development efficiency
- Custom module, a file version in Python, called modules, which can put a plurality of functions, it is then divided into multiple files for each function, a function module having large.
- Module form:
- Custom Modules
- Third-party libraries, so you need to download, such as requests
- Built-in module, directly on the line, such as time
- File splint, called the package in pyton
How module
Generally, we use import and import modules from ... import ...
import与from...import
Module is to make use of them, simplify operations, we do not need to repeat the same functionality development, brought with it
- import time
Open time file
The contents of the time file is read into memory python interpreter, then the name in the file into specific modules time namespace
time.sleep(0)
Using the 'time .sleep (0)' when the method will go inside the module namespace time to find the time
Import using 'time.sleep (0)' it
Advantage: time on the whole got some inside
Disadvantages: relatively large memory footprint, you must be prepared by time out
- from...import...
from time import sleep
- Open time file,
- The contents of the time file is read into memory Python interpreter, and then in the name of the file into a specific time module namespace
- To sleep alone out into the 'import and from ... import.py' namespace went
sleep(0)
- Import using 'sleep (0)' it
Benefits: direct use sleep (0) can be, without prefix, code more streamlined
Disadvantages: can only get sleep, if the file defines sleep parameters, easily conflict with the currently executing file namespace name
- to sum up
Same point:
- Both modules will execute the corresponding file, the two modules will have a namespace
- When both calling function to find the scope of the relationship, and calls need to go to define the position has nothing to do
difference
- import need to add the prefix; from ... import ... no need to add the prefix
Import problem-solving cycle
例如:
我现在有m1和m2两个文件,然后m1需要找到m2的y;m2需要找到m1 的x,但是由于代码至上而下运行,m1中的x还没有生成;m2中的y也没有生成, 所以m1找不到m2的y;m2找不到m1的x,就造成了一个死循环
解决方案一:
导入之前让变量提前生成,但是又发现了一个新问题,会执行两次print print(__name__)
在当前执行文件内打印它为'__main__',在导入时打印它为文件名
解决方案二:
把需要导入的名字y封装到函数体内部。名字的执行顺序:内置--》全局--》局部,调用函数的时候才会用到y,调用函数之前,全局变量x已经生成了,m2能找到x,y就能够生成, m1就能找到y,问题就解决了
The module search path
After the write operation of the project will be divided into files and module files, run files need to find these modules when there is the order of
When import modules lookup module sequence is:
- Start with the memory module has been imported looking
- Built-in module
- Environment variable to find
1.内存中有的: 运行过的
import time
import m1 # 在文件内找到的,保存在内存里了
# 删除了m1
time.sleep(10) # m1还是在内存里的
import m1 # 现在内存里找的
2.内置模块
import time
3.环境变量中(自定义)
import sys
print(sys.path)
- Search path to the executable file prevail
Two uses Python file
- To import the module as module file
- Run as a file to run, run files
Writing good a python file may have two purposes:
- Script, a file is the entire program, to be executed
- Module, stored in a pile of document function is introduced for use