This article brings you relevant knowledge about python, which mainly introduces issues related to modular programming and third-party module installation, and also includes packages in Python, etc. Let’s take a look at it together, I hope it will be helpful to everyone.
Recommended learning: python video tutorial
The relationship between modules and functionsA module can contain N multiple functions;
In Python, a file with the extension .py is a module;
Modules contain classes, functions and statements;
A program project consists of N modules;
Benefits of using modules :
Convenient for import and use by other programs and scripts
Avoid conflicts between function names and variable names (two modules have the same variable names without conflict)
Improve the maintainability of the code
Improve Code reusability
Create a module:
Create a new .py file, and the name should not be the same as the standard module name that comes with Python Same
Import module:
The first type: import module name [as alias]
The second type: from Module name import function/variable/class
##First type
#第一种导入方式 import math print(id(math)) print(type(math)) print(math) print(dir(math)) #查看math中的属性方法 print('--------------------------------------') print(math.pi) print(math.pow(2,5)) print(math.ceil(9.01)) print(math.floor(9.999))
Second type
#第二种导入方式 只导入模块中指定的一部分 from math import pi print(pi) print(pow(2,3)) from math import pow print(pow(2,3))
Similarly, the same method is used to import modules written by yourself.
3. Execute in the form of the main programSometimes when we call the content of other modules, some statements will be output in the definitions of other modules, resulting in our When the current module outputs content, the output content of the calling module will also be output. At this time, we can add conditions before the output of the called module:if __name__=='__main__': pass
Only when this module is running as the main program, Pass
4.Package in pythonPackage: is a hierarchical directory structure that organizes a group of modules with similar functions together.
Function: Code standardization, avoid module name conflicts (module names of different modules in different packages can be the same)
The difference between packages and directories: Contains __init__ The directory of the .py file is called a package
The directory usually does not contain the __init__.py file
Import of the package:
import package name.module nameimport pagekge.module_A print(pagekge.moudle_A.a)#调用一个包里的一个模块的一个属性或函数 import pagekge.module_A as mm #取小名 print(mm.a)Copy after login
5. Commonly used built-in modules in PythonSummary Note:
Using the import method starting with import can only import the package name or module name
Use from …The import import method can import packages, modules, functions, and variables
import sys print(sys.getsizeof(24)) print(sys.getsizeof(99)) print(sys.getsizeof(True)) print(sys.getsizeof(False)) import time print(time.time()) #输出秒 print(time.localtime(time.time())) #输出当前时间 import urllib.request #与爬虫有关的 print(urllib.request.urlopen('http://www.baidu.com').read()) #读取百度网址
Online installation methodTake the installation of schedule module as an example :pip install module name
The first step: Windows logo key R brings up the window, enter cmd, click OK
The second step: Enter pip install the module to be installed Name, press Enter
Step 3: Enter python, press Enter to enter the python interactive program, enter the import module name, press Enter, if no error is reported, install it success!
Recommended learning:import module name
import scheduleimport timedef job(): print('666666')schedule.every(3).seconds.do(job)while True: schedule.run_pending() #检测job的时间到了没有 time.sleep(1)Copy after login
The above is the detailed content of Python modularization and installation of third-party modules (summary sharing). For more information, please follow other related articles on the PHP Chinese website!