从源代码(或Heroku)中找出Python版本 [英] Find out Python version from source code (or Heroku)

查看:376
本文介绍了从源代码(或Heroku)中找出Python版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们运行一个名为inteokej.nu的网站,我需要找出它运行的Python版本。我把所有的文件都拉到我的电脑里,但是我不知道是否以及如何从中找到版本号?该网站在Heroku上举办,也许有一种方法可以使用某种Heroku命令来查找该版本?



现在我没有任何改变的可能性任何代码(例如添加代码段以获取版本)。



提前感谢

解决方案

取决于你所需要的。 (a)您正在运行的版本或(b).pyc文件下的版本是否已编译?



a。如果您需要知道您正在运行的python 版本,请执行以下操作:

 >>>导入sys 
>>>> sys.version_info
sys.version_info(major = 2,minor = 7,micro = 3,releaselevel ='final',serial = 0)
>>>打印sys.version_info.major
2
>>>打印sys.version_info.minor
7
>>>打印sys.version_info.micro
3
>>>打印'%s。%s。%s'%(sys.version_info.major,sys.version_info.minor,sys.version_info.micro)
2.7.3
/ pre>

b。如果您想知道.pyc文件下的python 版本已编译,请执行以下:

 >>> f = open('somefile.pyc')
>>> magic = f.read(4)
>>> magic
'\x03\xf3\r\\\
'
>>>> magic.encode('hex')
'03f30d0a'
>>> import struct
>>>> struct.unpack(< HH,magic)
(62211,2573)
>>> struct.unpack(< HH,magic)[0]
62211

已知值列在python源文件 Python / import.c 中。这里Python 2.7.10rc1中的已知值:

  Python 1.5:20121 
Python 1.5.1:20121
Python 1.5.2:20121
Python 1.6:50428
Python 2.0:50823
Python 2.0.1:50823
Python 2.1:60202
Python 2.1.1 :60202
Python 2.1.2:60202
Python 2.2:60717
Python 2.3a0:62011
Python 2.3a0:62021
Python 2.3a0:62011(!)
Python 2.4a0:62041
Python 2.4a3:62051
Python 2.4b1:62061
Python 2.5a0:62071
Python 2.5a0:62081(ast-branch)
Python 2.5a0:62091(含)
Python 2.5a0:62092(更改为WITH_CLEANUP操作码)
Python 2.5b3:62101(修正错误代码:for x,in ...)
Python 2.5b3:62111(修复错误的代码:x + = yield)
Python 2.5c1:62121(修复错误的lnotab与for循环和
存储常量应该已被删除)
Python 2.5c2:62131(修正错误代码:for x,in ...在listcomp / genexp中)
Python 2.6a0:62151(peephole优化和STORE_MAP操作码)
Python 2.6a1:62161(WITH_CLEANUP优化)
Python 2.7a0:62171(优化列表推导/更改LIST_APPEND )
Python 2.7a0:62181(优化条件分支:
引入POP_JUMP_IF_FALSE和POP_JUMP_IF_TRUE)
Python 2.7a0 62191(介绍SETUP_WITH)
Python 2.7a0 62201(介绍BUILD_SET)
Python 2.7a0 62211(介绍MAP_ADD和SET_ADD)

为了解决你的问题,你可以递归走您的目录并从每个.pyc文件中提取值,并填充每个值/版本的文件列表的字典。请参见以下示例:

  import os,struct 

your_path = / your / path'#< - 在这里输入您的路径
values = {}

for root,dirs,os.walk(your_path)中的文件:
文件:
如果file.endswith('。pyc'):
full_path = os.path.join(根,文件)
value = struct.unpack(< HH,打开(full_path).read(4))[0]#< - evtl。包含在try块
如果不是values.has_key(value):
values [value] = []
值[value] .append(full_path)
print'% s%s'%(value,full_path)

的值,values.items()中的文件:
print'以下文件的值为%s'%(value)
对于文件中的文件:
打印'%s'%(文件)

如果你是在Linux下,您可以使用以下一行突击队来解决您的问题(感谢Neftas建议!):

  dir = / your /路径;找到$ {dir} -name* .pyc|同时读取文件;做头-c 2 $ {file} | od -d |头1 | awk -v z = $ {file} -F'{print $ 2\tz}';完成

这将输出一个包含值和文件名的列表,如下所示:

  62211 /usr/lib/pymodules/python2.7/reportbug/ui/urwid_ui.pyc 
