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

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

问题描述

有没有什么方法知道哪个Python版本的 .pyc 文件是编译的?

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

推荐答案

你可以得到你的Python的神奇数字如下:

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'

pyc文件的幻数可以执行以下操作:

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'

就会知道生成pyc文件的python版本。

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

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

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