Python modularization and installation of third-party modules (summary sharing)

WBOY
Release: 2022-05-16 17:54:10
forward
2739 people have browsed it

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.

Python modularization and installation of third-party modules (summary sharing)

Recommended learning: python video tutorial

Modular programming

1. What are modules? ?

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

2. Custom module

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))
Copy after login

Python modularization and installation of third-party modules (summary sharing)
Second type

#第二种导入方式 只导入模块中指定的一部分
from math import pi
print(pi)
print(pow(2,3))
from math import pow
print(pow(2,3))
Copy after login

Similarly, the same method is used to import modules written by yourself.

3. Execute in the form of the main program

Sometimes 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
Copy after login

Only when this module is running as the main program, Pass

4.Package in python

Package: 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 name

import pagekge.module_A
print(pagekge.moudle_A.a)#调用一个包里的一个模块的一个属性或函数
import pagekge.module_A as mm #取小名
print(mm.a)
Copy after login

Summary 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

5. Commonly used built-in modules in Python

Python modularization and installation of third-party modules (summary sharing)

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()) #读取百度网址
Copy after login
6 .Installation and use of third-party modules

The power of Python cannot be separated from third-party modules. There are many third-party modules written by many people to implement many functions, and we only need to install them to use them.

Installation

pip install module name

Online installation method

Take the installation of schedule module as an example :


The first step: Windows logo key R brings up the window, enter cmd, click OK
Python modularization and installation of third-party modules (summary sharing)
The second step: Enter pip install the module to be installed Name, press Enter
Python modularization and installation of third-party modules (summary sharing)
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!
Python modularization and installation of third-party modules (summary sharing)

Use

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
Recommended learning:

python video tutorial

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!