Python模块

2 篇文章 0 订阅
1 篇文章 0 订阅


Python搜索路径的目录列表可在sys模块的path变量中找到:

>>> import sys, pprint
>>> pprint.pprint(sys.path)
['',
 '/usr/lib64/python27.zip',
 '/usr/lib64/python2.7',
 '/usr/lib64/python2.7/plat-linux2',
 '/usr/lib64/python2.7/lib-tk',
 '/usr/lib64/python2.7/lib-old',
 '/usr/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages']
>>> 

如何让你的模块可用:

1、只要将模块放入类似site-packages这样的目录中,所有程序都能将其导入了。

2、编辑sys.path,或是在环境变量PYTHONPATH中包含模块所在目录。

设置PYTHONPATH:

①在UNIX和Mac OS中,你可以在每次登陆都要执行的shell文件中设置环境变量。如果你使用类似bash的shell文件,那么设置的就是.bashrc,你可以在主目录中找到它。将下面的命令添加到啊这个文件中,从而将~/python加入到PYTHONPATH:

export PYTHONPATH=$PYTHONPATH:~/python

多个路径以冒号分隔。

②将需要配置的路径放在以.pth为扩展名的文件中,其中的import开头的文件会被执行。将.pth放置到site-packages目录或是sys.prefix目录(windows而言)。


查看模块中有哪些名称:

>>> import copy
>>> [n for n in dir(copy) if not n.startswith('_')]
['Error', 'PyStringMap', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']

>>> dir (copy)
['Error', 'PyStringMap', '_EmptyClass', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_dispatch', '_copy_immutable', '_copy_inst', '_copy_with_constructor', '_copy_with_copy_method', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_inst', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', '_test', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']

使用Python你自带文档学习模块使用:

>>> help(copy)


Help on module copy:


NAME
    copy - Generic (shallow and deep) copying operations.


FILE
    /usr/lib64/python2.7/copy.py


DESCRIPTION
    Interface summary:
    
            import copy
    
            x = copy.copy(y)        # make a shallow copy of y
            x = copy.deepcopy(y)    # make a deep copy of y

……

>>> print range.__doc__
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers


Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
>>> range(1,8,2)
[1, 3, 5, 7]

标准库:

sys模块让你能够访问与Python解释器联系紧密的变量和函数。

os模块提供了访问多个操作系统服务的功能。

fileinput模块能够让你遍历文本文件的所有行。

[root@admin-node python-learn]# cat numberlines.py 
#numberlines.py

import fileinput

for line in fileinput.input(inplace = True):
    line = line.rstrip()
    num = fileinput.lineno()
    print '%-40s # %2i' % (line, num)
[root@admin-node python-learn]# 

数据结构:

set,dic,list,heap,deque

set:frozenset

heap:heapq模块

deque:collections模块



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值