有没有办法知道 .pyc 文件是由哪个 Python 版本编译的? [英] Is there a way to know by which Python version the .pyc file was compiled?

查看:69
本文介绍了有没有办法知道 .pyc 文件是由哪个 Python 版本编译的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道.pyc文件是用哪个Python版本编译的?

解决方案

您可以通过以下方式获取 Python 的幻数:

$ python -V蟒蛇 2.6.2# Python>>>进口进口>>>imp.get_magic().encode('hex')'d1f20d0a'

要获取 pyc 文件的幻数,您可以执行以下操作:

<预><代码>>>>f = open('test25.pyc')>>>魔法 = f.read(4)>>>魔术编码('十六进制')'b3f20d0a'>>>f = open('test26.pyc')>>>魔法 = f.read(4)>>>魔术编码('十六进制')'d1f20d0a'

通过比较幻数,您将知道生成 pyc 文件的 python 版本.

Is there any way to know by which Python version the .pyc file was compiled?

解决方案

You can get the magic number of your Python as follows:

$ python -V
Python 2.6.2
# python
>>> import imp
>>> imp.get_magic().encode('hex')
'd1f20d0a'

To get the magic number for a pyc file you can do the following:

>>> f = open('test25.pyc')
>>> magic = f.read(4)
>>> magic.encode('hex')
'b3f20d0a'
>>> f = open('test26.pyc')
>>> magic = f.read(4)
>>> magic.encode('hex')
'd1f20d0a'

By comparing the magic numbers you'll know the python version that generated the pyc file.

这篇关于有没有办法知道 .pyc 文件是由哪个 Python 版本编译的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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