Detect python version in shell script

前端 未结 14 2095
清歌不尽
清歌不尽 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:02

    You could use something along the following lines:

    $ python -c 'import sys; print(sys.version_info[:])'
    (2, 6, 5, 'final', 0)
    

    The tuple is documented here. You can expand the Python code above to format the version number in a manner that would suit your requirements, or indeed to perform checks on it.

    You'll need to check $? in your script to handle the case where python is not found.

    P.S. I am using the slightly odd syntax to ensure compatibility with both Python 2.x and 3.x.

    0 讨论(0)
  • 2020-12-07 21:02

    Adding to the long list of possible solutions, here's a similar one to the accepted answer - except this has a simple version check built into it:

    python -c 'import sys; exit(1) if sys.version_info.major < 3 and sys.version_info.minor < 5 else exit(0)'
    

    this will return 0 if python is installed and at least versions 3.5, and return 1 if:

    • Python is not installed
    • Python IS installed, but its version less than version 3.5

    To check the value, simply compare $? (assuming bash), as seen in other questions.

    Beware that this does not allow checking different versions for Python2 - as the above one-liner will throw an exception in Py2. However, since Python2 is on its way out the door, this shouldn't be a problem.

    0 讨论(0)
  • 2020-12-07 21:03

    You can use this too:

    pyv="$(python -V 2>&1)"
    echo "$pyv"
    
    0 讨论(0)
  • 2020-12-07 21:03

    To check if ANY Python is installed (considering it's on the PATH), it's as simple as:

    if which python > /dev/null 2>&1;
    then
        #Python is installed
    else
        #Python is not installed
    fi
    

    The > /dev/null 2>&1 part is there just to suppress output.

    To get the version numbers also:

    if which python > /dev/null 2>&1;
    then
        #Python is installed
        python_version=`python --version 2>&1 | awk '{print $2}'`
        echo "Python version $python_version is installed."
    
    else
        #Python is not installed
        echo "No Python executable is found."
    fi
    

    Sample output with Python 3.5 installed: "Python version 3.5.0 is installed."

    Note 1: The awk '{print $2}' part will not work correctly if Python is not installed, so either use inside the check as in the sample above, or use grep as suggested by Sohrab T. Though grep -P uses Perl regexp syntax and might have some portability problems.

    Note 2: python --version or python -V might not work with Python versions prior to 2.5. In this case use python -c ... as suggested in other answers.

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

    Here is another solution using hash to verify if python is installed and sed to extract the first two major numbers of the version and compare if the minimum version is installed

    if ! hash python; then
        echo "python is not installed"
        exit 1
    fi
    
    ver=$(python -V 2>&1 | sed 's/.* \([0-9]\).\([0-9]\).*/\1\2/')
    if [ "$ver" -lt "27" ]; then
        echo "This script requires python 2.7 or greater"
        exit 1
    fi
    
    0 讨论(0)
提交回复
热议问题