1. instance and Result
1) instance structure and specific files:
Pypackage
│ Pycommonm. py
│ _ Init _. py
│
─ ── P1package
│ P1m. py
│ P1mc. py
│ _ Init _. py
│
2017-p2
P2.py
P2m. py
2) pycommonm. py
Def pycommonmf (): Print "pycommonmf"
3) p1m. py:
Def p1mf (): Print 'p1mf'
4) p1mc. py:
Class p1mc ():
@ Staticmethod
Def p1olap (): Print 'p1w'
5) p2m. py:
Def p2mf (): Print 'p2mf'
6) p2.py:
Import p2m
From pypackage import pycommonm
From pypackage. p1package import p1m
From pypackage. p1package. p1mc import p1mc
Def p2f ():
Print 'p2f'
If _ name _ = '_ main __':
P2f ()
P2m. p2mf ()
P1m. p1mf ()
P1mc. p1olap ()
Pycommonm. pycommonmf ()
7) Running p2.py:
P2f
P2mf
P1mf
P1olap
Pycommonmf
Ii. Explanation
* A py file is a module, such as module: pycommonm. py, p2m. py, p1mc. py, and p1m. py.
* The folder containing _ init _. py is a package, for example, package: pypackage and p1package.
* You can directly use import to reference other modules in the same directory, for example, import p2m in p2.py.
* Use from import to reference the modules belonging to a certain package in other directories, for example, from pypackage import pycommonm and from pypackage. p1package import p1m in Py. py.
* Use from import to reference classes in a module, for example, from pypackage. p1package. p1mc import p1mc.
Note that the directory where the package is located must be in the pythonpath environment variable.
Complete!