Module
module:
The basic embodiment modular organization code pyth1.on
A python script can be run separately, you may be introduced into another script run, when the script is executed when introduced, which we will call the module (Module1)
All points p for the file can be imported as a module
Module with the same file name of the script, for example, we wrote a script called hello.pv you can import hello.py to import it in another script using
package:
python modules can be organized according to the directory for the package
Steps to create a package:
创建一个名字为包名字的目录,在该目录下创建一个__init__.py文件,根据需要在该目录下存放脚本文件或已编译的扩展及子包
grammar:
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/lib64/python2.7/site-packages/gtk-2.0',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython']
vim /root/.bashrc
最后加个export PYTHONPATH=/root/library
成功加入到Python环境变量中
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/lib64/python2.7/site-packages/gtk-2.0',
'/usr/lib/python2.7/site-packages',
'/usr/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython']
可被其他脚本调用的脚本:
#!/usr/bin/env python
#
def wordCount(s):
chars = len(s)
words = len(s.split())
lines = s.count('\n')
print lines,words,chars
if __name__=='__main__': #设置后,本脚本内的函数可被其他脚本调用test.py
s = open('/etc/passwd').read()
wordCount(s)
#
[root[@localhost](https://my.oschina.net/u/570656) 20181203]# python words.py
27 52 1324
[root[@localhost](https://my.oschina.net/u/570656) 20181203]# wc /etc/passwd
27 52 1324 /etc/passwd
test.py中导入words.py:
[root[@localhost](https://my.oschina.net/u/570656) 20181203]# cat test.py
#!/usr/bin/env python
import words
#
s="""hello world python"""
words.wordCount(s)
[root[@localhost](https://my.oschina.net/u/570656) 20181203]# python test.py
27 52 1324
[root[@localhost](https://my.oschina.net/u/570656) 20181203]# ls
test.py words.py words.pyc ###调用words后自动生成编译的文件。
Modules and packages:
the module is a script file that can be imported
Some packages are organized by category and sub-modules package, a directory the init .py file that can be stored packets.
Grammar module and import the package:
import,import as
from ... import ...
In [2]: from mode import mode
2 10 158
In [3]: mode.wordCount('abc')
0 1 3
In [4]: import mode.mode
In [5]: mode.mode.wordCount('hello')
0 1 5
In [7]: from mode.mode import wordCount
In [8]: wordCount('aaa')
0 1 3
#重新命名为wc
In [10]: from mode.mode import wordCount as wc
In [11]: wc('aaaddd')
0 1 6
#from package.mod import wordCount
#from package import mod
In [10]: from package import mod
In [11]: mod.wordCount('abc 123')
0 2 7
In [15]: from package.mod import wordCount
In [16]: wordCount('ava adsf34')
0 2 10