python 文件扩展名 .pyc .pyd .pyo 代表什么? [英] What do the python file extensions, .pyc .pyd .pyo stand for?

查看:117
本文介绍了python 文件扩展名 .pyc .pyd .pyo 代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What do these python file extensions mean?

  • .pyc
  • .pyd
  • .pyo

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

What are the differences between them and how are they generated from a *.py file?

推荐答案

  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
  1. .py: This is normally the input source code that you've written.
  2. .pyc: This is the compiled bytecode. If you import a module, python will build a *.pyc file that contains the bytecode to make importing it again later easier (and faster).
  3. .pyo: This was a file format used before Python 3.5 for *.pyc files that were created with optimizations (-O) flag. (see the note below)
  4. .pyd: This is basically a windows dll file. 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(我已经复制了下面的重要部分)

Also for some further discussion on .pyc vs .pyo, take a look at: http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html (I've copied the important part below)

  • 当使用 -O 标志调用 Python 解释器时,会生成优化的代码并将其存储在.pyo"文件中.优化器目前没有太大帮助;它只删除断言语句.使用 -O 时,优化所有字节码;.pyc 文件被忽略,.py 文件被编译为优化的字节码.
  • 将两个 -O 标志传递给 Python 解释器 (-OO) 将导致字节码编译器执行优化,这在极少数情况下可能会导致程序出现故障.目前只有 __doc__ 字符串从字节码中删除,从而产生更紧凑的.pyo"文件.由于某些程序可能依赖于提供这些功能,因此只有在您知道自己在做什么的情况下才应使用此选项.
  • 从.pyc"或.pyo"文件中读取程序的运行速度并不比从.py"文件中读取时快;.pyc"或.pyo"文件唯一更快的是它们的加载速度.
  • 当脚本通过在命令行中给出其名称来运行时,脚本的字节码永远不会写入.pyc"或.pyo"文件.因此,脚本的启动时间可以通过将其大部分代码移动到一个模块并使用一个导入该模块的小型引导脚本来减少.也可以直接在命令行上命名.pyc"或.pyo"文件.
  • 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 版本 实现了 PEP-488 并消除了 .pyo 文件.这意味着 .pyc 文件代表未优化和优化的字节码.

On 2015-09-15 the Python 3.5 release implemented PEP-488 and eliminated .pyo files. This means that .pyc files represent both unoptimized and optimized bytecode.

这篇关于python 文件扩展名 .pyc .pyd .pyo 代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