The Python programming language has many built-in functions. However, they are not enough to solve all kinds of problems, so programmers add tools by plugging in modules.
Table of Contents
What is a module
It is a separate file that contains some code. Any script written by a Python 3 programmer can be called a module. It can be either executable or pluggable. An executable module contains code that performs some actions on its own, while a plugin is a set of functions, classes and objects that can be used to solve problems in another program. The division of programs into modules has a number of advantages:
- The program has a clear structure that allows the programmer to easily navigate through thousands of lines of code.
- They allow to use the same names for functions, i.e., they do not allow overlapping of names.
- The programmer does not need to load the script with a large number of functions, most of which are not used, it is enough to include only the necessary modules or their parts.
Executable and plug-in
You can write a script that will both perform some actions (program) and be imported into other modules (library). It is important to design it properly:
- All executable code is placed in the
main()
function. - The
main()
function is called after the condition:if __name__ == '__main__': main()
Plugging in the module
This can be done in different ways, the choice depends only on the needs and desires of the programmer. Modules in Python are files with the “.py” extension. When importing, the extension is omitted, the interpreter already knows that the import command is followed by the module name. The programmer may easily plug in any module that is in the standard Python 3 library. To connect specific custom tools, they must first be downloaded. Usually the pip package manager is used for this.
The usual connection is import
Connect modules preferably at the very top of the script, using the keyword “import”, e.g. import random
. Once plugged, the program gets access to all functions, methods and classes contained in it. The programmer can call any function from the connected library using the prefix “module_name.
“. Example: random.randint(1,15)
where random
is the library that we connected, and randint
is the name of the function that is described in it. This method does not allow crossing of names, i.e. the programmer can use the same function name in the script, exactly the same as in the plugged library and not be afraid that after plugging it the function will be overridden. Here is a complete example of using the import instruction in Python 3:
import random a = random.randint(1, 15) print(a)
Using aliases – as
Some modules have long and awkward names. For convenience and to reduce the amount of code, the programmer can replace it with his own. For example, if you write “import random as rand
” then instead of a long random
you can use a short rand
to refer to the library functions.
Import components – from
In order not to clutter the program with a large number of unused tools, you can connect not the whole module, but some part of it. For example, a programmer wants to use only one function from the math
library. If he includes the whole library, the script will add over 40 functions that will take up space. To add some part to the project, use the from
keyword:
from <insert module name> import <function name>
This way, the main script accesses only a certain function. In addition, this connection does not use a prefix when calling functions from the plugin. It is important not to forget about it to avoid naming conflicts. You can connect multiple functions in one line. To do this, they must be listed separated by commas. If you write an asterisk “*” after import, all the contents of the module will be included. This is considered bad form because it will cause names from the main script to be identical to those in the plugin. But if the programmer is sure that he used unique names for functions and variables, he can theoretically use this method.
Reloading the library
A module can only be imported once per session. If the programmer, after importing, changes something in the file that imported and then imports it again, the main program will not see these changes. This is because the library is cached when it is imported; when they try to import it again, the Python interpreter simply uses the cached copy. Example:
import my_name, time print(my_name.name) # will print Sergei time.sleep(10) # pause during which we change # name value in my_name.py # from Sergei to Alexander import my_name print(my_name.name) # will print Sergei
If you still need to reload the module, the reload()
function from the standard importlib library comes in handy. Reloading has no effect on objects referencing the imported module and allows to implement a dynamic reloading of program components. Here is an example:
from importlib import reload import my_name, time print(my_name.name) # will print time.sleep(10)# change value from Sergey to Alexander my_name = reload(my_name) print(my_name.name) # outputs Alexander
Connecting from another folder
Libraries are plugged in very easily when the Python interpreter knows where to look for them. Python looks for modules:
- In folder of executable script.
- In built-in modules.
- In directories defined in sys.path (also contains PYTHONPATH variable).
To import a module from another folder into Python 3, you can do the following:
- Add the path to the module folder using the sys library command
import syspath.insert(0, "/path/module folder")import module
- Or move the module to a folder defined in the search paths of the Python interpreter.
To make the directory containing the files be defined as a package, you must add the file __init__.py to it. It shows the Python interpreter that the folder is a module package. Since Python 3.3 you no longer need to add __init__.py to the directory, the Python interpreter treats all folders as packages.