Help us understand the problem. What is going on with this article?

Pythonバージョンの取得方法

More than 1 year has passed since last update.

実行中のPythonプログラムからPythonバージョンを取得する方法と実行例を示す。
バージョン取得方法は以下の通り複数存在する:

  1. sys.version_info
  2. platform.python_version_tuple()
  3. six

方法1: sys.version_info

sys.version_infoは実行環境のPythonバージョンを格納している。
Python 2.7未満/3.0 ではtuple、2.7/3.1以降はnamedtupleが型となる。

実行例

python2.5
>>> import sys
>>> sys.version_info
(2, 5, 2, 'final', 0)
python2.7
>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
python3.5
>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

Reference

28.1. sys — System-specific parameters and functions — Python 2.7.11 documentation

sys.version_info

A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0). The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on.

New in version 2.0.

Changed in version 2.7: Added named component attributes

方法2: platform.python_version_tuple()

platform.python_version_tuple() はPythonバージョンに依らず、tuple としてバージョン番号情報を取得する。

実行例

>>> import platform
>>> platform.python_version_tuple()
('2', '7', '14')
python3.6
>>> import platform
>>> platform.python_version_tuple()
('3', '6', '4')

Reference

15.15. platform — Access to underlying platform’s identifying data — Python 2.7.11 documentation

New in version 2.3.

platform.python_version_tuple()

Returns the Python version as tuple (major, minor, patchlevel) of strings.

Note that unlike the Python sys.version, the returned value will always include the patchlevel (it defaults to '0').

方法3: six

sixで取得できるのはPython 2系か、Python 3系かの判別(とPython 3.4 以上かは判定できる)。

実行例

Python2.7
>>> import six
>>> six.PY2
True
>>> six.PY3
False
>>> six.PY34
False
Python3.6
>>> import six
>>> six.PY2
False
>>> six.PY3
True
>>> six.PY34
True

内部実装では、上述の sys.version_info を使用している。

six.py
# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)

おまけ: Shell Script上でPythonバージョン判定

Pythonのmajor versionを取得するワンライナー

Python2/3
$ pymajorver=$(python -c "from __future__ import print_function; import sys; print(sys.version_info[0])")
$ echo $pymajorver
3

以下のように case 文でも使える:

case $(python -c "from __future__ import print_function; import sys; print(sys.version_info[0])") in
    "2") echo "Python2 !!" ;;
    "3") echo "Python3 !!" ;;
    *) echo "Unknown Python version" ;;
esac

Pythonのmajor/minorバージョンを取得するワンライナー

Python 2/3両対応

Python2.7
$ python -c "from __future__ import print_function; import sys; print('{}{}'.format(*sys.version_info[0:2]))"
27

Python 3のみ

Python3.6
$ pyversion=$(python -c "import sys; print('{}{}'.format(*sys.version_info[0:2]))")
$ echo $pyversion
36
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away