python---模块定义,模块导入方式,模块同目录调用、模块跨目录调用

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# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐为波

看着给就好了,学习写作有点累!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值