标签:影响 port imp def https ges http display input
__all__ = ["test1","num"] #只能让调用test1,num1 def test1(): print("----test1") def test2(): print("----test2") num = 1 class Dog(object): pass
In [6]: from sendmsg import * #只能调用__all__里面规定的方法 In [7]: test1() ----test1 In [8]: test2() --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-8-35ebc1c532fc> in <module>() ----> 1 test2() NameError: name ‘test2‘ is not defined In [9]: num Out[9]: 1 In [10]: Dog() --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-10-ff6dc2469c3e> in <module>() ----> 1 Dog() NameError: name ‘Dog‘ is not defined
In [1]: import sendmsg #通过这个方式不受影响 In [2]: sendmsg. sendmsg.Dog sendmsg.py sendmsg.test2 sendmsg.num sendmsg.test1 In [2]: sendmsg. sendmsg.Dog sendmsg.py sendmsg.test2 sendmsg.num sendmsg.test1 In [2]: sendmsg.test1() ----test1 In [3]: sendmsg.test2() ----test2
### sendmsg.py def test1(): print("--sendmsg-test1---") #### recvmsg.py def test2(): print("---recvmsg--test2---")
1)版本1:
.
├── infordisplay.py
└── Msg #具有很多模块的集合场所
├── recvmsg.py
└── sendmsg.py
#### IPython 2.4.1 python2认为这只是文件夹 In [1]: import Msg --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-1-32b2df17b362> in <module>() ----> 1 import Msg ImportError: No module named Msg
### iPython 3.5.2 python认为此文件夹是包 In [1]: import Msg In [2]: Msg.sendmsg.test1() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-2-889fe4d38288> in <module>() ----> 1 Msg.sendmsg.test1() AttributeError: module ‘Msg‘ has no attribute ‘sendmsg‘
2)版本2:__init__.py 让文件夹变成包
. ├── infordisplay.py └── Msg #包 ├── __init__.py ├── recvmsg.py └── sendmsg.py
### IPython 2.4.1 In [1]: import Msg In [2]: Msg.sendmsg.test1() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-2-889fe4d38288> in <module>() ----> 1 Msg.sendmsg.test1() AttributeError: ‘module‘ object has no attribute ‘sendmsg‘
#### Python 3.5.2 In [1]: import Msg In [2]: Msg.sendmsg.test1() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-2-889fe4d38288> in <module>() ----> 1 Msg.sendmsg.test1() AttributeError: module ‘Msg‘ has no attribute ‘sendmsg‘
3)版本3:
### Msg/__init__.py __all__ = ["sendmsg"] print("when import this bag ,do this __init__.py ") import sendmsg
#### IPython 2.4.1 可以直接执行
### python3 不能执行 In [1]: import Msg when import this bag ,do this __init__.py In [2]: Msg.sendmsg.test1() --sendmsg-test1---
4)版本4:最终版本
## Msg/__init__.py __all__ = ["sendmsg"] print("when import this bag ,do this __init__.py ") from . import sendmsg #. 当前目录
#### python 2 3 都可以执行 In [1]: import Msg when import this bag ,do this __init__.py In [2]: Ms Msg Msg/ In [2]: Msg.sendmsg.test1() --sendmsg-test1---
标签:影响 port imp def https ges http display input
原文地址:http://www.cnblogs.com/venicid/p/7896229.html