How do I detect the Python version at runtime? [du

2020-01-24 06:15发布

I have a Python file which might have to support Python versions < 3.x and >= 3.x. Is there a way to introspect the Python runtime to know the version which it is running (for example, 2.6 or 3.2.x)?

标签: python
9条回答
疯言疯语
2楼-- · 2020-01-24 06:35

To make the scripts compatible with Python2 and 3 i use :

from sys import version_info
if version_info[0] < 3:
    from __future__ import print_function
查看更多
放我归山
3楼-- · 2020-01-24 06:37

Version check example below.

Note that I do not stop the execution, this snippet just:
- do nothing if exact version matches
- write INFO if revision (last number) is different
- write WARN if any of major+minor are different

import sys
import warnings

def checkVersion():
    # Checking Python version:
    expect_major = 2
    expect_minor = 7
    expect_rev = 14
    if sys.version_info[:3] != (expect_major, expect_minor, expect_rev):
        print("INFO: Script developed and tested with Python " + str(expect_major) + "." + str(expect_minor) + "." + str(expect_rev))
        current_version = str(sys.version_info[0])+"."+str(sys.version_info[1])+"."+str(sys.version_info[2])
        if sys.version_info[:2] != (expect_major, expect_minor):
            warnings.warn("Current Python version was unexpected: Python " + current_version)
        else:
            print("      Current version is different: Python " + current_version)
查看更多
男人必须洒脱
4楼-- · 2020-01-24 06:38

Sure, take a look at sys.version and sys.version_info.

For example, to check that you are running Python 3.x, use

import sys
if sys.version_info[0] < 3:
    raise Exception("Must be using Python 3")

Here, sys.version_info[0] is the major version number. sys.version_info[1] would give you the minor version number.

In Python 2.7 and later, the components of sys.version_info can also be accessed by name, so the major version number is sys.version_info.major.

See also How can I check for Python version in a program that uses new language features?

查看更多
▲ chillily
5楼-- · 2020-01-24 06:43

The best solution depends on how much code is incompatible. If there are a lot of places you need to support Python 2 and 3, six is the compatibility module. six.PY2 and six.PY3 are two booleans if you want to check the version.

However, a better solution than using a lot of if statements is to use six compatibility functions if possible. Hypothetically, if Python 3000 has a new syntax for next, someone could update six so your old code would still work.

import six

#OK
if six.PY2:
  x = it.next() # Python 2 syntax
else:
  x = next(it) # Python 3 syntax

#Better
x = six.next(it)

http://pythonhosted.org/six/

Cheers

查看更多
来,给爷笑一个
6楼-- · 2020-01-24 06:47

Try this code, this should work:

import platform
print(platform.python_version())
查看更多
看我几分像从前
7楼-- · 2020-01-24 06:50

Just in case you want to see all of the gory details in human readable form, you can use:

import platform;

print(platform.sys.version);

Output for my system:

3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0]

Something very detailed but machine parsable would be to get the version_info object from platform.sys, instead, and then use its properties to take a predetermined course of action. For example:

import platform;

print(platform.sys.version_info)

Output on my system:

sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
查看更多
登录 后发表回答