在python裡面,每個module會以一個file的方式儲存。但是python直譯器需要透過額外 __init__.py檔,才有辦法辨識某一個file是不是module。
在[1],我們引用下段文字來更了解:
Package are a way of structuring Python's module namespace by using "dotted module names". For example, the module name
A.B
designates submodule name B in a package named A. Juste like the use of modules saves author of different modules from having no worry about each other's global variable names, the use of dotted module names the authors of multi-module packages like NumPy or Pillow from having to worry about each other's module names.Suppose you want to design a collection of modules ( a "package") for the uniform handling of sound files and sound data. There are many different sound file formats (usually recognized by their extension, for example: .wav, .aiff, .au), so you may need to create and maintain a growing collection of modules for the conversion between various file formats. There are also many different operations you might want to to perform on sound data (such as mixing, adding echo, applying an equalizer function, creating an artificial stereo effect), so in addition you will be writing a never-ending stream of modules to perform these operations. Here's a possible structure for your package (expressed in terms of hierarchical filesystem) like Figure 1.
When importing the package, Python searches through the directories on sys.paht looking for the package subdirectory.
The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, form intententionally hiding vaild modules that occur later on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for package or set the __all variable.
Users can import individual modules from the packageimport sound.effects.echo
This loads submodule sound.effects.echo. It must be referenced with its full name.sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
An alternative way of importing the submodule is:from sound.effects import echo
This also loads the submoduleecho
, and makes it available without its package prefix ,so it can be used as follows:echo.echofilter(input, output, delay=0.7, atten=4)
Yet another variation is to import the desired function or variable directly:from sound.effects.echo import echofilter
Again, this loads the submoduleecho
, but this makes its functions echo filter() directly available:echo filter(input, output, delay=0.7, atten=4)
Note that when usingfrom package import item
, the item can be either a submodule (or sub package) of the package, or some other name defined in the package, like a function, class or variable. Theimport
statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError exception is raised. Contrarily, when using syntax likeimport item.subitem.subsubitem
, each item except for the last must be a package; the last item can be module or a package but can't be a class or function or variable defined in the previous item
Figure 1: Hierarchical filesystem of sound package and its submodule.
[0]
https://pydoing.blogspot.tw/2011/02/python-package.html
[1]