When you are writing a code and you make some variables or functions, you can’t use them once you get out of the file. In order to fix this, python3 gives us the option to use modules. Modules are very useful, for example, when we’re make an extremely big program we tend to separate the code in more than 1 files so its easier to debug or to improve later. So maybe we have a piece of code that includes variables and functions we wan’t to use in other pieces of code and it’ll have a lot of disadvantajes to copy and paste in each file the same code. So this is where modules come, we set a piece of code as a module and we call it in every programm so we dont have to copy-paste everything.

Lets take the math module as an example. When we want to get a square root of a number we have to call the module first, and we call it like this:

import math

once we’ve done that we can call the function:

sqrt.math()

As i said at the beginning we can create modules too. To create a module we just mae a python file, and to call the functions inside of it we just type at the beginning of the file:

import xxxxx

We can see how when we import modules we created must not put the last ‘.py’ because it wont run.

This is how you call and create modules.

CC BY 4.0 How to create and how to call modules by fersabal is licensed under a Creative Commons Attribution 4.0 International License.