Python Module (Module) is a Python file that ends in .py and contains Python object definitions and Python statements.The
module allows you to logically organize your Python code snippets.
Assigning the relevant code to a module will make your code better and easier to understand.
Modules can define functions, classes, and variables, and modules can also contain executable code.
The following example is a simple module support.py:
Introduction of modules
After the module is defined, we can use the import statement to introduce the module with the following syntax:
import module1[,< /span> module2[,... moduleN]]
For example, to reference the module math, you can use import math at the beginning of the file to introduce it. When calling a function in the math module, it must be referenced like this:
module name.function name
When the interpreter encounters an import statement, the module will be imported if it is in the current search path.
The search path is a list of all the directories that the interpreter will search first. If you want to import the module support.py, you need to put the command at the top of the script:
test.py file code:
The above example output:
Hello : Runoob
A module will only be imported once, no matter how many times you perform an import. This prevents the import module from being executed over and over again.
Python's from statement lets you import a specific part from the module into the current namespace. The syntax is as follows:
from modname import Span> name1[, name2 [, ... nameN]]
For example, to import the fibonacci function of the module fib, use the following statement:
from fib import Span> fibonacci
This declaration does not import the entire fib module into the current namespace. It only introduces fibonacci in the fib to the global symbol table of the module that executes the declaration.