[Hikari-Python] Chapter 08-01 Module (Import Module)

[Python] Chapter 08-01 Importing Modules

I made my own original function in Chapter 06. You can have other people use these self-made functions. You can combine multiple functions you created into one file, and this file is called a ** module (part) **.

In fact, various modules created by Python programmers around the world are published on the Internet. These modules are free to use and are used by ** import **.

There are various publicly available modules such as modules used in AI and IoT, and modules used in game creation and data analysis.

In this chapter, we will first explain how to import modules that you have created yourself, and then import modules that have been created on the Internet.

Import your own module

Let's actually create some self-made functions and save them as modules. First, create a module, but do not create chap08 </ font> yet, but create it directly under ** [python] ** below. (Right-click [python] as shown below) image.png

Then save it with the file name sampmod08_01 </ font>. (It is sampmod08_01. It is not sampmod08-01. → It cannot be read by using a hyphen in the module file name.)

.sampmod08_01.py


def my_func():
    print('I am running from a function in the module.')

def calc_func(x, y):
    return x**2 + y

my_func()
print(calc_func(3, 3))

Next, create a new chap08 </ font>, and in it, the file name samp08-01-01.py </ font> Create a file with and write the following code. Then, execute samp08-01-01.py </ font>.

.samp08-01-01.py


import sampmod08_01

[Execution result] </ font> I am running from a function in the module. 12

The ** import statement ** loads your own module ** sampmod08_01.py ** and executes it.

This can also be done with the ** Python Console **. Enter the following code from the ** Python Console **.

>>> import sampmod08_01
I am running from a function in the module.
12

You can see that the same result is output even if you import it in the Python Console. * </ font> After executing in Python Console, be sure to click the following to stop it. (Because it remains imported) image.png image.png

Call the imported function

Now let's change the module a little. As before, create the file name sampmod08_02 </ font> directly under ** [python] **. Then enter the code below.

.sampmod08_02.py


def my_func():
    print('I am running from a function in the module.')

def calc_func(x, y):
    return x**2 + y

This time, I haven't created the calling function in the module. Consider this ** calling a function from outside the module **. Create a file with the file name samp08-01-02.py </ font> in chap08 </ font>, and use the following code Please write. Then run samp08-01-02.py </ font>.

.samp08-01-02.py


import sampmod08_02

sampmod08_02.my_func()
print(sampmod08_02.calc_func(3, 3))

[Execution result] </ font> I am running from a function in the module. 12

This time, there is no caller in the module, but in ** samp08-01-02.py **. When calling a function in another module

Module name to call.Function in the module to call

It can be called in the format of.

How to alias a module

Earlier, when calling a function in another module, I called it as ** sampmod08_02.my_func () ** or ** sampmod08_02.calc_func (3, 3) **, but the module name is long and it takes time to input. It will take.

A common method in such cases is to use ** as ** to specify an alias.

import Module name to call as Alias module

Create a file with the file name samp08-01-03.py </ font> in chap08 </ font>, and use the following code Please write. Then run samp08-01-03.py </ font>.

.samp08-01-03.py


import sampmod08_02 as sm0802

sm0802.my_func()
print(sm0802.calc_func(3, 3))

[Execution result] </ font> I am running from a function in the module. 12

By specifying the module alias, I think the module name is a little shorter. Often, when reading from outside for use in AI, you often specify an alias.

Also, if you look closely, you're reusing the previous module. You can see how the modules are being reused without having to create new modules one by one. This is also the advantage of the module.

Import by from

After importing, I found that I could execute functions in other modules. I also touched on specifying aliases. However, it is difficult to specify the module name (alias) such as ** sm0802.my_func () ** anyway.

So you can solve that by using from when importing. Generally, specify as follows.

from module name import function name 1,Function name 2 ...

Let's actually write it. Let's import the sampmod08_02 </ font> from earlier. This time I will run it from ** Python Console **.

>>> from sampmod08_02 import my_func
>>> my_func()
I am running from a function in the module.
>>> sampmod08_02.calc_func(3, 10)
19

First, ** from ** loads the module name, and ** import ** loads the ** my_func function **. By doing so, you can call the function only with ** my_func () ** without specifying ** sampmod08_02.my_func () **.

Note that ** calc_func ** is not specified in the place of ** import **, so you need to set ** sampmod08_02.calc_func (3, 10) ** to call it.

About \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _'

I haven't seen this ** \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _'** in practice, but maybe it will be a question in the Fundamental Information Technology Engineer Examination. I've written it in the sense that you shouldn't panic if you look at this code.

First, the following program written in ** samp08-01-01.py **,

.sampmod08_01.py


def my_func():
    print('I am running from a function in the module.')

def calc_func(x, y):
    return x**2 + y

my_func()
print(calc_func(3, 3))

I have confirmed that when I import this module, it runs as follows.

>>> import sampmod08_01
I am running from a function in the module.
12

However, for ** operation check etc., it is possible to prevent the function in the module from being called even if it is imported **. Conversely, you can have it output when you run the module itself. Create the file name sampmod08_03 </ font> directly under ** [python] **. Then enter the code below.

sampmod08_03.py


def my_func():
    print('I am running from a function in the module.')


def calc_func(x, y):
    return x**2 + y


if __name__ == '__main__':
    my_func()
    print(calc_func(3, 4))

Try importing this into the ** Python Console **. Enter the following code from the ** Python Console **. (You can probably see that nothing is output.)

>>> import sampmod08_03

This is done by writing the condition ** \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _'** in the if statement so that the function will not be called suddenly when importing. You can go through.

To execute this function, you need to specify the function in the module imported by ** Python Console ** and execute it. ..

>>> import sampmod08_03
>>> sampmod08_03.my_func()
I am running from a function in the module.
>>> sampmod08_03.calc_func(3,5)
14

Also, if you execute ** sampmod08_03.py ** earlier, the output will be as follows. Since this is executed with the module called sampmod08_03.py as the main, it is possible to call the function in the if statement. (In the execution result, you can see that the result of the calc_func function is different.)

[Execution result] </ font> I am running from a function in the module. 13

Finally

This time I mentioned importing a module I created. From now on, module import will always come out as a matter of course when creating AI and IoT related programs. I haven't touched on loading external modules and libraries (described later) yet, but I will load them in the same import format, so let's hold it down for the future.

Return to [Table of Contents Link]