i. pyc files
We develop a python script with a file extension of .py. If we run this py file, Python internally compiles the source code file (.py file) into a byte code file (.pyc file) first. The compiled byte code is then run, and the result of this run is output on the console. To summarize, pyc is an executable compiled py file, whose feature is that at runtime, it saves the python compiler from having to compile it again, which in turn optimizes performance.
II. How to compile?
2.1. Source code compilation
Writing a Python script and using the code to compile it is a good way for those who are skilled in Python.
import py_compile as pc ('')
2.2. cmd command mode compilation
First locate the directory where the py file is located and open cmd. enter the following command
python -m py_compile
III. Multi-document compilation
It is worth noting that we will not use only one file in practice either. This time you need to compile all the source code files in the project directory at once, the operation is also very simple. There are also two ways to compile, each choose the one that suits you.
3.1 Multi-file Source Code Compilation
import compileall as ca # Compile all source code files in the xxx\xx directory on disk d ca.compile_dir(r'D:\\xxx\xx')
3.2 Multi-file cmd command mode compilation
python -m compileall D:\\xxx\xx
IV. Notes on running after compilation
4.1. Original platform operation
The compilation will generate a _pycache_ directory in the directory with the source code, which holds all compiled pyc files in the current directory. The points that need to be done are shown below:
1) Copy pyc under _pycache_ to the corresponding file directory.
2) Delete the source py file.
3) Rename the pyc file to the original source code file name.
4) Just delete the original py file
4.2. Notes on cross-platform operation
The compiled pyc is cross-platform, but the version of python is not. For example, if you use version 3.6 to compile a pyc file, you need to use version 3.6 on another platform. (not tested)
to this article on the python file compiled to run after the implementation of pyc steps to this article, more related python file compiled to run after the pyc content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!