564

I have to run a Python script on a Windows server. How can I know which version of Python I have, and does it even really matter?

I was thinking of updating to the latest version of Python.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali_IT
  • 7,551
  • 8
  • 28
  • 44
  • 2
    Yes, the (major) version number matters. Make sure you select python documentation that matches your python version. – André Caron Jan 18 '12 at 21:47
  • If you want to also find out what Python is associated with `.py` files you might be interested in a batch file in [How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?](http://stackoverflow.com/questions/7825780/) – Piotr Dobrogost Jan 19 '12 at 09:33
  • 2
    Related: Is there a guaranteed way to see all versions of Python that are available/installed on your Linux system? – Dennis Nov 12 '12 at 07:23
  • 1
    @esteban: None of the answers are Windows-specific, and titles don't need tags anyway. Rolled back. – Wooble Jan 17 '14 at 19:43
  • @Wooble So, the question could be more general? – Esteban Cacavelos Jan 17 '14 at 20:06
  • 1
    Related thread to check python version from python script/program - [How do I check what version of Python is running my script?](https://stackoverflow.com/q/1093322/465053) – RBT Jul 20 '18 at 05:12

26 Answers26

722
python -V

http://docs.python.org/using/cmdline.html#generic-options

--version may also work (introduced in version 2.5)

Dov
  • 15,530
  • 13
  • 76
  • 177
theglauber
  • 28,367
  • 7
  • 29
  • 47
  • 3
    On my Windows 8.1 Pro machine, Python 2.7.10 outputs `Python 2.7.10` for `-V` and `--version`; and Python 3.4.3 similarly outputs `Python 3.4.3` for both options too. – J0e3gan Jun 02 '15 at 06:00
  • 2
    you could have several other versions of python too, something like this ``sudo find / -iname python`` would probably discover them. – PatrickT Jun 19 '16 at 11:57
  • 6
    @PatrickT this post was about python on windows server, sudo and find would confuse some newbies, as they wouldn't work on windows – Michael B. Feb 06 '17 at 16:32
  • 26
    not to be confused with `python -v` (lowercase v) which increases the logging verbosity – joel Oct 01 '18 at 13:11
  • 5
    If you are building an API please consider allowing both `-v` and `-version` aliases. Clearly about 500 developers had to look this up and upvote this answer for Python on SO. That's a bad design – P.Brian.Mackey Jan 21 '19 at 16:18
  • Does not work for me. When I use it in the script, I get the error `NameError: name 'python' is not defined`. When I use it in the terminal of PyCharm, I get the error (translated) "the name"python" was not recognized as a Cmdlet" or an exectuable program", when I use it in the Python Console I get the error message "NameError: name 'python' is not defined" – PeterBe Aug 29 '22 at 13:40
167

In a Python IDE, just copy and paste in the following code and run it (the version will come up in the output area):

import sys
print(sys.version)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pzp
  • 6,249
  • 1
  • 26
  • 38
151

Python 2.5+:

python --version

Python 2.4-:

python -c 'import sys; print(sys.version)'
mcandre
  • 22,868
  • 20
  • 88
  • 147
Abbas
  • 6,720
  • 4
  • 35
  • 49
  • Note: The interpreter may not use the same Python version as the one that runs your scripts. I think there's some circumstances where, by default, your interpreter is Python 3, but your scripts are run in Python 2 (need `#!python3` as the first line). – leewz Jul 13 '14 at 18:53
  • 1
    NOTE: On Windows, you need to go to the "Python (command line)" to enter the above steps. – HPWD Dec 10 '14 at 14:25
  • This answer is more useful, the script would get ability, to run or not. – AjayKumarBasuthkar Feb 24 '15 at 20:01
  • This worked for me once I used double-quotes around the statement. `python -c "import sys; print sys.version"` – S3DEV Feb 05 '18 at 11:26
  • Can you update your answer wrt. to Windows? See other comments. Thanks in advance. – Peter Mortensen Nov 24 '19 at 17:00
34

At a command prompt type:

python -V

Or if you have pyenv:

pyenv versions
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Brian Willis
  • 22,768
  • 9
  • 46
  • 50
27

Although the question is "which version am I using?", this may not actually be everything you need to know. You may have other versions installed and this can cause problems, particularly when installing additional modules. This is my rough-and-ready approach to finding out what versions are installed:

updatedb                  # Be in root for this
locate site.py            # All installations I've ever seen have this

The output for a single Python installation should look something like this:

/usr/lib64/python2.7/site.py
/usr/lib64/python2.7/site.pyc
/usr/lib64/python2.7/site.pyo

Multiple installations will have output something like this:

/root/Python-2.7.6/Lib/site.py
/root/Python-2.7.6/Lib/site.pyc
/root/Python-2.7.6/Lib/site.pyo
/root/Python-2.7.6/Lib/test/test_site.py
/usr/lib/python2.6/site-packages/site.py
/usr/lib/python2.6/site-packages/site.pyc
/usr/lib/python2.6/site-packages/site.pyo
/usr/lib64/python2.6/site.py
/usr/lib64/python2.6/site.pyc
/usr/lib64/python2.6/site.pyo
/usr/local/lib/python2.7/site.py
/usr/local/lib/python2.7/site.pyc
/usr/local/lib/python2.7/site.pyo
/usr/local/lib/python2.7/test/test_site.py
/usr/local/lib/python2.7/test/test_site.pyc
/usr/local/lib/python2.7/test/test_site.pyo
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2099484
  • 4,417
  • 2
  • 21
  • 9
26

When I open Python (command line) the first thing it tells me is the version.

poy
  • 10,063
  • 9
  • 49
  • 74
  • 1
    This should be the right answer for windows. I tried for hours with "python" but didn't work. Then I typed "Python" which worked. +1 for correctly providing the command. – Natasha Oct 03 '18 at 21:39
  • Do you mean pressing the Windows key and typing *"Python"*? Or something else? – Peter Mortensen Nov 24 '19 at 17:06
13
In [1]: import sys

In [2]: sys.version
2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec  6 2015, 18:08:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

In [3]: sys.version_info
sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)

In [4]: sys.version_info >= (2,7)
Out[4]: True

In [5]: sys.version_info >= (3,)
Out[5]: False
wsdzbm
  • 3,096
  • 3
  • 25
  • 28
13

In short:

Type python in a command prompt

Simply open the command prompt (Win + R) and type cmd and in the command prompt then typing python will give you all necessary information regarding versions:

Python version

mar10
  • 14,320
  • 5
  • 39
  • 64
10

I have Python 3.7.0 on Windows 10.

This is what worked for me in the command prompt and Git Bash:

To run Python and check the version:

py

To only check which version you have:

py --version

or

py -V    # Make sure it is a capital V

Note: python, python --version, python -V,Python, Python --version, Python -V did not work for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dahiana
  • 1,171
  • 2
  • 9
  • 11
  • `python -V` works back to Fedora 1 with Python 2.2.3. `py --version` results in *`command not found`*. `python --version` results in *`unknown option: --`*. – jww Dec 29 '18 at 17:10
8

Use

python -V

or

python --version

NOTE: Please note that the "V" in the python -V command is capital V. python -v (small "v") will launch Python in verbose mode.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yogesh Yadav
  • 4,557
  • 6
  • 34
  • 40
8
>>> import sys; print('{0[0]}.{0[1]}'.format(sys.version_info))
3.5

so from the command line:

python -c "import sys; print('{0[0]}.{0[1]}'.format(sys.version_info))"
Baczek
  • 1,179
  • 1
  • 13
  • 23
7

You can get the version of Python by using the following command

python --version

You can even get the version of any package installed in venv using pip freeze as:

pip freeze | grep "package name"

Or using the Python interpreter as:

In [1]: import django
In [2]: django.VERSION
Out[2]: (1, 6, 1, 'final', 0)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pooja
  • 1,254
  • 14
  • 17
7

To check the Python version in a Jupyter notebook, you can use:

from platform import python_version
print(python_version())

to get version number, as:

3.7.3

or:

import sys
print(sys.version)

to get more information, as

3.7.3 (default, Apr 24 2019, 13:20:13) [MSC v.1915 32 bit (Intel)]

or:

sys.version_info

to get major, minor and micro versions, as

sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
nucsit026
  • 652
  • 7
  • 16
6

On Windows 10 with Python 3.9.1, using the command line:

    py -V

Python 3.9.1

    py --version

Python 3.9.1

    py -VV

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit 
(AMD64)]
Sam
  • 223
  • 3
  • 7
  • I've updated this for version 3.9.1, as the previous syntax in Windows 10 now takes you to a link to Python in the Microsoft store, rather than displaying version details (this can be changed here https://superuser.com/questions/1437590/typing-python-on-windows-10-version-1903-command-prompt-opens-microsoft-stor) . The original answer was for python 3.6, the current version is 3.9.1. The syntax has since changed from *python* to *py*. – Sam Dec 16 '20 at 19:15
  • also the -V needs to be capitalised, otherwise the command line will just open python. If this happens, just Ctrl+Z and Enter to exit. – Sam Dec 16 '20 at 19:21
