How can I find out what versions of Python I have?
I am using Ubuntu 14.04 (Trusty Tahr).
How can I find out what versions of Python I have?
I am using Ubuntu 14.04 (Trusty Tahr).
You can use python -V
(et al.) to show you the version of Python that the python
command resolves to. If that's all you need, you're done. But to see every version of python in your system takes a bit more.
In Ubuntu we can check the resolution with readlink -f $(which python)
. In default cases in 14.04 this will simply point to /usr/bin/python2.7
.
We can chain this in to show the version of that version of Python:
$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V'
/usr/bin/python2.7: Python 2.7.6
But this is still only telling us what our current python
resolution is. If we were in a Virtualenv (a common Python stack management system) python
might resolve to a different version:
$ readlink -f $(which python) | xargs -I % sh -c 'echo -n "%: "; % -V'
/home/oli/venv/bin/python: Python 2.7.4
This is real output.
The fact is there could be hundreds of different versions of Python secreted around your system, either on paths that are contextually added, or living under different binary names (like python3
).
If we assume that a Python binary is always going to be called python<something>
and be a binary file, we can just search the entire system for files that match those criteria:
$ sudo find / -type f -executable -iname 'python*' -exec file -i '{}' \; | awk -F: '/x-executable; charset=binary/ {print $1}' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V'
/home/oli/venv/bin/python: Python 2.7.4
/media/ned/websites/venvold/bin/python: Python 2.7.4
/srv/chroot/precise_i386/usr/bin/python2.7: Python 2.7.3
/srv/chroot/trusty_i386/usr/bin/python2.7: Python 2.7.6
/srv/chroot/trusty_i386/usr/bin/python3.4: Python 3.4.0
/srv/chroot/trusty_i386/usr/bin/python3.4m: Python 3.4.0
/usr/bin/python2.7: Python 2.7.6
/usr/bin/python2.7-dbg: Python 2.7.6
/usr/bin/python3.4: Python 3.4.0
/usr/bin/python3.4dm: Python 3.4.0
/usr/bin/python3.4m: Python 3.4.0
/web/venvold/bin/python: Python 2.7.4
It's obviously a pretty hideous command but this is again real output and it seems to have done a fairly thorough job.
readlink: extra operand '/usr/bin/python2.7'
Try 'readlink --help' for more information.
python -v
would return the version and executed this as root. it printed out a long list of things, but at the top it said installing zipimport hook
and now it seems as if i'm in a program which i have no idea how to get out of, let alone uninstall?? can you help me out here
– oldboy
Aug 14 '17 at 22:14
-V
flag for version is case-sensitive. python -v
put you in a Python console with verbose messages turned on. Control + D to get out. Or write exit()
and press return.
– Oli
Aug 15 '17 at 08:29
e.g. if I run "python --version" or "python3 --version", in both cases I get "Python 3.7.6", which if I run your command I get all sorts of versions apart from 3.7.6 i.e. I get 3.6.9 files, 3.9.13, older version of 3.5.2 but nowhere 3.7.6
Also, I run "which python" and "which python3" to see where it's taking it from, and seems to be picking it up from my anaconda3 installation:
/home/user/anaconda3/bin/python3
Any suggestions why?
– Angelo Sep 05 '22 at 05:57echo $PATH
should tell you the what, but I can't explain why. I'm not familiar with conda. There may be something in ~/.bashrc or elsewhere automatically setting the path.
– Oli
Sep 06 '22 at 08:22
Type following in the terminal (Ctrl+Alt+T):
python -V
or
python --version
You can find a list of options/parameters for many commands in the terminal by typing the command followed by --help
Example:
python --help
Manual/manpages also available for most of such CLI which can be displayed by man <command>
(Ex: man python
)
From man python
:
COMMAND LINE OPTIONS
-V , --version
Prints the Python version number of the executable and exits.
There is also python3
installed on many machines, so you can do:
python3 --version
to find out what python 3.x you are running.
python --version
and
python2 --version
show the version of Python 2.x,
python3 --version
the installed version of Python 3.x
If you want to see all versions of Python available as commands in Bash, run compgen -c python
. E.g:
$ compgen -c python | sort -u
python
python2
python2.7
python3
python3.4
python3.4m
python3m
If you want to get the version of each of the above:
compgen -c python | sort -u | grep -v -- '-config$' | while read -r p; do
printf "%-14s " "$p"
"$p" --version
done
python Python 2.7.6
python2 Python 2.7.6
python2.7 Python 2.7.6
python3 Python 3.4.3
python3.4 Python 3.4.3
python3.4m Python 3.4.3
python3m Python 3.4.3
Notes:
This ignores aliases. For example if I had alias python=python3
, the above would still show 2.7 for python
. To include aliases, you could use eval
(though this might be unsafe):
eval "$p --version"
I'm filtering out the python*-config
programs with grep -v
since they don't support the --version
flag. For example:
$ python3-config --version
Usage: /usr/bin/python3-config --prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--abiflags|--configdir
zsh
. I guess compgen
is bash specific? Or at least, it seems to exist in zsh
as well, but compgen -c python | wc -l
reports over 6000 matches for me, making it pretty useless.
– bluenote10
Jun 01 '23 at 15:46
compgen
doesn't even exist in my Zsh.
– wjandrea
Jun 01 '23 at 17:25
When you run python
in the terminal, it will produce output like this:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
See the first line - Python 2.7.6
.
Also run python3
. I have 3.4.1
Python 3.4.1 (default, Jul 31 2014, 12:46:17)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
However, this won't show you them all. If you have 2 or more python 3.X.x versions, you will only see the latest one.
By default on 14.04, you have 2.7.6 and 3.4.0. As you can see, I have updated to 3.4.1. I know that I still have 3.4.0 because when I go to /usr/bin
I see python3.4
, and if I run /usr/bin/python3.4
in the command line, I get 3.4.0, and running /usr/local/bin/python3.4
gives me 3.4.1
My pronouns are He / Him
PATH
environment variable? Why would running python3
always run the highest (sub)version of python3 that is installed? Or, in practice, won't it show whatever version the /usr/bin/python3
symbolic link points to, which need not be the latest?
– Eliah Kagan
Jul 31 '14 at 18:43
/usr/bin
one...
– Tim
Jul 31 '14 at 18:45
env
in hashbang lines to find whichever interpreted may be system-preferred in common in Python scripts). I'm not sure if that's what you mean by differences between some programs and others.
– Eliah Kagan
Jul 31 '14 at 18:50
You can also check Python version from code itself using platform
module from standard library. There are two functions: platform.python_version()
(returns string) and platform.python_version_tuple()
(returns tuple). Script:
import platform
print(platform.python_version())
print(platform.python_version_tuple())
Running:
$ python test.py
3.4.1
('3', '4', '1')
$ python2.7 test.py
2.7.8
('2', '7', '8')
sys.version
or sys.version_info
.
– Matt Nordhoff
Aug 02 '14 at 16:31
Easily, open the terminal and do the following:
Write
python
to verify your 2.x version In my case, it will appear:
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
For the 3.x, write:
python3
In my case, it appears:
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
For both cases, to get out from Python shell, write:
exit()
In shell terminal
$ which -a python
lists all your python
.
$ which -a python2.7
lists all your python2.7
.
$ /usr/bin/python -V
gives information about the version of /usr/bin/python
.
check in python script
here is an ilustration in ipython shell:
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
python -v
would return the version and executed this as root. it printed out a long list of things, but at the top it said installing zipimport hook
and now it seems as if i'm in a program which i have no idea how to get out of, let alone uninstall?? can you help me out here
– oldboy
Aug 14 '17 at 22:14
python -v
means interactive interpreter in verbose mode, with a freakish flood of detail. Big-v python -V
gets the version. Exiting the interpreter is easy and consistent. It's either Ctrl-Z
or Ctrl-D
or Ctrl-Break
or exit()
or quit()
or sys.exit()
. Okay it's the opposite of easy and consistent.
– Bob Stein
Mar 28 '21 at 15:36
For bash script to recognize which python command to use:
#!/usr/bin/env bash
usage() {
echo "Put your help/usage explanation here"
exit 1
}
get_python_version_to_use() {
if python3 --version 2>&1 | grep -q "Python 3" ; then
echo "python3"
elif python --version 2>&1 | grep -q "Python 3"; then
echo "python"
elif python --version 2>&1 | grep -q "Python 2" ; then
echo "python"
else
echo "Error: Python not found";
usage
fi
}
PYTHON_COMMAND=$(get_python_version_to_use)
echo $PYTHON_COMMAND
Now you can use it to run your script in the bash shell:
PYTHON_COMMAND=$(get_python_version_to_use)
$PYTHON_COMMAND my_script.py
And it will use python3 or python2 according to what installed on your machine (Preferring python3)
ls /usr/bin | grep python
– Tejas Kale Aug 01 '14 at 13:06ls /usr/bin/python*
(orls /usr/bin/*python*
if you really want files withpython
anywhere in the name). That way,ls
still formats its output for a terminal (and you get multiple columns and, with the defaultls
alias in Ubuntu, colorization). – Eliah Kagan Aug 15 '17 at 00:03