Detect python version in shell script

前端 未结 14 2113
清歌不尽
清歌不尽 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 20:59

    using sys.hexversion could be useful if you want to compare version in shell script

    ret=`python -c 'import sys; print("%i" % (sys.hexversion<0x03000000))'`
    if [ $ret -eq 0 ]; then
        echo "we require python version <3"
    else 
        echo "python version is <3"
    fi
    

提交回复
热议问题