We know that the use of functions not only reduces the workload, but also makes the code more concise and easier to maintain. But if in another
A file, we want to use a function defined in the previous file, what should we do? We need to weigh
Do you want to implement the previous function again? And when we're implementing a very complex feature, even if this feature
Broken down into smaller functions, the required code is still large, causing the entire file to be difficult to maintain. So is there a better
How is it implemented?
The answer is, of course, that we can classify different functions into separate
. PYFile and use it when needed.
This method is used in many programming languages, except for the different names.
In Python, each of these. py files is called
Modular module.
Benefits of using modulesSimilar to the use of functions,
Make your code more concise and easier to maintain。 And one of the big advantages of Python
are huge third-party modules that provide a lot of powerful functionality, and when we need these features, we only need to
Importing these modules allows you to use them directly, without having to implement them yourself, reducing the amount of work that is being developed. To avoid naming conflicts between modules, we introduce packages
PackageThe various modules are classified into individual
package, organize each module by directory. Such as: The module temp.py into the package Test, at this time the module temp name
It becomes test.temp. How do I install a package? Before using the package, we need to make sure that it is already installed and that in Python3, the package is
With PIP3, the specific syntax is as follows:
PIP3 Install package nameOnce the installation is complete, we can
Import third-party packagesand use them in the following ways:
Import Name
Import the package name and invoke the function or variable in name through Name.sth
import Name as Mn
Import the package Name and set an alias of Mn for it
Note: At this point, we can use Mn.sth to invoke the function or variable in name sometimes we just need a feature in the package, we can
simply import this feature without having to import the entire package:
Form Name Import Sth
From Name import sth as Sth
Installation and use of Python modules