python (module programming, creating guide package, custom module and guide)

Use the extra time to learn more python content, these are the content of online learning, I hope to help everyone, the same is to supervise myself and continue to work hard.

Commonly used modules (can be thought of as files) and packages (can be thought of as folders) projects (can be understood as a disk):
modules: improve code maintainability, avoid function names, variable name name conflicts)

Module classification: built-in standard modules (also called the standard library is estimated to be more than 300), third-party open source modules (downloaded on pypi.org), custom modules (new modules based on your own needs).
Insert picture description here

Module call: Once the module is called, it is equivalent to opening another py file.

import  requests #直接导用requests
requests.模块名称 #使用部分模块
from os import execl#导人os模块execl部分
from bs4 import BeautifulSoup  as tist#导入模块下部分进行重命名
from random import  * #导入模块下所以方法,直接使用模块(不建议使用,调用相同名称容量产生bug)

The use of
custom modules: custom modules:

def name():
    print('我的名字')
print('检测模块调用就是打开文件')

Call the custom module:

import tesst#导用自定义模块名称
print(
tesst
)
print(tesst.name())#使用模块下方法

phenomenon:
Insert picture description here

Custom modules cannot be searched across files, because the python interpreter has its own search management. If it cannot be found, it will be discarded and the reply will not be found. (If you want to find it, you must be in the interpreter reading directory.
See python explanation Search module path:


import sys
print(sys.path) #模块查找路径


'''小编查看的路径
现象:
['D:\\pr\\pythonProject', #第一条为运行的同级目录
'D:\\pr\\pythonProject',
 'D:\\pr\\pythonProject\\venv\\Scripts\\python36.zip', 
 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python36\\lib', 
 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python36', 
 'D:\\pr\\pythonProject\\venv', 
 'D:\\pr\\pythonProject\\venv\\lib\\site-packages',
  'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
'''

Call custom function methods across files (method one, static)

import sys
print(sys.path) #模块查找路径
name='跨文件运行的目录路径(第一条为运行的同级目录)'
sys.path.append(name)#加入到解释器当中去

Package usage:

Create package: Rename Project...Create a package.
Insert picture description here

Create a subpackage:
Insert picture description here

The package calls directly:
a1:

print("haha")

Call on name:
Insert picture description here

phenomenon:
Insert picture description here

Insert picture description here

__init__ type file: 1, which means it is a package 2. When importing the package, the __init__ file must be executed first:

Guess you like

Origin blog.csdn.net/weixin_47514459/article/details/112135606