Module of the same name does not conflict 1. A packet and the packet B, Aa and Ba as from two namespaces
2. The common directory structure
1 import os 2 os.makedirs('glance/api') 3 os.makedirs('glance/cmd') 4 os.makedirs('glance/db') 5 l = [] 6 l.append(open('glance/__init__.py','w')) 7 l.append(open('glance/api/__init__.py','w')) 8 l.append(open('glance/api/policy.py','w')) 9 l.append(open('glance/api/versions.py','w')) 10 l.append(open('glance/cmd/__init__.py','w')) 11 l.append(open('glance/cmd/manage.py','w')) 12 l.append(open('glance/db/models.py','w')) 13 map(the lambda F: f.close (), L) 14 15 create a directory of code
3. Directory Structure
1 glance/ #Top-level package 2 3 ├── __init__.py #Initialize the glance package 4 5 ├── api #Subpackage for api 6 7 │ ├── __init__.py 8 9 │ ├── policy.py 10 11 │ └── versions.py 12 13 ├── cmd #Subpackage for cmd 14 15 │ ├── __init__.py 16 17 │ └── manage.py 18 . 19 └── DB # Subpackage for DB 20 is 21 is ├── the __init__ .py 22 is 23 is └── the models.py 24 25 directory structure
4. The contents of the file
1 #文件内容 2 3 #policy.py 4 def get(): 5 print('from policy.py') 6 7 #versions.py 8 def create_resource(conf): 9 print('from version.py: ',conf) 10 11 #manage.py 12 def main(): 13 print('from manage.py') 14 15 #models.py 16 DEF register_models (Engine): . 17 Print ( ' from the models.py: ' , Engine) 18 is . 19 file content
5. software development specification
6. Absolute Import
1 glance/ 2 3 ├── __init__.py from glance import api 4 from glance import cmd 5 from glance import db 6 7 ├── api 8 9 │ ├── __init__.py from glance.api import policy 10 from glance.api import versions 11 12 │ ├── policy.py 13 14 │ └── versions.py 15 16 ├── cmd from glance.cmd import manage 17 18 │ ├── __init__.py 19 20 │ └── manage.py 21 22 └── db from glance.db import models 23 24 ├── __init__.py 25 26 └── models.py 27 28 绝对导入
7. relative imports
1 glance/ 2 3 ├── __init__.py from . import api #.表示当前目录 4 from . import cmd 5 from . import db 6 7 ├── api 8 9 │ ├── __init__.py from . import policy 10 from . import versions 11 12 │ ├── policy.py 13 14 └── versions.py │ 15 16 ├── cmd from . Import Manage . 17 18 is │ ├── the __init__ .py . 19 20 is │ └── manage.py from ..api Import Policy 21 is # .. indicates the parent directory , would like to manage the use of the policy method requires a glance back down the package directory to find api, import policy from api 22 is 23 is └── DB from . import Models 24 25 ├── the __init__ .py 26 is 27 └ the models.py - the 28 29 relative imports
Methods module called directly after glance 8.import
1 glance/ 2 3 ├── __init__.py from .api import * 4 from .cmd import * 5 from .db import * 6 ├── api 7 8 │ ├── __init__.py __all__ = ['policy','versions'] 9 10 │ ├── policy.py 11 12 │ └── versions.py 13 14 ├── cmd __all__ = ['manage'] 15 16 │ ├── __init__.py 17 18 │ └── manage.py 19 20 └── db __all__ = ['models'] 21 22 ├── __init__.py 23 24 └── models.py 25 26 27 import glance 28 policy.get() 29 30 import glance
Module of the same name does not conflict 1. A packet and the packet B, Aa and Ba as from two namespaces
2. The common directory structure
1 import os 2 os.makedirs('glance/api') 3 os.makedirs('glance/cmd') 4 os.makedirs('glance/db') 5 l = [] 6 l.append(open('glance/__init__.py','w')) 7 l.append(open('glance/api/__init__.py','w')) 8 l.append(open('glance/api/policy.py','w')) 9 l.append(open('glance/api/versions.py','w')) 10 l.append(open('glance/cmd/__init__.py','w')) 11 l.append(open('glance/cmd/manage.py','w')) 12 l.append(open('glance/db/models.py','w')) 13 map(the lambda F: f.close (), L) 14 15 create a directory of code
3. Directory Structure
1 glance/ #Top-level package 2 3 ├── __init__.py #Initialize the glance package 4 5 ├── api #Subpackage for api 6 7 │ ├── __init__.py 8 9 │ ├── policy.py 10 11 │ └── versions.py 12 13 ├── cmd #Subpackage for cmd 14 15 │ ├── __init__.py 16 17 │ └── manage.py 18 . 19 └── DB # Subpackage for DB 20 is 21 is ├── the __init__ .py 22 is 23 is └── the models.py 24 25 directory structure
4. The contents of the file
1 #文件内容 2 3 #policy.py 4 def get(): 5 print('from policy.py') 6 7 #versions.py 8 def create_resource(conf): 9 print('from version.py: ',conf) 10 11 #manage.py 12 def main(): 13 print('from manage.py') 14 15 #models.py 16 def register_models(engine): 17 print('from models.py: ',engine) 18 19 文件内容
5.软件开发规范
6.绝对导入
1 glance/ 2 3 ├── __init__.py from glance import api 4 from glance import cmd 5 from glance import db 6 7 ├── api 8 9 │ ├── __init__.py from glance.api import policy 10 from glance.api import versions 11 12 │ ├── policy.py 13 14 │ └── versions.py 15 16 ├── cmd from glance.cmd import manage 17 18 │ ├── __init__.py 19 20 │ └── manage.py 21 22 └── db from glance.db import models 23 24 ├── __init__.py 25 26 └── models.py 27 28 绝对导入
7.相对导入
1 glance/ 2 3 ├── __init__.py from . import api #.表示当前目录 4 from . import cmd 5 from . import db 6 7 ├── api 8 9 │ ├── __init__.py from . import policy 10 from . import versions 11 12 │ ├── policy.py 13 14 │ └── versions.py 15 16 ├── cmd from . import manage 17 18 │ ├── __init__.py 19 20 │ └── manage.py from ..api import policy 21 #..表示上一级目录,想再manage中使用policy中的方法就需要回到上一级glance目录往下找api包,从api导入policy 22 23 └── db from . import models 24 25 ├── __init__.py 26 27 └── models.py 28 29 相对导入
8.import glance 之后直接调用模块中的方法
1 glance/ 2 3 ├── __init__.py from .api import * 4 from .cmd import * 5 from .db import * 6 ├── api 7 8 │ ├── __init__.py __all__ = ['policy','versions'] 9 10 │ ├── policy.py 11 12 │ └── versions.py 13 14 ├── cmd __all__ = ['manage'] 15 16 │ ├── __init__.py 17 18 │ └── manage.py 19 20 └── db __all__ = ['models'] 21 22 ├── __init__.py 23 24 └── models.py 25 26 27 import glance 28 policy.get() 29 30 import glance