python 模块的导入和使用


来看一看python的模块导入和是用
 
 
[root@web-02 dist-packages]# cat indentation.py
 
#!/usr/bin/enb python
version = 'v1.0'
author = 'Madon'
def sayHi (n):
  print 'hi , this is a module.',n

 

//这里说明一下,我写的这个模块。2-3行使用的变量,用户调用version和author输出对应内容。
//第四行使用sayHi 加用户输入内容。则输出print里面的内容。
 
[root@web-02 dist-packages]# pwd
/usr/lib/python2.7/dist-packages
[root@web-02 dist-packages]# 
 
 
>>> import tab
>>> import indentation
>>> indentation.
indentation.__class__(         indentation.__package__
indentation.__delattr__(       indentation.__reduce__(
indentation.__dict__           indentation.__reduce_ex__(
indentation.__doc__            indentation.__repr__(
indentation.__file__           indentation.__setattr__(
indentation.__format__(        indentation.__sizeof__(
indentation.__getattribute__(  indentation.__str__(
indentation.__hash__(          indentation.__subclasshook__(
indentation.__init__(          indentation.author
indentation.__name__           indentation.sayHi(
indentation.__new__(           indentation.version
>>> indentation.version
'v1.0'
>>> indentation.author
'Madon'
>>> indentation.sayHi('Madon')
hi , this is a module. Madon
>>> exit()
[root@web-02 di
 
os模块以及直接使用system
>>> import tab
>>> import os
>>> os.system('du ')
16      .
0
>>> os.system('df ') 
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       40771124 1879660  38891464   5% /
devtmpfs          240200       0    240200   0% /dev
tmpfs             248952       0    248952   0% /dev/shm
tmpfs             248952   24964    223988  11% /run
tmpfs             248952       0    248952   0% /sys/fs/cgroup
/dev/sda1          98988   95756      3232  97% /boot
0
>>> from os import system
>>> system('df')
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       40771124 1879660  38891464   5% /
devtmpfs          240200       0    240200   0% /dev
tmpfs             248952       0    248952   0% /dev/shm
tmpfs             248952   24964    223988  11% /run
tmpfs             248952       0    248952   0% /sys/fs/cgroup
/dev/sda1          98988   95756      3232  97% /boot
0
>>> 
>>> from os import system as s
>>> s('df ')
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       40771124 1879660  38891464   5% /
devtmpfs          240200       0    240200   0% /dev
tmpfs             248952       0    248952   0% /dev/shm
tmpfs             248952   24964    223988  11% /run
tmpfs             248952       0    248952   0% /sys/fs/cgroup
/dev/sda1          98988   95756      3232  97% /boot
0
>>> 
 
如果模块在这些路径下 没有找到的话,就会报错。
>>> import sys
>>> 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']
>>> 

评论关闭