有没有办法知道 .pyc 文件是由哪个 Python 版本编译的?
有什么方法可以知道 .pyc
文件是由哪个 Python 版本编译的?
Is there any way to know by which Python version the .pyc
file was compiled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
.pyc
文件的前两个字节是表示字节码版本的幻数。该单词以小端格式存储,已知值为:with
WITH_CLEANUP
操作码for x, in ...
x +=yield
for x, in ...
in listcomp/genexpSTORE_MAP
操作码WITH_CLEANUP
优化LIST_APPEND
POP_JUMP_IF_FALSE
和POP_JUMP_IF_TRUE
SETUP_WITH
BUILD_SET
MAP_ADD
和SET_ADD
UNARY_CONVERT
BUILD_SET
print
成为函数__file__
和__name__
unicode__file__
指向源文件WITH_CLEANUP
优化POP_EXCEPT
LIST_APPEND
和SET_ADD
,添加MAP_ADD
POP_JUMP_IF_FALSE
和POP_JUMP_IF_TRUE
SETUP_WITH
,标签: cpython-32DUP_TOP_TWO
,删除DUP_TOPX
和ROT_FOUR
,标签:cpython-32DELETE_DEREF
来源:
The first two bytes of the
.pyc
file are the magic number that tells the version of the bytecodes. The word is stored in little-endian format, and the known values are:with
WITH_CLEANUP
opcodefor x, in ...
x += yield
for x, in ...
in listcomp/genexpSTORE_MAP
opcodeWITH_CLEANUP
optimizationLIST_APPEND
POP_JUMP_IF_FALSE
andPOP_JUMP_IF_TRUE
SETUP_WITH
BUILD_SET
MAP_ADD
andSET_ADD
UNARY_CONVERT
BUILD_SET
print
becomes a function__file__
and__name__
unicode__file__
points to source fileWITH_CLEANUP
optimizationPOP_EXCEPT
LIST_APPEND
andSET_ADD
, addMAP_ADD
POP_JUMP_IF_FALSE
andPOP_JUMP_IF_TRUE
SETUP_WITH
, tag: cpython-32DUP_TOP_TWO
, removeDUP_TOPX
andROT_FOUR
, tag: cpython-32DELETE_DEREF
Sources:
您可以按如下方式获取 Python 的幻数:
要获取 pyc 文件的幻数,您可以执行以下操作:
通过比较幻数,您将知道生成 pyc 文件的 python 版本。
You can get the magic number of your Python as follows:
To get the magic number for a pyc file you can do the following:
By comparing the magic numbers you'll know the python version that generated the pyc file.
或者,如果您有 GNU/Linux 系统,您可以在终端中使用命令“file”:
Or, if you have a GNU/Linux system you can use the command "file" in a terminal:
看一下我的 Python 脚本,它检测并返回文件所使用的 Python 版本(*.pyc 或 *.pyo)已编译。
它检测从 Python 1.5 到最新 Python 3 版本的 Python 版本。
Take a look at my script in Python that detects and returns the version of Python by which the file (*.pyc or *.pyo) was compiled.
It detects versions of Python from Python 1.5 up to last Python 3 build.
官方 Python github 存储库似乎不再将列表保留在 import.c 中。
在搜索比我在其他地方找到的更新的列表时,我遇到了似乎是 Google 截至 2017 年 5 月的最新列表。
https://github.com/google/pytype/blob/master/pytype/pyc/magic.py
The official Python github repository no longer appears to keep the list in import.c.
When searching for a more current list than I could find elsewhere, I encountered what appears to be an up-to-date list from Google as of May 2017.
https://github.com/google/pytype/blob/master/pytype/pyc/magic.py
添加@Igor Popov 的答案。
要从较新版本的 Python 检查已编译脚本的版本:
您可以在此处查找输出编号:
https://github.com/google/pytype/blob/主/pytype/pyc/magic.py
Adding to @Igor Popov's answer.
To check the version of the compiled script from a newer version of Python:
You can lookup the output number here:
https://github.com/google/pytype/blob/master/pytype/pyc/magic.py