python模块使用
模块 module
- 模块是python组织代码的基本方式
- 一个python脚本可以单独运行,也可以导入到另一个脚本中运行,当脚本被导入运行时,称其为模块(module)
- 所有的.py文件都可以作为一个模块导入
- 模块名与脚本的文件名相同
- 例如写了一个名为hello.py的脚本,则可以在另一个脚本中用import hello语句来导入它
包 package
- python的模块可以按目录组织为包
- 创建一个包的步骤如下:
- 创建一个名为包名的目录
- 在该目录下创建一个init.py文件
- 根据需要,在该目录下存放脚本文件或已编译的扩展及子包
- import pack.m1,pack.m2,pack.m3
修改环境变量
- 修改sys.path这个变量
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/bin',
'/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/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/cloud_init-0.7.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython']
In [3]: sys.path.append('/root/library')
In [4]: sys.path
Out[4]:
['',
'/usr/bin',
'/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/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/cloud_init-0.7.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython',
'/root/library'] # 注意加到末尾
In [5]: '
- 修改 /root/.bashrc 这个文件
[root@KARL ~]# vim /root/.bashrc #如下所示
[root@KARL ~]# . /root/.bashrc
[root@KARL ~]# echo $PYTHONPATH
/root/library
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# 末尾添加如下这一句
export PYTHONPATH=/root/library
重新打开一个终端
[root@KARL ~]# . /root/.bashrc
[root@KARL ~]# ipython
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/bin',
'/root/library', # 注意加到前面
'/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/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/cloud_init-0.7.6-py2.7.egg',
'/usr/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython']
统计文件的行数、字数、字符数
#!/usr/bin/python
# -*- coding:utf-8 -*-
def wordCount(s):
chars = len(s)
words = len(s.split())
lines = s.count('\n')
# lines = len(s.split('\n')) # 这样统计行数会多一行
print lines, words, chars
with open('/etc/passwd') as fd:
s = fd.read()
wordCount(s)
[root@KARL ~]# python u1.py
22 31 956
[root@KARL ~]# wc /etc/passwd # 使用wc统计文件的行数、字数和字符数
22 31 956 /etc/passwd
[root@KARL ~]# cp u1.py wc.py
[root@KARL ~]# vim u2.py
[root@KARL ~]# cat u2.py
#!/usr/bin/python
import wc
[root@KARL ~]# python u2.py
22 31 956
[root@KARL ~]# vim u2.py
[root@KARL ~]# cat u2.py
#!/usr/bin/python
import wc
s = '''hello workd
python'''
wc.wordCount(s)
[root@KARL ~]# python u2.py
22 31 956
1 3 18
[root@KARL ~]# cat wc.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
def wordCount(s):
chars = len(s)
words = len(s.split())
lines = s.count('\n')
# lines = len(s.split('\n')) # 这样统计行数会多一行
print lines, words, chars
print __name__
'''
with open('/etc/passwd') as fd:
s = fd.read()
wordCount(s)
'''
[root@KARL ~]# python wc.py
__main__
[root@KARL ~]# python u2.py
wc
1 3 18
[root@KARL ~]# cat wc.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
def wordCount(s):
chars = len(s)
words = len(s.split())
lines = s.count('\n')
print lines, words, chars
if __name__ == '__main__': # 多加1行判断,是的输出符合预期
with open('/etc/passwd') as fd:
s = fd.read()
wordCount(s)
[root@KARL ~]# python wc.py
22 31 956
[root@KARL ~]# python u2.py
1 3 18
包的导入
# 创建空脚本 __init__.py
[root@KARL ~]# mkdir newpack
[root@KARL ~]# cp wc.py newpack/
[root@KARL ~]# touch newpack/__init__.py
[root@KARL ~]# ls newpack/
__init__.py __init__.pyc wc.py wc.pyc
# 1)
In [1]: import newpack.wc
In [2]: wc.wordCount('hell0 \n')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-3146ba553844> in <module>()
----> 1 wc.wordCount('hell0 \n')
NameError: name 'wc' is not defined
In [4]: newpack.wc.wordCount('hello \n')
1 1 7
# 2)
In [1]: from newpack import wc
In [2]: wc.wordCount('abc asd\n')
1 2 8
# 3)
In [1]: from newpack.wc import wordCount
In [2]: wordCount('hello \n')
1 1 7
# 4)
In [1]: from newpack.wc import wordCount as wc
In [2]: wc('hello')
0 1 5
网友评论