1. A module with the same name under Package A and package B does not conflict because A.A and B.A come from two namespaces
2. Common directory Structure
# Create Directory Code import osos.makedirs (' Glance/api ') os.makedirs (' Glance/cmd ') os.makedirs (' glance/db ') L = []l.append (' glance/__init__.py ', ' W ')) l.append (open (' glance/api/__init__.py ', ' W ')) l.append (open (' glance/api/policy.py ', ' W ') ) L.append (Open (' glance/api/versions.py ', ' W ')) l.append (open (' glance/cmd/__init__.py ', ' W ')) l.append (Open (' glance /cmd/manage.py ', ' W ')) l.append (open (' glance/db/models.py ', ' W ')) Map (Lambda F:f.close (), L)
3. Directory structure
glance/ #Top-level package├──__init__.py #Initialize The Glance package├──api #Subpackage for api│ ├──__init__.py│ ├──policy.py│ └──versions.py├──cmd #Subpackage for cmd│ ├──__init__.py│ └── Manage.py└──db #Subpackage for db ├──__init__.py └──models.py
4. File contents
#文件内容 #policy.pydef Get (): print (' from policy.py ') #versions. Pydef create_resource (conf): print (' From version.py: ', conf) #manage. Pydef Main (): print (' from manage.py ') #models. Pydef register_models (engine): Print (' From models.py: ', engine)
5. Software Development Specification
6. Absolute Import
glance/ ├──__init__.py from glance Import API from glance import cmd from glance import Db├──api │< c5/>├──__init__.py from glance.api Import policy from glance.api import versions│ ├──policy.py│ └── Versions.py├──cmd from glance.cmd import manage│ ├──__init__.py│ └──manage.py└──db from GLANCE.DB Import Models ├──__init__.py └──models.py
7. Relative Import
glance/ ├──__init__.py from . Import API #. Indicates the current directory from the . Import cmd from . Import Db├──api │
├──__init__.py from. Import Policy from . Import versions│ ├──policy.py│ └──versions.py├──cmd< C11/>from. Import manage│ ├──__init__.py│ └──manage.py from : API Import Policy #: Represents the previous level of the directory, you want to manage the method in the policy will need to go back to the previous level glance directory to find the API package, import from the API policy└──db from . Imports Models ├──__init__ . PY └──models.py
8.import glance directly after calling the method in the module
glance/ ├──__init__.py from, API import * from. cmd import * from . DB Import * ├──api │ ├── __init__.py __all__ = [' policy ', ' versions ']│ ├──policy.py│ └──versions.py├──cmd __all__ = [' Manage '] │ ├──__init__.py│ └──manage.py └──db __all__ = [' models '] ├──__init__.py └──models.pyimport glancepolicy.get () Import glance
Python full stack development "supplemental" package supplement