Modules and packages in Python

Module

A module is a toolkit. If you want to use the tools (such as functions) in this toolkit, you must import this module.
Modules are very simple Python files. A single Python file is a module, and two modules are two Python files.

Import of modules

import module name 1, module name 2
import module name. function name
from module name import function name/variable name
import long module name as short module name
from module name as import *

Question 1

Why is it necessary to add the module name to call?
import module name. function name
Because there may be a situation where multiple modules contain functions with the same name. If only the function name is used to call it, the interpreter cannot know which function to call. So when you import a module like the above, you must add the module name to the calling function

Question 2

If only a certain function/variable/class in the module is needed, how to import it?
from module name import function/variable name
When imported in this way, only the function name can be given when calling the function, but the module name cannot be given, but when two modules contain functions with the same name, the latter import will overwrite the previous import

Question 3

What should I do if the imported module name/function name/variable name is too long?
import long module name as short module name
from module name import long function name as short function name

Question 4

What if I want to import all the contents of a module?
from module name import *
This provides a simple way to import all items in a module. However, this statement should not be used too much

Question 5

What is the function of __all__ in the module?
If there is an all variable in a file, it means that there are no elements in this variable and will not be imported by the from module name import *

Question 6

What does __name__ ==' main ' in the module do?
Determine whether the current code is imported by other code, if not, execute the content inside

package

The package organizes related modules together to effectively avoid the problem of module name conflicts and make the application organization structure clearer

Package production and import

Make the framework of
Insert picture description here
the package Write custom content in the package
Insert picture description here
Insert picture description here

Import
Insert picture description here

Use package
Insert picture description here
Run result
Insert picture description here

Automatically send mail code

Need to install yagmil module and schedule module

import yagmail
def send_mail(sender='[email protected]',password=None,receivers=None,subject='自动化发送邮件',
              contents=None,attaches=None,host='smtp.qq.com'):
    """..."""
    try:
        # host 是smtp服务器,QQ邮箱:smtp.qq.com 163邮箱: smtp.163.com
        mail = yagmail.SMTP(user=sender,password=password,host=host)
        # 真正发送邮件
        mail.send(to=receivers,subject=subject,contents=contents,attachments=attaches)
    except Exception as e:
        print('发送邮件失败,失败原因是:', e)
    else:
        print('[主题:%s]邮件发送成功' %(subject))
if __name__ == '__main__':
    import schedule
    # 授权码
    password = 'rfwethjddustecbf'
    friends = ['******@qq.com']
    subject = ['邮件报警,磁盘已使用90%']
    # h1是一级标题,style='color:red'红色显示
    contents = ['邮件告警',"<hi style='color:red'>磁盘已使用90%</h1>"]
    attaches = ['bao.py']
    # 定时任务,为了测试,每隔2秒执行一次
    schedule.every(1).seconds.do(send_mail,password=password,
                                 receivers=friends,
                                 subject=subject,
                                 contents=contents,
                                 attaches=attaches)
    while True:
        schedule.run_pending()

Guess you like

Origin blog.csdn.net/qq_42958401/article/details/109056758