How To Get Python Version From Command Line Or Python Source Code

This article will tell you how to get your python version from the command line and in the python source code.

1. How To Get Python Version From The Command-Line.

  1. Open a terminal in your OS.
  2. Run the command python –version to show the current python version.
    > python --version
    Python 3.8.12
  3. You can also run the command python -V to show the same python version.
    > python -V
    Python 3.8.12
  4. If your python executable file name is python3, then you should run the command python3 –version, or python3 -V.
  5. If your python version is bigger than 3.5, you can run the python -VV command to get the more detailed python version information. Take care of the argument -VV should be uppercase V.
    (MyPythonEnv) C:\Users\zhaosong>python -VV
    Python 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)]

2. How To Get Python Version In Python Source Code.

  1. You can also use the python sys, platform module to get the Python version in your python source code.
  2. Open a terminal and run the command python to enter the interactive console.
    > python
    Python 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  3. Run the command import sys to import the Python sys module.
  4. Then run the command sys.version to get the Python version string value.
    >>> import sys
    >>>
    >>> sys.version
    '3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)]'
    >>>
    >>> type(sys.version)
    <class 'str'>
  5. You can run the command sys.version_info to get the Python version data in a tuple object.
    >>> import sys
    >>>
    >>> python_ver = sys.version_info
    >>>
    >>> print(python_ver)
    sys.version_info(major=3, minor=8, micro=12, releaselevel='final', serial=0)
    >>>
    >>> type(python_ver)
    <class 'sys.version_info'>
    >>>
    >>> python_ver.major
    3
    >>>
    >>> python_ver[1]
    8
  6. Besides the Python sys module, you can also use the Python platform module to get the python version information.
  7. Import the platform module, and then call the platform.python_version() function to get the Python version string value, and you can call the platform.python_version_tuple() function to get the Python version in a tuple object.
    >>> import platform
    >>>
    >>> platform.python_version()
    '3.8.12'
    >>>
    >>> type(platform.python_version())
    <class 'str'>
    >>>
    >>> platform.python_version_tuple()
    ('3', '8', '12')
    >>>
    >>> type(platform.python_version_tuple())
    <class 'tuple'>

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.