Check Python Version

Python Check Python Version

As you improve your basic Python skills, you’re going to find out (usually the hard way) that Python 2.x and Python 3.x are not as compatible as you expected. That’s why it’s important to establish Python version before starting a new project or deploying it into new environment.

Quickest Way to Check Python Version

Simply use -V option to one of the Python binaries available on your system:

[email protected]:~ # python -V
Python 2.7.16
[email protected]:~ # python3 -V
Python 3.7.3

You’ll get the same result if you use –version instead of -V:

[email protected]:~ # python --version
Python 2.7.16
[email protected]:~ # python3 --version
Python 3.7.3

Check Python Version By Running python Command

If you start Python in interpreter mode, it should report version information at the very start:

[email protected]:~ # python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

You’ll need to press Ctrl+D (or Cmd+D in macOS) to stop Python.

Check Python Version using sys.version

Sometimes you need to check Python version programmatically, from within your Python script. Then your best bet is to use the sys module:

[email protected]:~ $ python -c "import sys; print(sys.version)"
2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0]

See Also

Keep Learning

Follow me on Facebook, Twitter or Telegram:
I learn with Educative: Educative I'm also a fan of SetApp for macOS: SetApp for macOS
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
17 Nov 2022

Video: awk delimiters

25 Oct 2022

macOS Ventura 13.0

21 Oct 2022

Video: How To Generate ed25519 key

Recent Tweets