Find out the Python version from the source code (or Heroku)

We are launching a site called inteokej.nu, and I need to find out which version of Python it is running on. I pulled out all the files on 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 is a way to find out the version using any Heroku team?

At the moment, I have no way to change any code (for example, add a piece of code to get the version).

Thanks in advance!

+1
source share
4 answers

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

a. python , , :

>>> 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. , python .pyc , :

>>> 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

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 (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)

, .pyc /. . :

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)

Linux, ( Neftas!):

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

:

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
...
+3

, Heroku. , Python 2.7.9. , runtime.txt, , :

$ cat runtime.txt
python-3.4.2
+1

, ( Heroku) :

Heroku bash, - :

heroku run bash --app YOURAPPNAME

:

python --version

+1

, , Gunicorn 0.17.2, Python 2.x >= 2.6, Python 3.

0

All Articles