python—模块,概念,基本操作
1.模块的基本概念
>>> import linecache
>>> dir(linecache)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'cache', 'checkcache', 'clearcache', 'getline', 'getlines', 'os', 'sys', 'updatecache']
>>> help(dir)
>>>
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
(END)
>>> linecache.__file__
'/usr/lib/python2.7/linecache.pyc'
>>>
if __name__ == "__main__":#读取到文件时才执行,可以放测试用列在此方法中,测试是否正确
print "sth"
2.模块的基本操作(以下3种方法)
import
from module import sth
from module import all
>>> import linecache
>>> linecache.getline
<function getline at 0xb74b5764>
>>>
>>> from linecache import getlines#大模块中导入一个特定方法(减少性能开销)
>>>
>>> from linecache import *#对模块linecache中导入所有方法,只能导入__all__定义的公开的函数,其他隐藏函数(私有的)不可以访问的
>>> getlines#可以直接使用linecache模块中getlines函数
<function getlines at 0xb74b57d4>
>>>
!注意!如:linecache模块中使用
from linecache import *
则只能导入__all__ = ["getline", "clearcache", "checkcache"]中的三个模块,像updatecache的私有函数是不可以访问的
root@kali:~/python/laowangpy/function# cat /usr/lib/python2.7/linecache.py
"""Cache lines from files.
This is intended to read lines from modules imported -- hence if a filename
is not found, it will look down the module search path for a file by
that name.
"""
import sys
import os
__all__ = ["getline", "clearcache", "checkcache"]
def getline(filename, lineno, module_globals=None):
lines = getlines(filename, module_globals)
if 1 <= lineno <= len(lines):
return lines[lineno-1]
else:
return '
..................
...................
......................
def updatecache(filename, module_globals=None):
"""Update a cache entry and return its list of lines.
If something's wrong, print a message, discard the cache entry,
and return an empty list."""
........................
........................
3.包的创建
包就是一堆的模块组成的,也是个文件夹(如下是testm文件夹)
同一目录调用模块1:
root@kali:~/python/laowangpy/function/testm# ls
m1.py test.py
root@kali:~/python/laowangpy/function/testm# python test.py
4
root@kali:~/python/laowangpy/function/testm# cat m1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
def hash():
return 4
root@kali:~/python/laowangpy/function/testm# cat test.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import m1#调用m1模块
print m1.hash()#直接使用m1模块的hash()函数
root@kali:~/python/laowangpy/function/testm# python test.py
4
root@kali:~/python/laowangpy/function/testm#
同一目录调用模块1:
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cat m1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
def hash():
return 4
root@kali:~/python/laowangpy/function/testm# cat tttt.py
#coding:utf-8
from m1 import hash#调用模块m1中的hash()函数功能
#from m1 import *#这个语句与from m1 import hash同样效果
print hash()#直接使用m1模块中hash()函数功能
root@kali:~/python/laowangpy/function/testm# python tttt.py
4
root@kali:~/python/laowangpy/function/testm#
谨记:慎用from m1 import * 可能会导致模块中函数名称与自定义的名称冲突!!!
跨目录调用模块1:
root@kali:~/python/laowangpy/function/testm# cat test.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
#import m1
#print m1.hash()
import m2.url#第一种方式:不在同一个目录下的模块文件,通过此种方法导入m2模块的url方法函数
print dir(m2)#查看m2有哪些内置方法
print m2.__file__#查看m2模块的文件路径
print "-----------------"
print m2.url
root@kali:~/python/laowangpy/function/testm# python test.py
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'url']
/root/python/laowangpy/function/testm/m2/__init__.pyc
-----------------
<module 'm2.url' from '/root/python/laowangpy/function/testm/m2/url.pyc'>
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc m2 test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cd m2
root@kali:~/python/laowangpy/function/testm/m2# cat __init__.py
root@kali:~/python/laowangpy/function/testm/m2# ls
__init__.py __init__.pyc url.py url.pyc
root@kali:~/python/laowangpy/function/testm/m2# cat url.py
#coding:utf-8
def get_page():
return "some page content!"
root@kali:~/python/laowangpy/function/testm/m2#
跨目录调用模块2:
root@kali:~/python/laowangpy/function/testm# pwd
/root/python/laowangpy/function/testm
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc m2 test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cat test.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import m2.url as url#第一种方式:不在同一个目录下的模块文件,通过此种方法导入m2模块的url方法函数
print url
print "------------------------"
print url.get_page()
root@kali:~/python/laowangpy/function/testm# python test.py
<module 'm2.url' from '/root/python/laowangpy/function/testm/m2/url.pyc'>
------------------------
some page content!
root@kali:~/python/laowangpy/function/testm# cd m2
root@kali:~/python/laowangpy/function/testm/m2# ls
__init__.py __init__.pyc url.py url.pyc
root@kali:~/python/laowangpy/function/testm/m2# pwd
/root/python/laowangpy/function/testm/m2
root@kali:~/python/laowangpy/function/testm/m2# cat __init__.py
root@kali:~/python/laowangpy/function/testm/m2# cat url.py
#coding:utf-8
def get_page():
return "some page content!"
root@kali:~/python/laowangpy/function/testm/m2#
跨目录调用模块3:
root@kali:~/python/laowangpy/function/testm# pwd
/root/python/laowangpy/function/testm
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc m2 test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cat test.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
#import m2.url as url#第一种方式:不在同一个目录下的模块文件,通过此种方法导入m2模块的url方法函数
from m2 import url#第二种方式
print url
print "------------------------"
print url.get_page()
root@kali:~/python/laowangpy/function/testm# python test.py
<module 'm2.url' from '/root/python/laowangpy/function/testm/m2/url.pyc'>
------------------------
some page content!
root@kali:~/python/laowangpy/function/testm# cd m2/
root@kali:~/python/laowangpy/function/testm/m2# ls
__init__.py __init__.pyc url.py url.pyc
root@kali:~/python/laowangpy/function/testm/m2# cat __init__.py
root@kali:~/python/laowangpy/function/testm/m2# cat url.py
#coding:utf-8
def get_page():
return "some page content!"
root@kali:~/python/laowangpy/function/testm/m2#
跨目录调用模块4:
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc m2 test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cat test.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
#import m2.url as url#第一种方式:不在同一个目录下的模块文件,通过此种方法导入m2模块的url方法函数
#from m2 import url#第二种方式
from m2.url import get_page#第二种方式
print get_page()
root@kali:~/python/laowangpy/function/testm# python test.py
some page content!
root@kali:~/python/laowangpy/function/testm# cd m2/
root@kali:~/python/laowangpy/function/testm/m2# ls
__init__.py __init__.pyc url.py url.pyc
root@kali:~/python/laowangpy/function/testm/m2# cat __init__.py
root@kali:~/python/laowangpy/function/testm/m2# cat url.py
#coding:utf-8
def get_page():
return "some page content!"
root@kali:~/python/laowangpy/function/testm/m2#
跨目录调用模块5:
root@kali:~/python/laowangpy/function/testm# pwd
/root/python/laowangpy/function/testm
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc m2 test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cat test.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
#import m2.url as url#第一种方式:不在同一个目录下的模块文件,通过此种方法导入m2模块的url方法函数
#from m2 import url#第二种方式
#from m2.url import get_page#第二种方式
from m2 import *#使用模块导入*的话,一定去m2模块文件夹中的__init__内容查看是否有__all__ = ["url"],没有内容则会报错,或为空也报错
print url.get_page()
root@kali:~/python/laowangpy/function/testm# python test.py
some page content!
root@kali:~/python/laowangpy/function/testm# cd m2/
root@kali:~/python/laowangpy/function/testm/m2# cat __init__.py
__all__ = ["url"]
root@kali:~/python/laowangpy/function/testm/m2# cat url.py
#coding:utf-8
def get_page():
return "some page content!"
root@kali:~/python/laowangpy/function/testm/m2#
4.搜索模块
root@kali:~/python/laowangpy/function/testm/m2# pwd
/root/python/laowangpy/function/testm/m2
root@kali:~/python/laowangpy/function/testm/m2# ls
abc.py __init__.py __init__.pyc url.py url.pyc
root@kali:~/python/laowangpy/function/testm/m2# cat abc.py
#coding:utf-8
#对下级目录调用上级目录中模块py文件或者调用任意位置的模块py文件,必须导入sys模块,并且定义这个要调用的模块py的绝对目录,用来搜索定位
import sys
sys.path.append('/root/python/laowangpy/function/testm')
import m1
print m1.hash()
root@kali:~/python/laowangpy/function/testm/m2# cd ..
root@kali:~/python/laowangpy/function/testm# pwd
/root/python/laowangpy/function/testm
root@kali:~/python/laowangpy/function/testm# ls
m1.py m1.pyc m2 test.py tttt.py
root@kali:~/python/laowangpy/function/testm# cat m1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
def hash():
return 4
root@kali:~/python/laowangpy/function/testm#