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

6
  • *Clarification: I am getting the error message here: print ("The length of list is : ", end="") Commented Jun 27, 2018 at 11:46
  • 1
    Could you specify what your error message is, to ensure the correct problem is addressed? Commented Jun 27, 2018 at 11:49
  • To work in python2, use print "The minimum element of list is : {}".format(min(ls)). Commented Jun 27, 2018 at 11:55
  • You are asking how to solve the problem in python2 or how to change the python version in sublime text? Because your question and accepted answer do not match Commented Jun 28, 2018 at 7:05
  • I want to change the python version in sublime text. Commented Jun 28, 2018 at 8:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

If this solves your doubt, either upvote or mark this as the answer. Desperately need some reputation. :P
got it. appreciate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.