python # what is .pyc

2018-10-28  本文已影响16人  FlyingPenguin

Python's *.py file is just a text file in which you write some lines of code.

When you try to execute this file using say "python filename.py"
This command invokes Python Virtual Machine.
Python Virtual Machine has 2 components: "compiler" and "interpreter".

Interpreter cannot directly read the text in *.py file, so this text is first converted into a byte code which is targeted to the PVM (not hardware but PVM).
PVM executes this byte code. *.pyc file is also generated, as part of running it which performs your import operation on file in shell or in some other file.

If this *.pyc file is already generated then every next time you run/execute your *.py file, system directly loads your *.pyc file which won't need any compilation(This will save you some machine cycles of processor).

Once the *.pyc file is generated, there is no need of *.py file, unless you edit it.

Python的虚拟机有两个组件: 编译器 和 解释器。
*.py只是个text文件,解释器是无法直接读取*.py上的文本信息的。
*.py上的text需要转换为byte code,此时,Python的虚拟机才能运行这个byte code。

Python code goes through 2 stages.
First step compiles the code into .pyc files which is actually a bytecode.
Then this .pyc file(bytecode) is interpreted using CPython interpreter.

References:

https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files

上一篇下一篇

猜你喜欢

热点阅读