[Python tutorial] Chapter 66 package

In this article, we introduce packages in Python and learn how to use packages to build applications.

Python package

Suppose we need to develop a large application to handle the sales process from order to payment. This application will contain many modules. With the increase of the number of modules, it is difficult to maintain all modules in one place. At the same time, we also want to group modules in a certain way. At this point, we need to use the package.

Packages can organize modules in a hierarchical structure. Python packages and modules are structured like folders and files in the operating system. To create a package is to create a folder and then store the relevant module files in that folder.

In order for Python to use a folder as a package, we need to create one in the folder__ init__.py file.

Note that the function of implicit namespace package has been introduced since Python 3.3, which allows Python to__ init__. The folder of Py files is used as a package.

For example, the following figure shows the file structure of sales package, including order, delivery and billing packages:

Import package

The syntax of importing a package using the import statement is as follows:

import package.module

Then, the syntax of accessing objects in the module is as follows:

package.module.function

The following example uses the order, delivery and billing module functions in the sales package:

# main.py
import sales.order
import sales.delivery
import sales.billing


sales.order.create_sales_order()
sales.delivery.create_delivery()
sales.billing.create_billing()

To simplify the code, we can use the following statement to import the functions in the module:

from module import function

For example:

# main.py
from sales.order import create_sales_order
from sales.delivery import create_delivery
from sales.billing import create_billing


create_sales_order()
create_delivery()
create_billing()

You can also rename objects when importing them:

# main.py
from sales.order import create_sales_order as create_order
from sales.delivery import create_delivery as start_delivery
from sales.billing import create_billing as issue_billing


create_order()
start_delivery()
issue_billing()

Package initialization

By convention, when we import a package, Python executes the__ init__.py file. Therefore, we can__ init__. Initialize package level data in py file.

The following example is in the sales package__ init__.py file defines a default tax rate:

# __init__.py

# default sales tax rate
TAX_RATE = 0.07

In main Py file, we can access tax in the sales package_ RATE:

# main.py
from sales import TAX_RATE

print(TAX_RATE)

In addition to initialization data__ init__.py files can also be used to import modules from other packages. For example:

# __init__.py

# import the order module automatically
from sales.order import create_sales_order

# default sales tax rate
TAX_RATE = 0.07

In main After the PY file is imported into the sales package, you can directly use create_sales_order function:

# main.py
import sales

sales.order.create_sales_order()

from package import *

When we use the following statement to import all objects in the package:

from package import *

Python will find__ init__.py file. If the file exists, python loads a special list in the file__ all__ All modules included in. For example, we can__ all__ Add the following modules to the list:

# __init__.py

__all__ = [
    'order',
    'delivery'
]

Then in main The following import statements are used in the. Py file:

# main.py
from sales import *


order.create_sales_order()
delivery.create_delivery()

# cannot access the billing module

We are in main The functions in the order and delivery modules are accessed in the. Py file. However, we cannot use the billing module because it is not available__ all__ In the list.

subpackage

Python packages can contain sub packages. Sub packages can be used to further organize modules.

The sales package in the figure below contains three sub packages: order, delivery and billing. Each sub package has a corresponding module. We can put other modules related to order processing into the order sub package:

Package related operations can also be used for sub packages. For example, we can use the following statement to import the functions in the order sub package:

# main.py
from sales.order import create_sales_order

create_sales_order()

summary

  • A python package contains one or more modules. Python uses folder and file structures to manage packages and modules.
  • __ init__.py file is used to initialize package level data.
  • __ all__ The modules in the variable are loaded automatically when the package is imported.
  • Packages can contain sub packages.

Keywords: Python

Added by MatthewBJones on Mon, 28 Feb 2022 12:16:12 +0200