python - python 文件扩展名 .pyc .pyd .pyo 代表什么?

标签 python pyc

这些 python 文件扩展名是什么意思?

  • .pyc
  • .pyd
  • .pyo

它们之间有什么区别以及它们是如何从 *.py 文件生成的?

最佳答案

  1. .py:这通常是您编写的输入源代码。
  2. .pyc:这是编译后的字节码。如果你导入一个模块,python 将构建一个包含字节码的 *.pyc 文件,以便以后再次导入它更容易(更快)。
  3. .pyo:这是 Python 3.5 之前使用的文件格式,用于通过优化 (-O) 创建的 *.pyc 文件旗帜。 (见下面的注释)
  4. .pyd:这基本上是一个windows dll文件。 http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

还有关于 .pyc.pyo 的进一步讨论,请查看:http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html (我复制了下面的重要部分)

  • When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in ‘.pyo’ files. The optimizer currently doesn't help much; it only removes assert statements. When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.
  • Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. Currently only __doc__ strings are removed from the bytecode, resulting in more compact ‘.pyo’ files. Since some programs may rely on having these available, you should only use this option if you know what you're doing.
  • A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.
  • When a script is run by giving its name on the command line, the bytecode for the script is never written to a ‘.pyc’ or ‘.pyo’ file. Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module. It is also possible to name a ‘.pyc’ or ‘.pyo’ file directly on the command line.

注意:

2015 年 9 月 15 日 Python 3.5 release实现了 PEP-488 并消除了 .pyo 文件。 这意味着 .pyc 文件代表未优化和优化的字节码。

关于python - python 文件扩展名 .pyc .pyd .pyo 代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8822335/

相关文章:

python - pandas read_csv 忽略分隔符

python - 将 csv 文件中的列加载到 spaCy

python-3.x - 如何使python源代码的编译可重现

python - pyc 文件与 python 的次要版本无关吗?

Python 不解释已更改的文件,使用过时的 .pyc

python - 给定列的 Numpy 最小值

python - 注册 abc 的实现而不将其添加到 mro

python - .pyc 文件是否包含类型提示信息?

python - 我如何理解 .pyc 文件内容

python - 如何按多个键对对象进行排序?