python modules and packages
https://realpython.com/python-modules-packages/
在软件开发中,一个module是具有一些相关功能的软件集合,比如,当你在开发一个游戏时,可能会有一个模块负责game logic,而另一个module负责在屏幕上绘制对应的界面。每个module是一个不同的文件,可以单独编辑。
modules
python中每一个单独的.py文件就是一个module,模块的名称就是文件的名称。一个module可以有一组函数,类或者变量。比如,上面说道的游戏开发中,可能会有两个文件组成的两个module:
# game.py
# import the draw module
import draw def play_game():
... def main():
result = play_game()
draw.draw_game(result) # this means that if this script is executed, then
# main() will be executed
if __name__ == '__main__':
main()
在上面的game模块中,我们定义了play_game函数,它将使用定义在draw模块中的draw_game函数。而draw模块中实现draw_game对应的逻辑。
下面我们看看draw 模块长的样子:
# draw.py def draw_game():
... def clear_screen(screen):
...
在本例子中game module通过import draw来加载draw模块,而这将赋能game模块引用draw模块中实现的函数或者类。为了使用draw模块中的draw_game,我们需要通过dot点操作符告知game块draw_game是哪个模块中的实现。
当import draw这个directive被执行时,python解释器将从game.py文件所在目录开始寻找draw.py文件,如果没有找到,python解释器将继续在built-in内置模块中寻找。
你可能注意到当导入一个module时,将会有一个.pyc文件出现,这个文件是一个编译过的Python文件。python解释器将module文件编译成python的byte code以便不用每次Import时都需要重新解析他。如果已经有了.pyc文件存在,则直接加载draw.pyc文件,这个过程对于用户来说是透明的。
importing module objects到当前的命名空间namespace
我们可以通过from及import命令将对应模块的函数加载到主脚本所在的命名空间中:
# game.py
# import the draw module
from draw import draw_game def main():
result = play_game()
draw_game(result)
通过上面的from, import后我们就不用再使用prefixmodule.function的方式来引用,而只需要function了,因为function已经存在于主脚本的命名空间里面了!
import all objects from module
# game.py
# import the draw module
from draw import * def main():
result = play_game()
draw_game(result)
自定义加载后的命名
# game.py
# import the draw module
if visual_mode:
# in visual mode, we draw using graphics
import draw_visual as draw
else:
# in textual mode, we print out text
import draw_textual as draw def main():
result = play_game()
# this can either be visual or textual depending on visual_mode
draw.draw_game(result)
上面的例子中,使用as关键字,以及条件加载使得不同的module中定义的函数使用同一个名称draw
module initialization
当一个module首次被加载时,对应module的代码将会执行一次以便初始化。如果其他的模块再次加载同一个module,则不会再重复加载!因此,module中的局部变量就像一个singleton一样,因为他们仅会初始化一次。
# draw.py def draw_game():
# when clearing the screen we can use the main screen object initialized in this module
clear_screen(main_screen)
... def clear_screen(screen):
... class Screen():
... # initialize main_screen as a singleton
main_screen = Screen()
看上面的例子,main_screen就是首次加载draw模块式初始化的变量,不会重复初始化!
扩展module的加载路径
在加载module时,除了默认的寻找路径:主脚本目录以及内置module外,我们可以通过以下方法告知python解释器,哪里可以去寻找对应的module:
- PYTHONPATH变量:
- sys.path.append
PYTHONPATH=/foo python game.py
sys.path.append("/foo") #在执行import之前运行该代码
built-in modules
和每个python发行版伴随的有很多内置的Python库,这些built-in modules使用C语言编写,提供诸如访问系统功能比如文件I/O的功能,这些库也提供一些常见问题的通用解决方案供调用。还有部分builtin模块用于抽象Python应用程序,以便和平台无关。
https://docs.python.org/3/library/
比如,我们向使用urllib这个内置库:
# import the library
import urllib # use it
urllib.urlopen(...)
我们可以通过dir函数来列出一个module中定义的函数:
>>> import urllib
>>> dir(urllib)
['ContentTooShortError', 'FancyURLopener', 'MAXFTPCACHE', 'URLopener', '__all__', '__builtins__',
'__doc__', '__file__', '__name__', '__package__', '__version__', '_ftperrors', '_get_proxies',
'_get_proxy_settings', '_have_ssl', '_hexdig', '_hextochr', '_hostprog', '_is_unicode', '_localhost',
'_noheaders', '_nportprog', '_passwdprog', '_portprog', '_queryprog', '_safe_map', '_safe_quoters',
'_tagprog', '_thishost', '_typeprog', '_urlopener', '_userprog', '_valueprog', 'addbase', 'addclosehook',
'addinfo', 'addinfourl', 'always_safe', 'basejoin', 'c', 'ftpcache', 'ftperrors', 'ftpwrapper', 'getproxies',
'getproxies_environment', 'getproxies_macosx_sysconf', 'i', 'localhost', 'main', 'noheaders', 'os',
'pathname2url', 'proxy_bypass', 'proxy_bypass_environment', 'proxy_bypass_macosx_sysconf', 'quote',
'quote_plus', 'reporthook', 'socket', 'splitattr', 'splithost', 'splitnport', 'splitpasswd', 'splitport',
'splitquery', 'splittag', 'splittype', 'splituser', 'splitvalue', 'ssl', 'string', 'sys', 'test', 'test1',
'thishost', 'time', 'toBytes', 'unquote', 'unquote_plus', 'unwrap', 'url2pathname', 'urlcleanup', 'urlencode',
'urlopen', 'urlretrieve']
当我们发现了我们希望使用的module中的某个function,还可以通过help命令来列出对应的帮助信息.
help(urllib.urlopen)
pypi第三方modules
虽然python本身内置了非常丰富的package供程序员使用,但是依然有很多场景需要加载第三方的package,比如numpy, pandas等等。。
https://pypi.org/
开发package
packages是包含了多个package和module的命名空间。简单来说,package就是一些目录,仅此而已。只要目录中包含了一个命名为__init__.py的特殊文件,我们就称该目录为一个package。这个文件本身可以是空的,这个文件的存在标识了该目录为一个python package.
比如,如果我们创建一个目录:foo,那么foo作为package名称,然后我们创建一个模块并命名为bar.py,我们不要忘记在foo目录下增加一个__init__.py的文件。那么要使用这个bar模块,我们可以这样做:
import foo.bar
# 或者:
from foo import bar
在__init__.py文件中,我们可以指定哪些模块作为暴露的api,而其他的模块作为私有的:
__init__.py: __all__ = ["bar"]
python modules and packages的更多相关文章
- Python Modules and Packages – An Introduction
This article explores Python modules and Python packages, two mechanisms that facilitate modular pro ...
- 解决yum升级的问题“There was a problem importing one of the Python modules”
yum命令升级的时候,报出这个错误. There was a problem importing one of the Python modules required to run yum. The ...
- Python Modules
[Python Modules] 1. a module is a python source file. 2. a package is a directory with a __init__.py ...
- Installing Python Modules
Email: distutils-sig@python.org As a popular open source development project, Python has an active s ...
- [Python] Reuse Code in Multiple Projects with Python Modules
A module is a function extracted to a file. This allows you to import the function and use it in any ...
- [Python Modules] unittest.mock
五夜光寒,照来积雪平于栈.西风何限,自起披衣看. 对此茫茫,不觉成长叹.何时旦,晓星欲散,飞起平沙雁. 在某个Python程序中看到这么一行 from unittest import mock 看起来 ...
- centos系统重装python或yum 报There was a problem importing one of the Python modules required to run yum. The error leading to this problem was:错误
sudo vim /usr/bin/yum #修个python所在的路径,例如 #/usr/local/bin/python2.6 或 /usr/local/bin/python2.7要原本你的系统原 ...
- [Dynamic Language] 用Sphinx自动生成python代码注释文档
用Sphinx自动生成python代码注释文档 pip install -U sphinx 安装好了之后,对Python代码的文档,一般使用sphinx-apidoc来自动生成:查看帮助mac-abe ...
- 后台运行python程序 遇到缓冲区问题
From: http://www.iteye.com/topic/867446 环境:linux 一段执行时间很长的程序(用python做hive客户端执行mapreduce) 在linux后台执行, ...
随机推荐
- spring boot整合Swagger2
Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服务器 ...
- HUE配置文件hue.ini 的hdfs_clusters模块详解(图文详解)(分HA集群和非HA集群)
不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...
- springboot-25-springboot 集成 ActiveMq
消息的发布有2种形式, 队列式(点对点) 和主题式(pub/sub) 模式, 队列式发布后, 接收者从队列中获取消息后, 消息就会消失, 但任意消费者都可以从队列中接受消息, 消息只能被接受一次 主题 ...
- Apache Thrift - 可伸缩的跨语言服务开发框架 ---转载
src:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ http://thrift.apache.org/
- [PKUWC 2018]随机算法
Description 题库链接 给定一张有 \(n\) 个点 \(m\) 条边的无向图,生成 \(1\sim n\) 的全排列,假设一个排列是 \(p\) , \(S\) 是当前最大独立集:如果 \ ...
- centos 6.5安装docker
安装linux,需要系统内核为3.x以上,如果centos版本为7以下,先升级系统内核 1.关闭selinux setenforce 0 sed -i '/^SELINUX=/c\SELINUX=di ...
- elasticsearch 分布式集群搭建
elasticsearch环境搭建及单节点搭建可参考我的上一篇:http://www.cnblogs.com/xuwenjin/p/8745624.html 本文以Elaticsearch 6.2.2 ...
- 使用tcmalloc替换系统的malloc
https://blog.csdn.net/educast/article/details/79166553?utm_source=blogxgwz0 今天对服务器进行压测,模拟的请求量到4万次/分的 ...
- php 多次导入文件导致 Cannot redeclare class
定义了类A b.php中导入了A c.php中导入了b.php c.php中导入A会报错:
- CentOS系统下安装SVN及常用命令
1.SVN的安装: yum install subversion 2.服务端命令 svnserver -- 控制svn系统服务的启动等 svnadmin -- 版本库的创建.导出.导入.删除等 svn ...