Home >Backend Development >Python Tutorial >How to import modules in python
Python module (Module) is a Python file, ending with .py, containing Python object definitions and Python statements. Modules allow you to organize your Python code snippets logically.
Assigning related code to a module can make your code more usable and easier to understand. Modules can define functions, classes and variables, and modules can also contain executable code.
Module Import
After the module is defined, we can use the import statement to import the module. The syntax is as follows: (Recommended learning: Python video tutorial)
import module1[, module2[,... moduleN]]
For example, if you want to reference the module math, you can use import math at the beginning of the file to introduce it. When calling functions in the math module, they must be quoted like this:
模块名.函数名
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 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:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 导入模块 import support # 现在可以调用模块里包含的函数了 support.print_func("Runoob")
The output result of the above example:
Hello : Runoob
A module will only be imported once, no matter you How many times the import has been executed. This prevents imported modules from being executed over and over again.
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to import modules in python. For more information, please follow other related articles on the PHP Chinese website!