If Python is interpreted, what are .pyc files?

前端 未结 11 959
夕颜
夕颜 2020-11-22 08:39

I\'ve been given to understand that Python is an interpreted language...
However, when I look at my Python source code I see .pyc files, w

11条回答
  •  孤城傲影
    2020-11-22 09:43

    To speed up loading modules, Python caches the compiled content of modules in .pyc.

    CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py , CPython caches the byte code in a foo.pyc file right next to the source.

    In python3, Python's import machinery is extended to write and search for byte code cache files in a single directory inside every Python package directory. This directory will be called __pycache__ .

    Here is a flow chart describing how modules are loaded:

    For more information:

    ref:PEP3147
    ref:“Compiled” Python files

提交回复
热议问题