Python 模块进阶
import导入模块
1. import 搜索路径
import sys
sys.path
例子:
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'E:\\Python\\Scripts\\ipython.exe',
'e:\\python\\python36.zip',
'e:\\python\\DLLs',
'e:\\python\\lib',
'e:\\python',
'e:\\python\\lib\\site-packages',
'e:\\python\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\ylg\\.ipython']
路径搜索
- 从上面列出的目录里依次查找要导入的模块文件
- ' ' 表示当前路径
程序执行时导入模块路径
sys.path.append('C:\\Users\\ylg')
sys.path.insert(0, 'C:\\Users\\ylg') #可以确保先搜索这个路径
sys.path.remove('C:\\Users\\ylg') # 移除路径
例子:
In [3]: sys.path.append('C:\\Users\\ylg')
In [4]: sys.path
Out[4]:
['',
'E:\\Python\\Scripts\\ipython.exe',
'e:\\python\\python36.zip',
'e:\\python\\DLLs',
'e:\\python\\lib',
'e:\\python',
'e:\\python\\lib\\site-packages',
'e:\\python\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\ylg\\.ipython',
'C:\\Users\\ylg']
In [5]: sys.path.remove('C:\\Users\\ylg')
In [6]: sys.path
Out[6]:
['',
'E:\\Python\\Scripts\\ipython.exe',
'e:\\python\\python36.zip',
'e:\\python\\DLLs',
'e:\\python\\lib',
'e:\\python',
'e:\\python\\lib\\site-packages',
'e:\\python\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\ylg\\.ipython']
2. 重新导入模块
模块被导入后,修改代码后import module不能重新导入模块,重新导入需用使用其他方法。
- 方法一:重新启动 python shell
- 方法二:使用
reload()模块- 在python 2.x中,
reload()是内置函数 - 在python 3.0-3.3中,可以使用
imp.reload(module) - 在python 3.4中,imp已经废弃,取而代之是
importlib
- 在python 2.x中,
例子:
>>> import importlib
>>> import hello
HeLLO
>>> importlib.reload(hello)
Hello!
常用模块库
Python有一套很有用的标准库(standard library)。标准库会随着Python解释器,一起安装在你的电脑中的。 它是Python的一个组成部分。这些标准库是Python为你准备好的利器,可以让编程事半功倍。
常用标准库
| 标准库 | 说明 |
|---|---|
| builtins | 内建函数默认加载 |
| os | 操作系统接口 |
| sys | Python自身的运行环境 |
| functools | 常用的工具 |
| json | 编码和解码 JSON 对象 |
| logging | 记录日志,调试 |
| multiprocessing | 多进程 |
| threading | 多线程 |
| copy | 拷贝 |
| time | 时间 |
| datetime | 日期和时间 |
| calendar | 日历 |
| hashlib | 加密算法 |
| random | 生成随机数 |
| re | 字符串正则匹配 |
| socket | 标准的 BSD Sockets API |
| shutil | 文件和目录管理 |
| glob | 基于文件通配符搜索 |
hashlib
import hashlib
m = hashlib.md5() #创建hash对象,md5:(message-Digest Algorithm 5)消息摘要算法,得出一个128位的密文
print m #<md5 HASH object>
m.update('itcast') #更新哈希对象以字符串参数
print m.hexdigest() #返回十六进制数字字符串
应用实例
用于注册、登录....
import hashlib
import datetime
KEY_VALUE = 'Itcast'
now = datetime.datetime.now()
m = hashlib.md5()
str = '%s%s' % (KEY_VALUE,now.strftime("%Y%m%d"))
m.update(str.encode('utf-8'))
value = m.hexdigest()
print(value)
运行结果:
8ad2d682e3529dac50e586fee8dc05c0
更多标准库
http://python.usyiyi.cn/translate/python_352/library/index.html
常用扩展库
| 扩展库 | 说明 |
|---|---|
| requests | 使用的是 urllib3,继承了urllib2的所有特性 |
| urllib | 基于http的高层库 |
| scrapy | 爬虫 |
| beautifulsoup4 | HTML/XML的解析器 |
| celery | 分布式任务调度模块 |
| redis | 缓存 |
| Pillow(PIL) | 图像处理 |
| xlsxwriter | 仅写excle功能,支持xlsx |
| xlwt | 仅写excle功能,支持xls ,2013或更早版office |
| xlrd | 仅读excle功能 |
| elasticsearch | 全文搜索引擎 |
| pymysql | 数据库连接库 |
| mongoengine/pymongo | mongodbpython接口 |
| matplotlib | 画图 |
| numpy/scipy | 科学计算 |
| django/tornado/flask | web框架 |
| xmltodict | xml 转 dict |
| SimpleHTTPServer | 简单地HTTP Server,不使用Web框架 |
| gevent | 基于协程的Python网络库 |
| fabric | 系统管理 |
| pandas | 数据处理库 |
| scikit-learn | 机器学习库 |
库的安装
使用pip工具就可以安装,pip是自带的。
在命令窗口输入pip就可看到许多关于pip的命令。
安装命令是:pip install 扩展库名
C:\Users\ylg>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring
environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be
used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be
used up to 3 times (corresponding to WARNING,
ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form
[user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should
attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname> Mark this host as trusted, even though it does
not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file
containing the private key and the certificate
in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine
whether a new version of pip is available for
download. Implied with --no-index.
Python 模块进阶的更多相关文章
- Python模块(进阶3)
转载请标明出处: http://www.cnblogs.com/why168888/p/6411917.html 本文出自:[Edwin博客园] Python模块(进阶3) 1. python中模块和 ...
- Python模块进阶、标准库、扩展库
模块进阶 Python有一套很有用的标准库(standard library).标准库会随着Python解释器,一起安装在你的电脑中的. 它是Python的一个组成部分.这些标准库是Python为你准 ...
- Python爬虫进阶五之多线程的用法
前言 我们之前写的爬虫都是单个线程的?这怎么够?一旦一个地方卡到不动了,那不就永远等待下去了?为此我们可以使用多线程或者多进程来处理. 首先声明一点! 多线程和多进程是不一样的!一个是 thread ...
- Python爬虫进阶一之爬虫框架概述
综述 爬虫入门之后,我们有两条路可以走. 一个是继续深入学习,以及关于设计模式的一些知识,强化Python相关知识,自己动手造轮子,继续为自己的爬虫增加分布式,多线程等功能扩展.另一条路便是学习一些优 ...
- python模块介绍-locustio:性能测试工具locustio
转自:http://automationtesting.sinaapp.com/blog/m_locustio_doc python测试文章 http://weibo.com/cizhenshi?is ...
- 年薪20万Python工程师进阶(7):Python资源大全,让你相见恨晚的Python库
我是 环境管理 管理 Python 版本和环境的工具 pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. virtualenv – 创建独立 Python 环 ...
- Python面向对象进阶(二)
Python面向对象进阶2.html :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1 ...
- Python 简明教程 --- 17,Python 模块与包
微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 正确的判断来源于经验,然而经验来源于错误的判断. -- Fred Brooks 目录 我们已经知道函 ...
- 学习python须知,Python基础进阶需掌握哪些知识点?
Python基础进阶需要掌握哪些知识点?Python将是每个程序员的标配,有编程基础再掌握Python语言对于日后的升职加薪更有利.Python语言简洁利于理解,语法上相对容易能够让开发者更专注于业务 ...
随机推荐
- Mininet 系列实验(七)
实验内容 本实验在基于 Mininet 脚本的不同拓扑环境下使用 OpenDaylight 控制交换机行为.任务一:一台交换机两台主机,从1端口进入的数据流转发到 2 端口,从 2 端口进入的数据流转 ...
- 20135239 益西拉姆 linux内核分析 扒开系统调用的三层皮(下)
一. 给MenuOS增加time-asm命令 代码解释 1.-rf:强制删除 2.clone :重新克隆 3.time-asm:显示系统时间的汇编形式 给MenuOS增加time和time-asm命令 ...
- 洛谷 3379 最近公共祖先(LCA 倍增)
洛谷 3379 最近公共祖先(LCA 倍增) 题意分析 裸的板子题,但是注意这题n上限50w,我用的边表,所以要开到100w才能过,一开始re了两发,发现这个问题了. 代码总览 #include &l ...
- tokenizer
http://blog.csdn.net/beyond__devil/article/details/52829241
- 【纪中集训2019.3.11】Cubelia
题目: 描述 给出长度为\(n\)的数组\(a\)和\(q\)个询问\(l,r\). 求区间\([l,r]\)的所有子区间的前缀和的最大值之和: 范围: $n \le 2 \times 10^5 , ...
- 团体程序设计天梯赛L3-019 代码排版(23分)
打算学完编译原理后再次实现它... 以下为比较“杂乱”的方法: 海量数据: https://pan.baidu.com/s/1Prd0ZqNLoCLLvXyJjCef3w 如果大家有发现这个程序的问题 ...
- C标准库函数--文件IO操作函数。
C标准库文件读写函数总结:都是对文件流进行输入输出的函数分为对文件的有格式读写以及无格式读写 一.文件的无格式读写根据每次读写字符的数量,分为三类:1.按字符读写文件 按字符读有三个函数:以下三个函数 ...
- python---基础知识回顾(十)进程和线程(进程)
前戏:进程和线程的概念 若是学过linux下的进程,线程,信号...会有更加深刻的了解.所以推荐去学习下,包括网络编程都可以去了解,尤其是对select,poll,epoll都会有更多的认识. 进程就 ...
- canvas 入门
<canvas>是HTML5新增的,是可以使用脚本(JavaScript)在其中绘制图像的HTML元素. canvas是由HTML代码配合高度和宽度属性而定义出的可绘制区域,JavaScr ...
- ASP.NET IOC之 AutoFac的认识和结合MVC的使用
这几天研究了解发现AutoFac是个牛X的IOC容器,是.NET领域比较流行的IOC框架之一,传说是速度最快的,~ 据相关资料,相关学习,和认知,遂做了一些整理 优点: 它是C#语言联系很紧密,也就是 ...