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.
- Open a terminal in your OS.
- Run the command python –version to show the current python version.
> python --version Python 3.8.12
- You can also run the command python -V to show the same python version.
> python -V Python 3.8.12
- If your python executable file name is python3, then you should run the command python3 –version, or python3 -V.
- 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.
- You can also use the python sys, platform module to get the Python version in your python source code.
- 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. >>>
- Run the command import sys to import the Python sys module.
- 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'>
- 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
- Besides the Python sys module, you can also use the Python platform module to get the python version information.
- 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'>