4

This question already has an answer here:

I have python 2.7 and 3.4 versions installed. But when I say say "python -V", it shows version as 2.7 not 3.4

How to tell ubuntu to use newest python? where is this setting configured ?

marked as duplicate by Florian Diesch, g_p, muru, Javier Rivera, Eric Carvalho Sep 25 '14 at 17:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    You don't. The python command will always be Python 2.x. If you want Python 3.x, you should use the python3 command to run it. Changing the python command will break all Python 2.x applications. – dobey Sep 24 '14 at 17:54
  • @FlorianDiesch At first sight definitely, but OP doesn't want a permanent change, but just wants to know how to define which version to use, running the code (see comment on the answer) – Jacob Vlijm Sep 24 '14 at 18:23
7

if you run python -V, you get: Python 2.7.6, However, if you run python3 -V, you get 3.4.0.

Running a script, you can specify the python version to use by using either:

python <script.py>

or:

python3 <script.py>

If the script or application is executable, and you run it without the python (either 2 or 3) command, you need to have the shebang in your script, in which you define the python version to use; either:

#!/usr/bin/env python

or:

#!/usr/bin/env python3

You cannot run code, written for python2 with python3 and vice versa, so you should not (try to) "tell" Ubuntu to use either version 2 or 3 in another way then above, according to the version of python, used in your code.

  • Thanks @Jacob, I wanted to run script with python version 3 and command python3 helped – Sachin Doiphode Sep 24 '14 at 18:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.