table of Contents
Module
What is a 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
.
Four forms module
In Python, a total of four forms of modules:
- Custom modules: If you write a py file, write a bunch of functions in the file, it is called a custom module, namely using .py files written in python
- Third-party modules: has been compiled as a shared library or DLL C or C ++ extensions
- Built-in modules: written in C and link to the python interpreter built-in module
- Package: The module organized into a series of file folders together (Note: there is a __init__.py file folder under the file folder called package)
Why use module
- With a third party or the built-in module is an ism, it can greatly enhance the development efficiency.
- Custom module, the procedures used in our own public function, write a python file, then the component parts of the program can reference the ability to customize the module by way of import.
How module
Generally, we use the import and from ... import ... import modules.
import
# run.py
import spam # from the spam.py
import spam
import import module for the first time three things happened:
- Create a module to module, whichever namespace
- Execution module corresponding file, will perform the namespace name generated in the process are thrown into the module
- Get a module name in the current executable file
Repeat the import module will directly create good results before drinking it, the file will not repeat the module, that is repeated import will happen: spam = spam = memory address space module name
# run.py
import spam as sm
money = 111111
sm.money
sm.read1() # 'spam模块:1000'
sm.read2
sm.change()
print(money) # 1000
Introducing a plurality of modules
import spam, time, os
# 推荐使用下述方式
import spam
import time
import os
from...import...
# run.py
from spam import money
money = 10
print(money) # 10
from ... import ... first import module happened three things:
- Create a module to module, whichever namespace
- Execution module corresponding file, will perform the namespace name generated in the process are thrown into the module
- Get a name in the current namespace executable file, the name of the module directly to a particular name, which means you can not add any prefix directly
- Pros: do not add a prefix code more streamlined
- Disadvantages: easily conflict with the currently executing file namespace name
Import all functions within the file:
# spam.py
__all__ = ['money', 'read1'] # 只允许导入'money'和'read1'
# run.py
from spam import * # 导入spam.py内的所有功能,但会受限制于__all__
Differences and similarities
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 circulation problems
# m1.py
print('from m1.py')
from m2 import x
y = 'm1'
- Creating m2 namespace
- Execution m2.py, the name of the generated execution thrown m2.py
- Get m2.x execution in the current document
# m2.py
print('from m2.py')
from m1 import y
x = 'm2'
- Creating m1 namespace
- Execution m1.py, the name of the generated execution thrown m1.py
- Get m1.y execution in the current document
# run.py
import m1
- Creating m1 namespace
- Execution m1.py, the name of the generated execution thrown m1.py
- M1 get the currently executing file
- If you run run.py, it will error
ImportError: cannot import name 'y'
- If you run m1.py, it will error
ImportError: cannot import name 'x'
- If you run m2.py, it will error
ImportError: cannot import name 'y'
solution
1.
# m1.py
print('from m1.py')
def func1():
from m2 import x
print(x)
y = 'm1'
# m2.py
print('from m2.py')
def func1():
from m1 import y
print(y)
x = 'm2'
2.
# m1.py
print('from m1.py')
y = 'm1'
from m2 import x
# m2.py
print('from m2.py')
x = 'm2'
from m1 import y
Module search path
Module is actually a file, if you want to execute the file, first of all need to find the path to the module (a folder). If the module file path and execute files are not under the same file directory, we need to specify the module's path.
The module search path refers to when importing module needs to retrieve folder.
When import modules lookup module sequence is:
- Start with the memory module has been imported looking
- Built-in module
- Custom Modules
- Environment variable sys.path looking in
import sys
print(f"sys.path: {sys.path}")
'''
['/Users/mac/Desktop/video/python/day16',
'/Users/mac/Desktop/video/python',
'/Applications/anaconda3/lib/python36.zip',
'/Applications/anaconda3/lib/python3.6',
'/Applications/anaconda3/lib/python3.6/lib-dynload',
'/Applications/anaconda3/lib/python3.6/site-packages',
'/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend']
'''
He emphasized: The first value of sys.path is currently executing the file where the folder
random module
import random
# 大于0且小于1之间的小数
print(random.random())
0.42866657593385415
# 大于等于1且小于等于3之间的整数
print(random.randint(1, 3))
3
# 大于等于1且小于3之间的整数
print(random.randrange(1, 3))
2
# 大于1小于3的小数,如1.927109612082716
print(random.uniform(1, 3))
2.1789596280319605
# 列表内的任意一个元素,即1或者‘23’或者[4,5]
print(random.choice([1, '23', [4, 5]]))
[4, 5]
# random.sample([], n),列表元素任意n个元素的组合,示例n=2
print(random.sample([1, '23', [4, 5]], 2))
['23', 1]
lis = [1, 3, 5, 7, 9]
# 打乱l的顺序,相当于"洗牌"
random.shuffle(lis)
print(lis)
[9, 1, 5, 7, 3]