Detect python version in shell script

前端 未结 14 2108
清歌不尽
清歌不尽 2020-12-07 20:26

I\'d like to detect if python is installed on a Linux system and if it is, which python version is installed.

How can I do it? Is there something more graceful than

14条回答
  •  眼角桃花
    2020-12-07 21:06

    You can use the platform module which is part of the standard Python library:

    $ python -c 'import platform; print(platform.python_version())'
    2.6.9
    

    This module allows you to print only a part of the version string:

    $ python -c 'import platform; major, minor, patch = platform.python_version_tuple(); print(major); print(minor); print(patch)'
    2
    6
    9
    

提交回复
热议问题