How can I print the version number of the current Python installation from my script?
How can I print the version number of the current Python installation from my script?
Try
import sys
print(sys.version)
This prints the full version information string. If you only want the python version number, then Bastien Léonard's solution is the best. You might want to examine the full string and see if you need it or portions of it.
If you are using jupyter notebook Try:
!python --version
If you are using terminal Try:
python --version
If you would like to have tuple type of the version, you can use the following:
import platform
print(platform.python_version_tuple())
print(type(platform.python_version_tuple()))
# <class 'tuple'>
If you specifically want to output the python version from the program itself, you can also do this. Uses the simple python version printing method we know and love from the terminal but does it from the program itself:
import os
if __name__ == "__main__":
os.system('python -V') # can also use python --version