Python module (1), python module (
During the development of computer programs, as the program code is written more and more, the code in a file will become longer and longer, making it increasingly difficult to maintain.
In order to write maintainable code, we divide many function groups into different files, so that each file contains less code, many programming languages use this method to organize code. In Python, A. py file is called a Module ). For example, OS is a system-related module, and file is a file-related module.
The biggest benefit of the module is that it greatly improves the maintainability of the Code. Second, you do not have to write the code from scratch. When a module is compiled, it can be referenced elsewhere.
There are three modules:
- Custom Module
- Third-party module
- Built-in modules
Import Module
The module import method is as follows:
1 import module2 from module import xx3 from module. xx import yy as [rename] 4 from module import * 5 _ import _ ('module') # import the module as a string
View the path of The py Module
1 import sys 2 print (sys. path) 3 4 # result 5 ['e: \ python \ coding \ day8', # path of the current python file 6'e: \ python \ pyInstall \ py3 \ python36.zip ', 7'e: \ python \ pyInstall \ py3 \ DLLs', 8'e: \ python \ pyInstall \ py3 \ lib ', 9 'e: \ python \ pyInstall \ py3', 10'e: \ python \ pyInstall \ py3 \ lib \ site-packages ']
Custom Module
If the python module path does not contain the package to be imported, you can use sys. path. append ('path') to add the module path.
1 import sys2 import os3 dirr = os.path.dirname(os.path.abspath("../day6/fs1.py"))4 sys.path.append(dirr)
Third-party module
Python has many third-party modules, which provide better performance than built-in modules.
pip3 install [module_name]
You can use the preceding command to install it in the operating system command line.