Supplementary package python

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

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(lambda f:f.close() ,l)
14 
15 创建目录代码

3.目录结构
 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 
21     ├── __init__.py
22 
23     └── models.py
24 
25 目录结构

4.文件内容

5. software development specification

6. Absolute Import

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 is
24     ├── __init__.py
25 
└── the models.py 26 is 
27 
28 Absolute introduction 

7. relative introduced
Glance /                    
 2 
 . 3 ├── from __init__.py. # Import API. represents the current directory 
 . 4 from. Import cmd 
 . 5 from. Import DB 
 . 6 
 . 7 ├── API                   
 . 8 
 . 9 │ ├── from __init__.py. Import Policy 
10 from . Import versions 
. 11 
12 is │ ├── policy.py 
13 is 
14 │ └── versions.py 
15 
16 ├── from cmd. Import Manage 
. 17 
18 is │ ├── __init__.py 
. 19 
20 is │ └── from manage.py import policy ..api    
21 # .. is the parent directory, and want to manage the policy method used will need to return to the previous directory to find api package glance down import policy from api
22 is 
23 └── db               from . import models
24 
25 ├── __init__.py 
26 is 
27 └── the models.py 
28 
29 introduced into the relative 

method of module called directly after 8.import glance
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

Guess you like

Origin www.cnblogs.com/intruder/p/10928811.html