<Study Notes>Self-study Python--Basic Grammar from Scratch (10) Libraries, Packages and Modules

        In the process of using Python, we often hear concepts such as libraries, packages, modules, etc., so what are these? What is the relationship between them? Let's sort it out today

1. Module

A file with a suffix of .py is called a module, and its English name is Module. Modules allow you to logically organize your Python code segments, and assigning related code into a module can make your code easier to use and easier to understand.

Suppose now there is a file named demo.py, the content of the file is as follows

print('欢迎调用本模块')

str='welcome ,这是demo.py模块'

You can import directly using the import statement. After importing, you can use the module name. Variable name to access this variable

import ceshi3

print(demo.str)



#输出:

PS C:\coding\aNewPy> & C:/Users/admin/anaconda3/python.exe c:/coding/aNewPy/demo.py
欢迎调用本模块
welcome ,这是demo.py模块

2 bags

In earlier versions of Python (before Python 3.3), if there is an __init__.py file in a folder, we call it a package, the English name is Package.

In later versions of Python (starting from Python 3.3), there is no such requirement. As long as it is a folder, it can be regarded as a package. We call it a space-named package. In order to distinguish it, I call the above package a traditional package. .

The __init__.py in the traditional package can be an empty file, but it must have this file. It is the iconic file of the package, and some package initialization can be performed in it if necessary.

demo
 __init__.py
 bar.py
 foo.py

There can be multiple modules in one package, for example, both foo.py and bar.py above belong to the demo module. If you want to use these modules, you need to import them like this

import demo.bar
import demo.foo

or

from demo import bar
from demo import foo

3. Library

A Python library refers to a collection of codes with certain functions, and is generally considered to be a complete project package.

Library->package->module is a hierarchical relationship from big to small!

Libraries: A library may consist of multiple packages and modules

Package: A package may consist of multiple modules

Module: A collection of functions, classes, and variables

Guess you like

Origin blog.csdn.net/qq_41597915/article/details/126336423