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

.pyc .pyd .pyo

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


当前回答

.py:这通常是您编写的输入源代码。 .pyc:这是编译后的字节码。如果你导入一个模块,python将构建一个*。Pyc文件,其中包含字节码,以使以后再次导入更容易(和更快)。 .pyo:这是Python 3.5之前用于*的文件格式。使用optimizations (-O)标志创建的pyc文件。(见下文附注) .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-09-15,Python 3.5版本实现了PEP-488并取消了.pyo文件。 这意味着.pyc文件既表示未优化的字节码,也表示优化的字节码。

其他回答

.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 modern header for use with cimports. .pxi - Cython legacy header for raw text includes. .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 .pyzw - 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. .egg - special zip used for distribution/import, mostly obsolete since PEP 427 .whl - special zip used for distribution only, widely used by pip

更多的Python文件扩展名(大多是罕见且非官方的)可以在http://dcjtech.info/topic/python-file-extensions/上找到

.py:这通常是您编写的输入源代码。 .pyc:这是编译后的字节码。如果你导入一个模块,python将构建一个*。Pyc文件,其中包含字节码,以使以后再次导入更容易(和更快)。 .pyo:这是Python 3.5之前用于*的文件格式。使用optimizations (-O)标志创建的pyc文件。(见下文附注) .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-09-15,Python 3.5版本实现了PEP-488并取消了.pyo文件。 这意味着.pyc文件既表示未优化的字节码,也表示优化的字节码。

.py:您所编写的输入源代码。

.pyc:编译后的字节码。如果你导入一个模块,python将构建一个.pyc 一个包含字节码的文件,以使以后更容易再次导入字节码 更快)。

.pyo:在优化(-O)开启时创建的.pyc文件。

.pyd: python的windows DLL文件。