In the process of using Python, I often get confused by the concepts of modules, libraries, packages, and frameworks. Today, we will take time to sort out these concepts.
A module is a file with a .py suffix in which constants and functions are defined. The name of a module is the name of that .py file. The name of the module is taken as a global variable
__name__ that can be fetched or imported by other modules.
The import of modules is done through ipmort and the modules are imported as follows.
Note: If the system comes with a sys module, your own module should not be named sys.py, otherwise you will not be able to import the sys module that comes with the system. Check this by executing import abc in Python interactive environment, if it succeeds, it means the system has abc module.
Packages embody the idea of structured management of modules. Packages are composed of module files, and a number of module files with related functions are structured and combined to form a package. From the programming development point of view, two developers A and B by may have given the same name to module files developed by each and with different functions. If a third developer imports a module by name, there is no way to identify which module has been imported. For this reason, developers A and B can build a package, put the module in the package folder, and specify the module by “package. Module Name” to specify the module.
A package is a folder-like thing that contains many .py files and an
__init__.py is something that describes how many modules there are. You can think of a package as something that accomplishes a set of functions.
According to the current PEP 420 proposal, there are actually two kinds of packages in Python today: regular packages and namespace packages.
- Regular packages: have existed since before Python 3.2, and are usually presented as a directory containing a py file. This
__init__.pyfile is executed implicitly when the package is imported.
- Namespace packages: As defined in PEP 420, a namespace package is composed of multiple portions –portion is similar to a child package under a parent package, but they are not necessarily physically located next to each other, and they may behave as files in .zip, files on the network, and so on. A namespace package does not require a py file; Python creates a namespace package for the top-level portion whenever it itself or a child package (i.e., a portion) is imported – so a namespace package does not necessarily correspond directly to an object in the filesystem; it can be a virtual module.
Note that Python packages are actually special modules: you can see this by looking at globals() after importing a package; in fact, any object with the
__path__ attribute is treated as a package by Python.
Libraries in Python are a concept borrowed from other programming languages, with no particular specific definition; Python libraries emphasize their functionality. In Python, modules and packages that have some functionality can be called libraries. Modules are composed of many functions, packages are composed of many modules institutionalized, and libraries can also contain packages, modules, and functions.
Referring to other programming languages, it means a collection of code in Python that accomplishes a certain function for the user to use as a combination of code.
A library is a collection of modules with related functionality. This is one of the great features of Python, namely having a powerful standard library, third-party libraries, and custom modules.
- Standard libraries: those modules that come with Python
- Third-party libraries: Modules with specific functionality that are published by other third-party organizations.
- Custom modules: Users can write their own modules and use them.
A framework is a collection of Python libraries. Frameworks are similar to libraries, in terms of functionality, frameworks often integrate the functionality of a variety of libraries, frameworks are used to assist in the development of a certain area of functionality of a package, and generally contain multiple sub-packages. The framework will facilitate the development of a certain type of project must be implemented in the code directly, you only need to focus on your project with a different part. Such as crawler framework scrapy, web development framework Django and flask, big data framework pyspark and so on.