1. Operation Process
The Python source program is run, not directly to the machine to run, but first to the Python virtual machine (through the C language), and then interpreted by the Python virtual machine as a machine-readable instruction to run;--so Python is an interpreted or dynamic language
The role of 2..PYC files
The. pyc file is a Python-aware bytecode file generated by the Python source program when interacting with a Python virtual machine, and then the Python virtual machine interprets it as a machine-readable instruction; ——. The PYc file is a Python virtual machine-readable bytecode file that is translated into Python source code
3. Why does the Python program run directly do not see the. pyc file, and the import module will see the generated. pyc file?
Here comes a caching mechanism, Directly running the Python program will also generate. pyc files, but not save, Python virtual machine will directly run the program as a long-running program, do not need to be called frequently, and import the module is considered to be frequently called, so when the module is imported, the next time you import the module, you will see if there is. P YC file, if any, import the. pyc file directly, or if not, transcode the module to a bytecode file PYc, and then import the bytecode file. This will involve the problem of module modification, if the module is modified, the virtual opportunity automatically detects the modification time of the module program, and if it is modified, the bytecode file is regenerated.
How Python Works