5

If you are already in a REPL window and don't see the welcome message with the version number, you can use help() to see the major and minor version:

>>>help()
Welcome to Python 3.6's help utility!
...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
OrigamiEye
  • 864
  • 1
  • 12
  • 31
5

Typing where python on Windows into a Command Prompt may tell you where multiple different versions of python are installed, assuming they have been added to your path.

Typing python -V into the Command Prompt should display the version.

Pro Q
  • 4,391
  • 4
  • 43
  • 92
4

If you have Python installed then the easiest way you can check the version number is by typing "python" in your command prompt. It will show you the version number and if it is running on 32 bit or 64 bit and some other information. For some applications you would want to have a latest version and sometimes not. It depends on what packages you want to install or use.

Sagar_c_k
  • 57
  • 4
  • 2
    This is practically identical to [poy's answer](https://stackoverflow.com/questions/8917885/which-version-of-python-do-i-have-installed/8917940#8917940). – Peter Mortensen Nov 24 '19 at 16:48
4

To verify the Python version for commands on Windows, run the following commands in a command prompt and verify the output:

c:\> python -V
Python 2.7.16

c:\> py -2 -V
Python 2.7.16

c:\> py -3 -V
Python 3.7.3

Also, to see the folder configuration for each Python version, run the following commands:

For Python 2, 'py -2 -m site'
For Python 3, 'py -3 -m site'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aamir M Meman
  • 1,645
  • 14
  • 15
4

The default Python version and the paths of all installed versions on Windows:

py -0p

One-Liners:

❯❯  python -V | cut -c8-
3.11.0

❯❯ ~ python -VV
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

❯❯ ~ python --version
Python 3.11.0

❯❯ ~ py --list
-V:3.11 *        Python 3.11 (64-bit)
-V:3.10          Python 3.10 (64-bit)
-V:3.9           Python 3.9 (64-bit)

❯❯ ~ py -V
Python 3.11.0

❯❯ ~ py -VV
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

❯❯ ~ py --version
Python 3.11.0

❯❯ ~ py -0p
-V:3.11 *        W:\Windows 10\Python311\python.exe
-V:3.10          W:\Windows 10\Python310\python.exe
-V:3.9           C:\Program Files\Python39\python.exe

❯❯ ~ python -c 'import sys; print(".".join(sys.version.split(".")[0:2]))'
3.11

❯❯ ~ python -c 'import sys; print(sys.version)'
3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

❯❯ ~ python -c 'import sys; print((str(sys.version_info.major) +"."+ str(sys.version_info.minor)))'
3.11

❯❯ ~ python -c 'import sys; print(sys.version_info)' sys.version_info(major=3, minor=11, micro=0, releaselevel='final', serial=0)

❯❯ ~ python -c 'import platform; print(platform.python_version()[:-2])'
3.11

❯❯ ~ python -c 'import platform; print(platform.python_version())'
3.11.0

❯❯ ~ python -c 'import platform; print("{0[0]}.{0[1]}".format(platform.python_version_tuple()))'
3.11

❯❯ ~ python -c 'import platform; print(platform.python_version_tuple())'
('3', '11', '0')
Szczerski
  • 839
  • 11
  • 11
3

For me, opening CMD and running

py

will show something like

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Is this on Windows? What Python implementation? [Anaconda](https://en.wikipedia.org/wiki/Anaconda_(Python_distribution))? (Respond by [editing your answer](https://stackoverflow.com/posts/34511331/edit), not here in comments (as appropriate)) – Peter Mortensen Nov 24 '19 at 16:08
3

Just create a file ending with .py and paste the code below into and run it.

#!/usr/bin/python3.6

import platform
import sys

def linux_dist():
  try:
    return platform.linux_distribution()
  except:
    return "N/A"

print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_dist(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
))

If several Python interpreter versions are installed on a system, run the following commands.

On Linux, run in a terminal:

ll /usr/bin/python*

On Windows, run in a command prompt:

dir %LOCALAPPDATA%\Programs\Python
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Don Matteo
  • 85
  • 1
  • 8
3

There are two simple ways to check for the version of Python installed.

Run any of the codes on the command prompt:

python -v

or

python --version
Happy N. Monday
  • 371
  • 1
  • 3
  • 8
1

For the latest versions please use the below command for the python version

py -V
Jb-99
  • 153
  • 1
  • 10
0

Mostly usage commands:

python -version

Or

python -V
KittoMi
  • 411
  • 5
  • 19
0

For bash scripts this would be the easiest way:

# In the form major.minor.micro e.g. '3.6.8'
# The second part excludes the 'Python ' prefix 
PYTHON_VERSION=`python3 --version | awk '{print $2}'`
echo "python3 version: ${PYTHON_VERSION}"
python3 version: 3.6.8

And if you just need the major.minor version (e.g. 3.6) you can either use the above and then pick the first 3 characters:

PYTHON_VERSION=`python3 --version | awk '{print $2}'`
echo "python3 major.minor: ${PYTHON_VERSION:0:3}"
python3 major.minor: 3.6

or

PYTHON_VERSION=`python3 -c 'import sys; print(str(sys.version_info[0])+"."+str(sys.version_info[1]))'`
echo "python3 major.minor: ${PYTHON_VERSION}"
python3 major.minor: 3.6
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
-2

Open a command prompt window (press Windows + R, type in cmd, and hit Enter).

Type python.exe

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Erina
  • 81
  • 1
  • 2
  • 13
  • This is practically identical to [poy's answer](https://stackoverflow.com/questions/8917885/which-version-of-python-do-i-have-installed/8917940#8917940). – Peter Mortensen Nov 24 '19 at 16:48