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

Python packages

A Python package is a directory containing Python modules and a __init__.py file. When we import a package, the __init__.py code is executed. This file contains the imports and code that's required to run other modules in the package. These packages can be nested within subdirectories. For example, the __init__.py file can contain import statements that bring in each Python file in the directory and all of the available classes or functions when the folder is imported. The following is an example directory structure and below that is the __init__.py file, which shows us how the two interact when imported. The last line in the following code block imports all specified items in the subdirectory's __init__.py file.

The hypothetical folder structure is as follows:

| -- packageName/
| -- __init__.py
| -- script1.py
| -- script2.py
| -- subDirectory/
| -- __init__.py
| -- script3.py
| -- script4.py

The top-level __init__.py file's contents is as follows:

from script1 import *
from script2 import function_name
from subDirectory import *

The code mentioned below executes the __init__ script we mentioned previously, and it will import all functions from script1.py, only function_name from script2.py, and any additional specifications from subDirectory/__init__.py:

import packageName  
主站蜘蛛池模板: 苍南县| 江阴市| 恩平市| 咸阳市| 乌拉特中旗| 大化| 方山县| 仙居县| 宜宾县| 密云县| 石台县| 池州市| 遂昌县| 万荣县| 广饶县| 邹城市| 黄梅县| 绵阳市| 肃南| 彭州市| 田阳县| 垦利县| 游戏| 孟村| 九龙坡区| 万年县| 大石桥市| 南涧| 巴南区| 年辖:市辖区| 鹤峰县| 大名县| 泾源县| 若羌县| 永泰县| 德格县| 勃利县| 安龙县| 克山县| 黄冈市| 遵义市|