from testpkg import mymodule

# -*- coding:gb2312 -*-
#!/usr/bin/python
# Filename: mymodule_demo3.py

from testpkg import mymodule
# Alternative:
# from mymodule import *

mymodule.sayhello()
print 'Version', mymodule.version
'''
D:\learnProg\python\exercise>python .\mymodule_demo3.py
testpkg
['a', 'b', 'c', 'd']
Hello, this is  testpkg.mymodule  speaking.
Version 0.1

D:\learnProg\python\exercise>
'''
# -*- coding:gb2312 -*-
# Filename: D:\learnProg\python\exercise\testpkg\__init__.py
print  __name__

__all__ = ['mymodule']
print __all__

'''
细心的用户会发现,有时在import语句中会出现通配符*,导入某个module中的所有元素,这是怎么实现的呢?
答案就在__init__.py中。我们在subPack1的__init__.py文件中写
__all__ = ['module_13', 'module_12']
然后进入python
>>>from package1.subPack1 import *
'''
#!/usr/bin/python
# Filename: D:\learnProg\python\exercise\testpkg\mymodule.py

def sayhello():
    print 'Hello, this is ', __name__  ,' speaking.'

version = '0.1'

# End of mymodule.py

 

posted @ 2018-01-19 10:58  sky20080101  阅读(91)  评论(0)    收藏  举报