Detect python version in shell script

前端 未结 14 2094
清歌不尽
清歌不尽 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:45
    python -c 'import sys; print sys.version_info'
    

    or, human-readable:

    python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))'
    
    0 讨论(0)
  • 2020-12-07 20:51

    In case you need a bash script, that echoes "NoPython" if Python is not installed, and with the Python reference if it is installed, then you can use the following check_python.sh script.

    • To understand how to use it in your app, I've also added my_app.sh.
    • Check that it works by playing with PYTHON_MINIMUM_MAJOR and PYTHON_MINIMUM_MINOR

    check_python.sh

    #!/bin/bash
    
    # Set minimum required versions
    PYTHON_MINIMUM_MAJOR=3
    PYTHON_MINIMUM_MINOR=6
    
    # Get python references
    PYTHON3_REF=$(which python3 | grep "/python3")
    PYTHON_REF=$(which python | grep "/python")
    
    error_msg(){
        echo "NoPython"
    }
    
    python_ref(){
        local my_ref=$1
        echo $($my_ref -c 'import platform; major, minor, patch = platform.python_version_tuple(); print(major); print(minor);')
    }
    
    # Print success_msg/error_msg according to the provided minimum required versions
    check_version(){
        local major=$1
        local minor=$2
        local python_ref=$3
        [[ $major -ge $PYTHON_MINIMUM_MAJOR && $minor -ge $PYTHON_MINIMUM_MINOR ]] && echo $python_ref || error_msg
    }
    
    # Logic
    if [[ ! -z $PYTHON3_REF ]]; then
        version=($(python_ref python3))
        check_version ${version[0]} ${version[1]} $PYTHON3_REF
    elif [[ ! -z $PYTHON_REF ]]; then
        # Didn't find python3, let's try python
        version=($(python_ref python))
        check_version ${version[0]} ${version[1]} $PYTHON_REF
    else
        # Python is not installed at all
        error_msg
    fi
    

    my_app.sh

    #!/bin/bash
    # Add this before your app's code
    PYTHON_REF=$(source ./check_python.sh) # change path if necessary
    if [[ "$PYTHON_REF" == "NoPython" ]]; then
        echo "Python3.6+ is not installed."
        exit
    fi
    
    # This is your app
    # PYTHON_REF is python or python3
    $PYTHON_REF -c "print('hello from python 3.6+')";
    
    0 讨论(0)
  • 2020-12-07 20:54

    You can use this command in bash:

    PYV=`python -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)";`
    echo $PYV
    
    0 讨论(0)
  • 2020-12-07 20:58

    If you need to check if version is at least 'some version', then I prefer solution which doesn't make assumptions about number of digits in version parts.

    VERSION=$(python -V 2>&1 | cut -d\  -f 2) # python 2 prints version to stderr
    VERSION=(${VERSION//./ }) # make an version parts array 
    if [[ ${VERSION[0]} -lt 3 ]] || [[ ${VERSION[0]} -eq 3 && ${VERSION[1] -lt 5 ]] ; then
        echo "Python 3.5+ needed!" 1>&2
        return 1
    fi
    

    This would work even with numbering like 2.12.32 or 3.12.0, etc. Inspired by this answer.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-07 21:01

    Yet another way to print Python version in a machine-readable way with major and minor version number only. For example, instead of "3.8.3" it will print "38", and instead of "2.7.18" it will print "27".

    python -c "import sys; print(''.join(map(str, sys.version_info[:2])))"
    

    Works for both Python 2 and 3.

    0 讨论(0)
提交回复
热议问题