0

Currently I have installed sublime text 3 in Linux for python programming. When I am executing the piece of code, I am getting error, this error is because of Python version sublime 3 is using is 2.7. How can i change the python version from 2.x to latest 3.x in sublime text 3. Here is my piece of code

lis = [2, 1, 3, 5, 4]
# using len() to print length of list
print ("The length of list is : ", end="")
print (len(lis))
# using min() to print minimum element of list
print ("The minimum element of list is : ", end="")
print (min(lis))
# using max() to print maximum element of list
print ("The maximum element of list is : ", end="")
print (max(lis))

When I am running this program on python 2.x version, I am getting an error as "invalid syntax", but this error is not coming in python 3.x version. Kindly guide me to change the python version of sublime text 3 from 2.x to 3.x

khelwood
  • 55,782
  • 14
  • 81
  • 108
Debashis Sahoo
  • 5,388
  • 5
  • 36
  • 41

2 Answers2

7

If you want to change the python version in Sublime Text, first of all you have to install the two Python versions and know where you actually installed them.
Ensure that by the cmd line you can call a .py script with python2 and python3.
Then, in sublime text, go in Tools --> Build System --> New Build System....
In the windows that opens, copy-paste this code:

{
    "cmd": ["python3", "-i", "-u", "$file"],
    "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
    "selector": "source.python"
}

and save this script as Python3.sublime-build

you should now have the following values in Tools --> Build System:

Python # which is your Python2
Python3 # which is your Python3

note that where we wrote "python3", if that's not working, you may put the path to your python3 installation, such as "/usr/bin/python3"

Check at the end of this discussion for some tips.

Gsk
  • 2,929
  • 5
  • 22
  • 29
0

Duplicate of this question.

You cannot run the statement print('something: ', end='') in python 2. The reason being print is a literal in Python2 and cannot take arguments. Whereas in Python3, print is a function.

In order to print something like that in Python2, you can use this syntax -

print "The length of list is : ",

Notice the comma at the end of the statement.

Source

MaJoR
  • 954
  • 7
  • 20