Quickly understand the connections and differences between Python modules, packages, libraries, and frameworks

When learning Python, you will often see the terms **"module Module", "package Package", "library Library" and "framework Framework"**, only if you understand what they are and what is the relationship between them , Only then can we truly understand the beauty of the program.
insert image description here

1. Module Module

insert image description here
A module is a collection of variables, arrays, functions, and classes . In addition, a module is a file with a suffix of .py, which is used to represent a part of the program, and the name of the module is the name of the .py file. The name of the module **as a value of the global variable __name__** can be obtained or imported by other modules.

The import of the module is realized through ipmort. The way to import the module is as follows:

import <moduleName>
from <moduleName> import <functionName>

2. Package

insert image description here
A package is a collection of several modules . The package embodies the structured management idea of ​​the module. The package is composed of the module files, and many module files with related functions are structurally combined to form a package.

When creating a package, first place the modules you want to package in a directory. In that directory, create a new special file called __init__.py. This is just a single Python file because it has a .py extension. However, with such a file named init.py, the files in this directory are treated as a single package.

# 目录结构
.
├── creatures
│   ├── __init__.py
│   ├── character.py
│   └── moster.py
└── magic
    ├── __init__.py
    └── magic.py

From the perspective of programming development, two developers A and B may have taken the same name for the module files developed by them and with different functions. If a third developer imports a module by name, there is no way to tell which module was imported. To this end, developers A and B can build a package, put the module under the package folder, and specify the module by "package.module name".

Import example:

import <packageName.moduleName>

3. Library


A library is a collection of several packages . There is no specific definition of a library in Python, and its functionality is emphasized. Modules and packages with certain functions can be called libraries. Libraries can also contain packages, modules, and functions.
The most powerful feature of python is that you can use a large number of powerful libraries, including the following:

  • Standard library: the modules that come with Python
  • Third-party library: It is a module with specific functions released by other third-party organizations.
  • Custom modules: Users can write modules by themselves and use them.
    insert image description here
    insert image description here
# 举个例子
import numpy as np

data = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr = np.array(data) 

print(arr)
print(arr.ndim) 
print(arr.shape) 

4. Framework Framework

A framework is a collection of Python libraries .
A framework is similar to a library. From a functional point of view, a framework often integrates the functions of various libraries. A framework is a package used to assist in the development of functions in a certain field. Generally, the package also contains multiple sub-packages.
Such as crawler framework scrapy, web development framework Django and flask, big data framework pyspark, etc.
Original address:

https://blog.csdn.net/qingfengxd1/article/details/118880204
https://blog.csdn.net/weixin_44801979/article/details/126225780?ops_request_misc=&request_id=&biz_id=102&utm_term=python%20%E6%A8%A1%E5%9D%97%20%E5%8C%85%20%E5%BA%93%20%E7%B1%BB&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-2-126225780.nonecase&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/weixin_43555555/article/details/129663708