[Hikari-Python] Chapter 08-02 Module (Import Package)

[Python] Chapter 08-02 Importing packages

In the previous section, we imported a file called a module and called the function. I used to import a single module there, but as I create various programs in the future, the number of modules will increase, and eventually management will become complicated.

In this section, you can make it easier to manage modules by grouping them into units called packages so that they are not complicated to manage. This allows you to organize ** modules **, but you can also manage ** modules in a hierarchy ** within a package. This section touches on the handling of such packages.

As an image, ** module ** is a file, and the folder that manages it is ** package **. image.png

Creating a package

Let's create a package. First, create an empty package by following the steps below. (Right-click [python folder]-> [New]-> [Python Package]) image.png

Name the package pkg </ font>. image.png

Then, the package will be created as follows. I should have created an empty package called ** pkg **, but it contains an empty file called ** \ _ \ _ init \ _ \ _. Py **. This indicates that pkg is a package. image.png

Now, let's create a module in the created ** pkg **. This time I will create two. Right-click on the ** pkg folder ** and create the module files ** sampmodA ** and ** sampmodB **. image.png image.png

Write the following code for each module.

.sampmodA.py


def calc_plus(x, y):
    print('calc in the sampmodA module_The plus function has been called.')
    return x + y

.sampmodB.py


def calc_mult(x, y):
    print('calc in the sampmod B module_The mult function has been called.')
    return x * y

Importing modules in the package

Next, consider importing the modules that are in the package. Generally, specify as follows. (There are several methods) ** [Method 1] **

import package name.Module name

With this method, when calling a function, ** "package name.module name.function name" ** is specified, and the package name and module name are described each time, which complicates the code.

** [Method 2] **

from package name.Module name import Function name (or variable name)

This method allows you to load only certain functions (variables only) from the modules in the package. This allows you to call a function in a module in a package just by specifying the function name, even when executing from another file.

Next, actually import the above two modules in the package. This time, import using the ** [Method 2] ** method.

Enter the following code from the ** Python Console **.

>>> from pkg.sampmodA import calc_plus
>>> from pkg.sampmodB import calc_mult
>>> calc_plus(10, 20)
calc in the sampmodA module_The plus function has been called.
30
>>> calc_mult(10, 20)
calc in the sampmod B module_The mult function has been called.
200

This should confirm that the modules in the package have been imported.

Finally

I mentioned importing packages, but this will also be required when loading external modules (libraries) from packages in the future. This time I created the package myself, then created the module myself and ran it for import, but I rarely create it myself. From now on, I think most of the time I will import modules in external packages. In this section, learn how to import modules in a package.

Return to [Table of Contents Link]

Recommended Posts

[Hikari-Python] Chapter 08-02 Module (Import Package)
[Hikari-Python] Chapter 08-01 Module (Import Module)
Python module import
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)