官术网_书友最值得收藏!

How it works...

In this example, the whole video package comprises two sub-packages, video formats and video effects, with video effects having several sub-packages of its own. Within each package, each .py file is a separate module. During module importation, Python looks for packages on sys.path.

The inclusion of the __init__.py files is necessary so Python will treat the directories as packages. This prevents directories with common names from shadowing Python modules further along the search path. They also allow calling modules as stand-alone programs via the -m option, when calling Python programs.

Initialization files are normally empty but can contain initialization code for the package. They can also contain an __all__ list, which is a Python list of modules that should be imported whenever from <package> import * is used.

The reason for __all__ is for the developer to explicitly indicate which files should be imported. This is to prevent excessive delay from importing all modules within a package that aren't necessarily needed for other developers. It also limits the chance of undesired side-effects when a module is inadvertently imported. The catch is, the developer needs to update the __all__ list every time the package is updated.

Relative imports are based on the name of the current module. As the main module for a program always has the name "__main__", any modules that will be the main module of an application must use absolute imports.

To be honest, it is generally safer to use absolute imports just to make sure you know exactly what you're importing; with most development environments nowadays providing suggestions for paths, it is just as easy to write out the auto-populated path as it is to use relative paths.

主站蜘蛛池模板: 扶沟县| 蓬莱市| 锦屏县| 十堰市| 阿坝县| 宜章县| 临沭县| 枝江市| 黑河市| 嘉黎县| 理塘县| 营口市| 大同市| 峨眉山市| 漳浦县| 裕民县| 西平县| 遂昌县| 鹤峰县| 西吉县| 隆化县| 商丘市| 隆安县| 抚宁县| 呼和浩特市| 鹤壁市| 阳曲县| 历史| 托里县| 延津县| 余干县| 蚌埠市| 栾川县| 乌海市| 横山县| 绍兴县| 锡林郭勒盟| 墨玉县| 余姚市| 祥云县| 宁乡县|