什么是模块?
类是变量和函数的封装
模块是最高级别的程序组织单元。这句话的意思是,模块什么都能封装
在模块中,咱们不但能够直接存放变量,还能存放函数,还能存放类
定义变量须要用赋值语句,封装函数须要用def语句,封装类须要用class语句,
但封装模块不须要任何语句
每一份单独的Python代码文件(后缀名是**.py**的文件)就是一个单独的模块
封装模块的目的也是为了把程序代码和数据存放起来以便再次利用。
若是封装成类和函数,主要仍是便于本身调用,
但封装了模块,咱们不只能本身使用,文件的方式也很容易共享给其余人使用
使用模块主要有两种方式,一种是本身创建模块并使用,另一种是使用他人共享的模块
本身的模块
在主程序的py文件中,使用import语句导入其余py文件
使用import语句导入一个模块,最主要的目的并非运行模块中的执行语句,
而是为了利用模块中已经封装好的变量、函数、类
试分析代码的输出...
# test.py
a = '我是模块中的变量a'
def hi():
a = '我是函数里的变量a'
print('函数“hi”已经运行!')
class Go1():
a = '我是类1中的变量a'
@classmethod
def do1(cls):
print('函数“do1”已经运行!')
class Go2:
a = '我是类2中的变量a'
def do2(self):
print('函数“do2”已经运行!')
print(a)
hi()
print(Go1.a)
Go1.do1()
A = Go2()
print(A.a)
A.do2()
复制代码
在另一个文件中引入该文件...(分析输出的结果...)
# index.py
import test
print(test.a)
test.hi()
print(test.Go1.a)
test.Go1.do1()
A = test.Go2()
print(A.a)
A.do2()
复制代码
从前有座山...
sentence = '从前有座山,'
def mountain():
print('山里有座庙,')
class Temple:
sentence = '庙里有个老和尚,'
@classmethod
def reading(cls):
print('在讲故事,')
class Story:
sentence = '一个长长的故事。'
def reading(self):
print('讲的什么故事呢?')
for i in range(10):
print(sentence)
mountain()
print(Temple.sentence)
Temple.reading()
A = Story()
print(A.sentence)
A.reading()
print()
复制代码
把代码拆分到两个模块中,执行语句放到main.py文件,封装好的变量、函数和类放到story.py文件
# main.py
import story
for i in range(10):
print(story.sentence)
story.mountain()
print(story.Temple.sentence)
story.Temple.reading()
A = story.Story()
print(A.sentence)
A.reading()
print()
复制代码
# story.py
sentence = '从前有座山,'
def mountain():
print('山里有座庙,')
class Temple:
sentence = '庙里有个老和尚,'
@classmethod
def reading(cls):
print('在讲故事,')
class Story:
sentence = '一个长长的故事。'
def reading(self):
print('讲的什么故事呢?')
复制代码
给模块起别名...
以为import story太长,就能够用import story as s语句,意思是为“story”取个别名为“s”
# main.py
import story as s
for i in range(10):
print(s.sentence)
s.mountain()
print(s.Temple.sentence)
s.Temple.reading()
A = s.Story()
print(A.sentence)
A.reading()
print()
复制代码
当须要同时导入多个模块时,能够用逗号隔开。
好比import a,b,c能够同时导入“a.py,b.py,c.py”三个文件。
若是我只想用模块中的某一个变量, 或者某一个函数呢?
from … import … 语句
**from … import …**语句可让你从模块中导入一个指定的部分到当前模块
# 【文件:test.py】
def hi():
print('函数“hi”已经运行!')
# 【文件:main.py】
from test import hi # 从模块test中导入函数“hi”
hi() # 使用函数“hi”时无需加上“模块.”前缀
复制代码
须要从模块中同时导入多个指定内容,也能够用逗号隔开,写成from xx模块 import a,b,c的形式
from test import a,hi,Go1,Go2
print(a) # 打印变量“a”
hi() # 调用函数“hi”
print(Go1.a) # 打印类属性“a”
Go1.do1() # 调用类方法“Go1”
A = Go2() # 实例化“Go2”类
print(A.a) # 打印实例属性“a”
A.do2() # 调用实例方法“do2”
复制代码
对于from … import …语句要注意的是,没有被写在import后面的内容,将不会被导入
from test import hi # 从模块test中导入函数“hi”
hi()
# 如下语句将会致使报错,由于并无导入test模块,只是导入test模块中的函数“hi”
test.hey()
复制代码
当咱们须要从模块中指定全部内容直接使用时,
能够写成**【from xx模块 import *】**的形式,*表明“模块中全部的变量、函数、类”
from test import *
print(a) # 打印变量“a”
hi() # 调用函数“hi”
print(Go1.a) # 打印类属性“a”
Go1.do1() # 调用类方法“Go1”
A = Go2() # 实例化“Go2”类
print(A.a) # 打印实例属性“a”
A.do2() # 调用实例方法“do2”
复制代码
from xxx import * 和 import xxx 的区别
if __name__ == '__main__'
__name__
是内置变量, 当前文件中, 显示为__main__
若是该py文件, 做为模块, 被其余文件导入, 则
__name__
会变成py的文件名让py文件, 既能自用, 又能他用...
__name__
内置变量在当前文件中, 它的值为
__main__
, 做为模块被其余py文件引入时, 它的值为文件名引入其余模块
import time # 引入的第三方模块
print('第一句话,过两秒出现第二句。')
time.sleep(2)
print('第二句话。')
复制代码
pip的使用
列出全部包 pip list
查看哪些包须要升级pip list --outdated / pip list -o
pip 卸载安装包 pip uninstall 包名
在线安装pip install + 模块名
pip install SomePackage 最新版本
pip install SomePackage==1.0.4 指定版本
pip install 'SomePackage>=1.0.4' 最小版本
模块升级pip install --upgrade 包名
pip 加速
路径:C:\Users\Administrator\pip\pip.ini
内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
复制代码
pip 安装 whl (离线安装)
www.lfd.uci.edu/~gohlke/pyt…
先下载, 再安装
安装包导出pip freeze >requirements.txt
astroid==2.2.5
autopep8==1.4.4
beautifulsoup4==4.7.1
certifi==2019.3.9
chardet==3.0.4
colorama==0.4.1
idna==2.8
isort==4.3.20
lazy-object-proxy==1.4.1
lxml==4.3.3
mccabe==0.6.1
natsort==6.0.0
pies==2.6.7
pprint==0.1
pycodestyle==2.5.0
pylint==2.3.1
PyMySQL==0.9.3
python-docx==0.8.10
reqeusts==1000.0.0
requests==2.22.0
six==1.12.0
soupsieve==1.9.1
typed-ast==1.3.5
urllib3==1.25.3
wrapt==1.11.1
复制代码
安装包导入pip install -r requirements.txt
如何学习一个模块?
查看模块的文件位置
import random
print(random.__file__)
复制代码
当咱们
from random import *
的时候, 咱们引入了什么?
阅读官方文档https://docs.python.org/3.6/library/random.html
或者直接百度搜索
把代码的使用案例作成笔记...
import random # 调用random模块
a = random.random() # 随机从0-1之间抽取一个小数
print(a) # 0.22
a = random.randint(0,100) # 随机从0-100之间抽取一个数字
print(a) # 66
a = random.choice('abcdefg') # 随机从字符串/列表/字典等对象中抽取一个元素(可能会重复)
print(a) # c
a = random.sample('abcdefg', 3) # 随机从字符串/列表/字典等对象中抽取多个不重复的元素
print(a) # ['d', 'a', 'e']
items = [1, 2, 3, 4, 5, 6] # “随机洗牌”,好比打乱列表
random.shuffle(items)
print(items) # [3, 2, 6, 4, 1, 5]
复制代码
可使用**dir()**函数查看一个模块,看看它里面有什么变量、函数、类、类方法
具体搜索某个函数...(seed)
小做业: 自学time和datetime模块, 作成代码笔记, 给你们讲解
猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--做业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小做业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小做业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同窗.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小做业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小做业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小做业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-做业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-做业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-做业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小做业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小做业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-做业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-做业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-做业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-做业-电影top250-做业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-做业.md
猫哥教你写爬虫 041--模拟登陆-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-做业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.mdhtml