How to check the version of Python
There are two main ways to check the version of Python: using the command line and from within Python code itself.
Checking Python Version Using the Command Line
The command line provides a quick way to check the Python version installed on your system. Follow these steps:
- Open a command prompt or terminal.
- Type the following command and press Enter:
python --version
# or
python -V
If you have both Python 2 and Python 3 installed, you can use the following command to check the Python 3 version specifically:
python3 --version
# or
python3 -V
Checking Python Version Using Code(sys, platform)
Checking Python Version using the ‘sys’
1import sys
2print(sys.version) # 3.10.2 (main, Apr 24 2022, 12:51:41) [Clang 13.1.6 (clang-1316.0.21.2)]
If you want to retrieve individual version numbers, you can use ‘sys.version_info’.
1import sys
2print(sys.version_info) # sys.version_info(major=3, minor=10, micro=2, releaselevel='final', serial=0)
You can retrieve it using a named tuple like this.
Checking Python Version using the ‘platform’
1import platform
2print(platform.python_version()) # 3.10.2
If you want to retrieve individual version numbers, you can use ‘platform.python_version_tuple()’.
1import platform
2print(platform.python_version_tuple()) # ('3', '10', '2')
Please be careful as it is not a named tuple.
Checking Python Version on Different Operating Systems
Checking Python Version on Windows
To check the Python version on Windows, follow these steps:
- Open a command prompt by pressing
Win + R
, typing “cmd,” and pressing Enter. - In the command prompt, type the following command and press Enter:
python --version
# or
python -V
Checking Python Version on Mac
To check the Python version on a Mac, follow these steps:
- Open a terminal by navigating to “Applications” > “Utilities” > “Terminal.”
- In the terminal, type the following command and press Enter:
python --version
# or
python -V
Checking Python Package (Library) Versions
Basic Package Version Checking
To check the versions of packages installed in your Python environment, you can use the following command in the command prompt or terminal:
pip freeze
Executing this command will display a list of installed packages along with their versions.
Checking the Version of a Specific Package (e.g., pandas)
To check the version of a specific package, such as pandas, you can use the following Python code:
1import pandas
2print(pandas.__version__)
Checking if Python is Installed
To check if Python is installed on your system, follow these steps:
- Open a command prompt or terminal.
- Type the following command and press Enter:
python --version
If Python is installed, it will display the installed version. Otherwise, it will show an error message indicating that the command is not recognized.