62211 /usr/lib/pymodules/python2.7 /reportbug/debbugs.pyc
62211 /usr/lib/pymodules/python2.7/reportbug/checkbuildd.pyc
...
62161 /usr/lib/pymodules/python2.6/ debianbts.pyc
62161 /usr/lib/pymodules/python2.6/fpconst.pyc
62161 /usr/lib/pymodules/python2.6/SOAPpy/Server.pyc
...


We run a site called inteokej.nu and I need to find out which version of Python it runs on. I have pulled all the files to my computer but I don't know if and how I can find out the version number from them? The site is hosted on Heroku and maybe there's a way to find out the version with some kind of Heroku command?

As for now I don't have any possibilities to change any code (e.g. add a code snippet to get the version).

Thanks in advance!

解决方案

Depending on what you need. Either (a) the version you are running on or (b) the version under the .pyc file was compiled?

a. If you need to know the python version you are running, do the following:

>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
>>> print sys.version_info.major
2
>>> print sys.version_info.minor
7
>>> print sys.version_info.micro
3
>>> print '%s.%s.%s' % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)
2.7.3

b. If you want to know the python version under a .pyc file was compiled, do the following:

>>> f = open('somefile.pyc')
>>> magic = f.read(4)
>>> magic
'\x03\xf3\r\n'
>>> magic.encode('hex')
'03f30d0a'
>>> import struct
>>> struct.unpack("<HH", magic)
(62211, 2573)
>>> struct.unpack("<HH", magic)[0]
62211

The known values are listed in the python source file Python/import.c. Here the known values from Python 2.7.10rc1:

   Python 1.5:   20121
   Python 1.5.1: 20121
   Python 1.5.2: 20121
   Python 1.6:   50428
   Python 2.0:   50823
   Python 2.0.1: 50823
   Python 2.1:   60202
   Python 2.1.1: 60202
   Python 2.1.2: 60202
   Python 2.2:   60717
   Python 2.3a0: 62011
   Python 2.3a0: 62021
   Python 2.3a0: 62011 (!)
   Python 2.4a0: 62041
   Python 2.4a3: 62051
   Python 2.4b1: 62061
   Python 2.5a0: 62071
   Python 2.5a0: 62081 (ast-branch)
   Python 2.5a0: 62091 (with)
   Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
   Python 2.5b3: 62101 (fix wrong code: for x, in ...)
   Python 2.5b3: 62111 (fix wrong code: x += yield)
   Python 2.5c1: 62121 (fix wrong lnotab with for loops and
                        storing constants that should have been removed)
   Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
   Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
   Python 2.6a1: 62161 (WITH_CLEANUP optimization)
   Python 2.7a0: 62171 (optimize list comprehensions/change LIST_APPEND)
   Python 2.7a0: 62181 (optimize conditional branches:
            introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
   Python 2.7a0  62191 (introduce SETUP_WITH)
   Python 2.7a0  62201 (introduce BUILD_SET)
   Python 2.7a0  62211 (introduce MAP_ADD and SET_ADD)

To solve your problem, you could recursively walk your directory and extract the values from every .pyc file and populate a dictionary with a list of files for every value/version. See following example:

import os, struct

your_path = '/your/path' # <- enter your path here
values = {}

for root, dirs, files in os.walk(your_path):
    for file in files:
        if file.endswith('.pyc'):
            full_path = os.path.join(root, file)
            value = struct.unpack("<HH", open(full_path).read(4))[0] # <- evtl. enclose this in a try block
            if not values.has_key(value):
                values[value] = []
            values[value].append(full_path)
            print '%s %s' % (value, full_path)

for value, files in values.items():
    print 'Following files have value %s' % (value)
    for file in files:
        print '    %s' % (file)

If you are under Linux, you could solve your problem with the following one line commando (thanks to Neftas suggestion!):

dir=/your/path; find ${dir} -name "*.pyc" | while read file; do head -c 2 ${file} | od -d | head -n 1 | awk -v z=${file} -F ' ' '{print $2 "\t" z}'; done

This outputs a list with value and filename like the following:

62211   /usr/lib/pymodules/python2.7/reportbug/ui/urwid_ui.pyc
62211   /usr/lib/pymodules/python2.7/reportbug/debbugs.pyc
62211   /usr/lib/pymodules/python2.7/reportbug/checkbuildd.pyc
...
62161   /usr/lib/pymodules/python2.6/debianbts.pyc
62161   /usr/lib/pymodules/python2.6/fpconst.pyc
62161   /usr/lib/pymodules/python2.6/SOAPpy/Server.pyc
...

这篇关于从源代码(或Heroku)中找出Python版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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