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

问题:python文件扩展名.pyc .pyd .pyo代表什么?

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

  • .pyc
  • .pyd
  • .pyo

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

What do these python file extensions mean?

  • .pyc
  • .pyd
  • .pyo

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


回答 0

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

另外,对于某些.pyc与vs 有关的讨论.pyo,请查看:http : //www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html(我已复制了下面的重要部分)

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

注意:

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

  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

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)

  • 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.

Note:

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.


回答 1

  • .py-常规脚本
  • .py3-(很少使用)Python3脚本。Python3脚本通常以“ .py”而不是“ .py3”结尾,但是我已经看过几次了
  • .pyc-编译脚本(字节码)
  • .pyo -优化的pyc文件(Python3.5的,Python将只使用PYC而非杓和PYC)
  • .pyw-在没有控制台的情况下以窗口模式运行的Python脚本;用pythonw.exe执行
  • .pyx -Cython src转换为C / C ++
  • .pyd -Windows DLL编写的Python脚本
  • .pxd -Cython脚本,等效于C / C ++头
  • .pxi -MyPy存根
  • .pyi-存根文件(PEP 484
  • .pyz -Python脚本存档(PEP 441); 这是一个在标准Python脚本标头之后包含二进制格式的压缩Python脚本(ZIP)的脚本
  • .pywz-适用于MS-Windows的Python脚本存档(PEP 441); 这是一个在标准Python脚本标头之后包含二进制格式的压缩Python脚本(ZIP)的脚本
  • .py [cod] -“ .gitignore”中的通配符表示文件可能是“ .pyc”,“。pyo”或“ .pyd”。
  • .pth-路径配置文件;其内容是要添加到的其他项(每行一个)sys.path。参见site模块。

可以在http://dcjtech.info/topic/python-file-extensions/找到更多其他Python文件扩展名的列表(大多数是罕见的和非正式的)。

  • .py – Regular script
  • .py3 – (rarely used) Python3 script. Python3 scripts usually end with “.py” not “.py3”, but I have seen that a few times
  • .pyc – compiled script (Bytecode)
  • .pyo – optimized pyc file (As of Python3.5, Python will only use pyc rather than pyo and pyc)
  • .pyw – Python script to run in Windowed mode, without a console; executed with pythonw.exe
  • .pyx – Cython src to be converted to C/C++
  • .pyd – Python script made as a Windows DLL
  • .pxd – Cython script which is equivalent to a C/C++ header
  • .pxi – MyPy stub
  • .pyi – Stub file (PEP 484)
  • .pyz – Python script archive (PEP 441); this is a script containing compressed Python scripts (ZIP) in binary form after the standard Python script header
  • .pywz – Python script archive for MS-Windows (PEP 441); this is a script containing compressed Python scripts (ZIP) in binary form after the standard Python script header
  • .py[cod] – wildcard notation in “.gitignore” that means the file may be “.pyc”, “.pyo”, or “.pyd”.
  • .pth – a path configuration file; its contents are additional items (one per line) to be added to sys.path. See site module.

A larger list of additional Python file-extensions (mostly rare and unofficial) can be found at http://dcjtech.info/topic/python-file-extensions/