Last Updated:

Modules in Python

Modules in Python

A module in Python is a unique feature of the language. A module is nothing more than a set of instructions and functions.

The module contains a certain code that is necessary for some tasks. By default, "out of the box" you install a Python interpreter and install a rich standard library of modules that allows you to interact with a number of areas: system administration, web, network configuration, mathematical calculations and much more.

 

Connecting the module

 

To connect a module, you need to use the import keyword, followed by the name of the desired module.

 

If you decide to connect a module that does not exist or is not yet connected, the interpreter will swear.

Plug-ins can be specified separated by commas or each module on a separate line.

 

To use the functions of a connected module, you need to specify the name of the module, put a period and use the functions (if you use a graphics editor, then press tab to get a list of module functions).

 

Many programmers around the world decided to write their modules to solve their problems. In this regard, there is a wealth of industries in which Python is used.

All third-party modules can be downloaded from the Internet and installed in your interpreter. The only thing that prevents the free installation of all modules is version compatibility. Some modules are written under the 2.x interpreter branch, others under 3.x, but there are also those compatible with both branches.

 

Installing modules

To install a module, you must have pip installed and simply specify the command to install a specific module in it

Installing a module with pip

or download the module and "put" it in the appropriate folder in the interpreter (the folder where the interpreter is installed is -> the Lib folder)

 

Deleting modules

To remove a module, you must have pip installed and simply specify the command to remove a specific module in it

Installing a module with pip

or find the module in the appropriate folder in the interpreter (the folder where the interpreter is installed -> lib folder) and erase it manually.

 

Another name to use the module

Suppose we downloaded two modules that are different in content, but with the same name.

 

If you access the integral module, the question arises: which of the modules will be used? The interpreter has the same question, so you will not get the desired result.

pseudonym (symbol) and the service word as come to the rescue.

First, specify import, then the name of the module, then enter as and alias.
The pseudonym should be spelled out in Latin and, very preferably, had an actual meaning.

 

Retrieving a Specific Function

If we plug in a module, then we load all the functions of the module, and this takes up the computer's memory. The more functions, the more memory is consumed. But that there is only one feature we need, and we are offered a hundred?

In this case, you need to extract this very function and that's it! To extract, you need to specify the service word from, then specify the name of the module, then enter import, and then, separated by a comma, specify all the necessary functions of the module.

Let's extract the function that derives the number Pi from the rich module math.

You can also use aliases.

 

 

Creating a module

Creating your own module is not as difficult as it may seem. It is enough to create a file with the extension .py and connect as a regular module.

Let's create our own module called hello.py, in which the greeting function will be displayed. To do this, create a hello.py file.

 

 

In the same directory (folder), create a file with any name, but the extension .py. note the following lines in the last file

 

You can place your module in the folder with the main program or place it in the Lib directory in the folder with the interpreter.

In such a simple way, we dealt with "modules in Python"!