1. Basic understanding of the module
Why use modules:
Programs are organized at the file level for easier management and reuse of functions
Improve development efficiency
View currently loaded modules:
Print sys.module, which contains the mapping between module names and objects
Aliasing the module:
import temp as tp print(tp.name)
The use of this one: you can choose to use the same function in different modules according to the user's input
inp = input('json or pickle>>>') if inp == 'json': import json as m elif inp == 'pickle': import pickle as m m.dumps({'k':'v'}) m.loads()
This is useful for two: aliasing an already imported module, useful for writing extensible code. Suppose there are two modules xmlreader.py and csvreader.py, both of which define the function read_data(filename): used to read some data from a file, but in different input formats. Code can be written to selectively pick read modules
if file_format == 'xml': import xmlreader as reader elif file_format == 'csv': import csvreader as reader data=reader.read_date(filename)
Import multiple modules in one line (deprecated):
import sys,os,re
Import method:
import--- from---import--- from --- import --- as --- from --- import 1,2,3
from --- import *
What he means is to import all the names in the module that do not start with an underscore into the current location, which is not recommended because it may overwrite the names that have been defined before, and the readability is very poor
Note: If you add _ before the name in the module, then * will not be imported
Module import specification:
First of all, the PEP8 programming specification must be followed.
Each line of import should import a module
. If it is not a necessary requirement, all modules should be imported at the top of the file.
About the order of importing modules, first import built-in, then import extension, and finally import custom
2. Execute the module as a script (very common)
Usage link: http://blog.konghy.cn/2017/04/24/python-entry-program/
#A written Python file is generally divided into two types of scripts: the file is the entire program, used to be executed Module: There are many functions stored in Wenjia, which are used to be imported and used. #The global variable __name__ is built into python when the file is executed as a script, __name__ = __main__ when the file is imported as a module, __name__ = module name #For if __name__ == '__main__': it is used to control the .py file to execute different logic in different application scenarios
3. About pyc files
The module is compiled and stored in the pyc file when it is imported for the first time (it will store the contents of the module in the form of bytes), and the imported one can directly run the pyc file to improve the running speed
4. About the package:
1. Whenever you encounter dots when importing statements, this is all about the import syntax that only packages have.
2. A package is a directory level (folder level), and folders are used to compose py files. The essence of a package is a directory containing an __init__.py file
3. Under py3, even if there is no __init__.py file in the package, import will not report an error. It will be automatically created when it is imported for the first time, and the file must exist under py2, otherwise an error will be reported
4. The purpose of creating a package is to be imported and used, not to run, it is just a form of module
## You can use import to import built-in or third-party modules (already in sys.path), but to avoid using import to import submodules of custom packages (not in sys.path), you should use from---import absolute or relative import
5. Software development specifications:
file(project name) # create it separately under this
File 1: bin # Entry of the entire program
**.py
File 2: conf # configuration file, which contains some file paths: database information
**.this
**.py
File 3: core # core file, where the core code is placed
**.py
File 4: db # file to store data
**.txt
File 5: lib # self-written library, no need to install, just import and use
**.py
File 6: log # log file