Article Directory
Modules in Python
There is a concept in Python called a module.
To put it plainly: a module is like a toolkit. To use the tools (like functions) in this toolkit, you need to import this module
For example, we often use the tool random, which is a module. After using import random
the import tool, the function of random can be used.
import module
1. There are five ways to import modules
import 模块名
from 模块名 import 功能名
from 模块名 import *
import 模块名 as 别名
from 模块名 import 功能名 as 别名
2. import
Use keywords to import a certain module in Python import
. For example, to import a system module math
, it can import math
be used .
grammar:
import 模块1,模块2,... # 导入方式
模块名.函数名() # 使用模块里的函数
think about it:
Why must add the module name call?
answer:
Because there may be such a situation: there are functions with the same name in multiple modules. At this time, if it is only called by the function name, the interpreter cannot know which function to call. So if you import a module like the above, you must add the module name to the calling function
Example:
import math
#这样才能正确输出结果
print(math.sqrt(2))
#这样会报错
print(sqrt(2))
3. from…import
Sometimes we only need to use a certain function in the module, just need to import the function, at this time we can use the following method to achieve:
from 模块名 import 函数名1,函数名2....
Not only can you introduce functions, but you can also introduce some global variables, classes, etc.
Notice:
When importing in this way, only the function name can be given when calling the function, and the module name cannot be given. However, when two modules contain functions with the same name, the latter import will overwrite the previous import. That is to say, if there is a function in module A function()
, there is also a function in module B. function()
If the function in A is introduced function
first and the function in B is function
introduced later, then when the function is called , the function function
in module B is executed .function
For example, to import a function fib
of a module fibonacci
, use the following statement:
from fib import fibonacci
Note: It will not fib
import the entire module into the current namespace, it will only import a single function fib
infibonacci
4. from … import *
It is also possible to import all the contents of a module into the current namespace, just use the following declaration:
from modname import *
NOTE: This provides an easy way to import all projects in a module. However, this statement should not be used too much.
5. as
Alias
import time as tt # 导入模块时设置别名为 tt
tt.sleep(1) # 使用别名才能调用方法
from time import sleep as sp # 导入方法时设置别名
sp(1) # 使用别名才能调用方法
When a module is very long and we only need one of the functions, but the function name is relatively long or not intuitive enough, then we can use
as
keywords to set aliases for the functions to make the function calls more convenient and concise.
For example, if we import the module numpy
and use it import numpy as np
, then we can np.array()
call the function numpy
in the module by array
.
in conclusion
- When importing
import
the entire module, you need to use模块名.函数名()
the method to call the function in the module. - Use
from 模块名 import 函数名
can directly use the function name to call the function in the module. - Use
from 模块名 import *
all functions in a module that can be imported, but are deprecated. - Use
import 模块名 as 别名
can set an alias for the module, use别名.函数名()
to call the function in the module. - You
from 模块名 import 函数名 as 别名
can set an alias for the function and use the alias to call the function.
These methods of importing modules can be selected according to actual needs, and flexible use can make our code clearer, more concise and easier to read.
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model