2

I am using Sublime Text 3 in Ubuntu. The default Python is python2 which Sublime uses when I build program using Ctrl+B.

I am trying to run code as,

print(f"Player {row[0]} Vertically =")

but it gives error as follows because code is run with default Python

File "/home/gd/Desktop/python/python-revision/pp_13_iterators_iterables.py", line 41
  print(f"Player {row[0]} Vertically =")
                                      ^
SyntaxError: invalid syntax
[Finished in 0.0s with exit code 1]
[shell_cmd: python -u "/home/gd/Desktop/python/python-revision/pp_13_iterators_iterables.py"]
[dir: /home/gd/Desktop/python/python-revision]

However, when I run same program from terminal, using python3 it works as expected.

$ python3 pp_13_iterators_iterables.py
[1, 2, 1]
[2, 0, 1]
[1, 2, 1]
Vertically
Player 1 Vertically =

How can I change default build environment of Sublime Text to Python 3 and build it using shortcut as Ctrl+B?

I tried using alias as

alias python="/usr/bin/python3.6"

but it didn't work and gave same error as previous.

6

Changing alias just changes the default python version when python as a command is called. But Sublime uses path /usr/bin/python when building python source. I strongly suggest you to roll back the alias definition.

Now to build your code using python3, you can either install python-is-python3 package which is available on 20.04 and higher to invoke python3 using python or create a new build system and set that build system as default.

The package python-is-python3 should be avoided if there are some existing packages which depend on Python 2. If you don't have such packages, install the above said package using

sudo apt install python-is-python3

Alternatively, to add a new build system,

  • Go to Tools→Build System→New Build System

    Screenshot

  • A new tab will be opened to create a new build system. Paste this

    {
      "cmd": ["/usr/bin/python3", "-u", "$file"],
      "file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)",
      "selector": "source.python"
    }
    
  • Press Ctrl+S to save it. Rename it to Python3.

  • Now to make Python3 as default, go to Tools→Build System and select newly created build system, here Python3.

  • (optional) Create a new .py file to check whether it has been changed and paste this

    import sys
    print("Sublime is using: ", sys.version)
    
  • Build your source using Ctrl+B

  • If the output is like

    Sublime is using:  3.6.7 (default, Oct 22 2018, 11:32:17) 
    [GCC 8.2.0]
    [Finished in 0.1s]
    

That means that code is built using python3.

| improve this answer | |
  • It's working now as imgur.com/a/Zp7mfDF. – ganeshdeshmukh Dec 23 '18 at 15:55
  • 1
    @ganeshdeshmukh I just noticed that I mistyped python version in rolling back the alias. So have a look at there again and change your alias back to python2 using alias python="/usr/bin/python" – Kulfy Dec 23 '18 at 17:47
  • Hi @Kulfy, I tried the same approach that you have given above but my default version is still python2 not python3 on Sublime. I am on ubuntu-mate OS. Can you please let me know where am I making the mistake? – AlphaBet Sep 3 '19 at 4:16
  • @AlphaBet What is the output of ls -l /usr/bin/python? – Kulfy Sep 4 '19 at 12:35
  • @Kulfy, here is the output lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7 – AlphaBet Sep 25 '19 at 8:09
0

if you use python comes with anaconda, you probably need to use the following (it is the case for me at least)

{
  "cmd": ["path to anaconda/bin/python", "-u", "$file"],
  "file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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