Check system python version

Get the OS and its version where Python is running

You can get the OS and its release version of the environment where Python is running with the standard library platform .

By platform , it is possible to switch the operation according to the OS and version.

This article describes the following contents:

  • Get the system/OS name: platform.system()
  • Get the system’s release version: platform.release() , version()
  • Get the OS, version, etc. together: platform.platform()
  • Examples for each OS
    • macOS
    • Windows
    • Ubuntu

    See the following article for how to get the version of Python.

    All sample code in the first half was run on macOS Mojave 10.14.2. Examples of results on Windows and Ubuntu are shown later. OS-specific functions will also be described later.

    Get the system/OS name: platform.system()

    platform.system() returns the system/OS name as a string.

    import platform print(platform.system()) # Darwin 

    Get the system’s release version: platform.release() , version()

    platform.release() and platform.version() returns the system’s release version as a string.

    As shown in the example below, platform.release() returns simpler contents.

    print(platform.release()) # 18.2.0 print(platform.version()) # Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 

    Get the OS, version, etc. together: platform.platform()

    platform.platform() returns a string containing the OS name, version information, etc.

    print(platform.platform()) # Darwin-18.2.0-x86_64-i386-64bit 

    If terse is True , only minimal information is returned.

    print(platform.platform(terse=True)) # Darwin-18.2.0 

    There is also an argument aliased .

    print(platform.platform(aliased=True)) # Darwin-18.2.0-x86_64-i386-64bit 

    The result is the same in the above example, but depending on the OS, alias is returned as the OS name.

    If aliased is true, the function will use aliases for various platforms that report system names which differ from their common names, for example SunOS will be reported as Solaris. platform.platform() — Access to underlying platform’s identifying data — Python 3.9.1 documentation

    Examples for each OS

    Examples of results run on macOS, Windows, and Ubuntu are shown below, along with OS-specific functions.

    macOS

    Same as the example shown above.

    print(platform.system()) # Darwin print(platform.release()) # 18.2.0 print(platform.version()) # Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 print(platform.platform()) # Darwin-18.2.0-x86_64-i386-64bit 

    Note that the OS name is Darwin , not macOS or Mojave . See Wikipedia for the version number corresponding to the name in macOS.

    platform.mac_ver() is a macOS-specific function that returns tuple (release, versioninfo, machine) .

    print(platform.mac_ver()) # ('10.14.2', ('', '', ''), 'x86_64') 

    Windows

    print(platform.system()) # Windows print(platform.release()) # 10 print(platform.version()) # 10.0.17763 print(platform.platform()) # Windows-10-10.0.17763-SP0 

    Note that the return value 10 of platform.release() is a string, not an integer.

    platform.win32_ver() is a Windows-specific function that returns tuple (release, version, csd, ptype) .

    print(platform.win32_ver()) # ('10', '10.0.17763', 'SP0', 'Multiprocessor Free') 

    platform.win32_edition() , which returns the Windows edition as a string, and platform.win32_is_iot() , which returns True for the IoT edition, have been added in Python 3.8.

    Ubuntu

    print(platform.system()) # Linux print(platform.release()) # 4.15.0-42-generic print(platform.version()) # #45-Ubuntu SMP Thu Nov 15 19:32:57 UTC 2018 print(platform.platform()) # Linux-4.15.0-44-generic-x86_64-with-Ubuntu-18.04-bionic 

    platform.linux_distribution() is a Unix-specific function that returns tuple (distname, version, id) .

    print(platform.linux_distribution()) # ('Ubuntu', '18.04', 'bionic') 

    Note that this function was removed in Python 3.8. It is recommended to use the third party library distro instead.

    Sample code that switches operation depending on the OS

    For example, by using platform.system() , the operation can be switched depending on the OS.

    Example of getting the creation date and time of a file:

    def creation_date(path_to_file): """ Try to get the date that a file was created, falling back to when it was last modified if that isn't possible. See http://stackoverflow.com/a/39501288/1709587 for explanation. """ if platform.system() == 'Windows': return os.path.getctime(path_to_file) else: stat = os.stat(path_to_file) try: return stat.st_birthtime except AttributeError: # We're probably on Linux. No easy way to get creation dates here, # so we'll settle for when its content was last modified. return stat.st_mtime 

    In this example, the value of platform.system() is used to determine whether it is Windows or not, and then exception handling is used to switch the operation depending on whether the st_birthtime attribute exists.

    See the following article for exception handling.

    Источник

    How to Check Python Version in Linux, Mac, & Windows

    Python is a popular programming language. Like many other programming languages, there can be several different versions organized by release date. Certain applications may require a specific version of Python.

    In this tutorial, learn how to check the Python version on Windows, Linux, or macOS systems.

    tutorial on how to check Python version.

    Access to a command-line/terminal window:

    • Linux: Ctrl-Alt-T, Ctrl-Alt-F2
    • Windows: Win+R > type powershell > Enter/OK
    • MacOS: Finder > Applications > Utilities > Terminal

    There are different versions of Python, but the two most popular ones are Python 2.7.x and Python 3.7.x. The x stands for the revision level and could change as new releases come out.

    When looking at the version number, there are usually three digits to read:

    While major releases are not fully compatible, minor releases generally are. Version 3.6.1 should be compatible with 3.7.1 for example. The final digit signifies the latest patches and updates.

    Python 2.7 and 3.7 are different applications. Software that’s written in one version often will not work correctly in another version. When using Python, it is essential to know which version an application requires, and which version you have.

    Python 2 will stop publishing security updates and patches after 2020. They extended the deadline because of the large number of developers using Python 2.7. Python 3 includes a 2 to 3 utility that helps translate Python 2 code into Python 3.

    How to Check Python Version in Linux

    Most modern Linux distributions come with Python pre-installed.

    To check the version installed, open a terminal window and entering the following:

    python version linux

    How to Check Python Version in Windows

    Most out-of-the-box Windows installations do not come with Python pre-installed. However, it is always a good idea to check.

    Open Windows Powershell, and enter the following:

    If you have Python installed, it will report the version number.

    check python version windows

    Alternately, use the Windows Search function to see which version of Python you have:

    Press the Windows key to start a search, then type Python. The system will return any results that match. Most likely a match will show something similar to:

    This defines which major and minor revision (3.x or 2.x) you are using.

    How to Check Python Version in MacOS

    If using a MacOS, check the Python version by entering the following command in the terminal:

    The system will report the version.

    check python version macos

    Note: In some cases, this will return a screen full of information. If that happens, just scan through the file locations for the word python with a number after it. That number is the version.

    Checking a System with Multiple Versions of Python

    Python2 and Python3 are different programs. Many programs upgrade from the older version to the newer one. However, Python 2.7.x installations can be run separately from the Python 3.7.x version on the same system.

    Python 3 is not entirely backward compatible.

    To check for Python 2.7.x:

    To check the version of Python 3 software:

    Most systems differentiate Python 2 as python and Python 3 as python3. If you do not have Python 2, your system may use the python command in place of python3 .

    Note: Python does not have a built-in upgrade system. You’ll need to download the latest version and install it.

    How to Check Python Version in Script

    When writing an application, it is helpful to have the software check the version of Python before it runs to prevent crashes and incompatibilities.

    Use the following code snippet to check for the correct version of Python:

    if not sys.version_info.major == 3 and sys.version_info.minor >= 6: print("Python 3.6 or higher is required.") print("You are using Python <>.<>.".format(sys.version_info.major, sys.version_info.minor)) sys.exit(1)

    When this script runs, it will test to see if Python 3.6 is installed on the system. If not, it will send a notification and displays the current Python version.

    Note: One of the common issues in working with Python and datasets is missing data. Learn how to handle missing data in Python.

    You should now have a solid understanding of how to check for the version of Python installed in several different operating systems. Python is a powerful programming language, thus it’s important to understand its different versions.

    If you want to learn how to upgrade Python to a newer version on Wondows, macOs, and Linux, check our article how to upgrade Python to 3.9.

    Источник

    How to Check Your Python Version (Windows, macOS, Linux)

    How to Check Your Python Version (Windows, macOS, Linux) Cover Image

    In this tutorial, you’ll learn how to check your Python version in Windows, macOS, and Linux. You’ll learn how to check the version of Python using the command line and within a Python script itself. You’ll learn how to get the version number of the interpreter that your scripts will use.

    Knowing how to do this is an important skill for any Python developer. For example, it can be an important skill in order to better troubleshoot your code. If your interpreter is set to a different version than you’re expecting (say, Python 2 versus Python 3), being able to identify the version of the interpreter can help troubleshoot your problems.

    By the end of this tutorial, you’ll have learned:

    • How to check the Python version of your interpreter in Windows, Mac OS, and Linux
    • How the check the Python version while running your script
    • How to access the major, minor and micro versions of your Python version programmatically

    How to Check Your Python Version

    To check the version that your Python interpreter is running we can use a version command on the python command. Because accessing the command line prompt or terminal varies from system to system, this part of the tutorial is split across the different operating systems available to you.

    Over the following sections, you’ll learn how to check your Python version using Windows 10, Windows 7, macOS, and Linux.

    How to Check Your Python Version on Windows 10

    In Windows 10, we can use the PowerShell to check the version of Python that we are running. In order to access the PowerShell, simply use the following steps:

    Once the PowerShell is open you can access the Python version your interpreter is running by writing the commands shown below.

    Источник

    Читайте также:  Opencv for android java
Оцените статью