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.
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 installpython-is-python3
package which is available on 20.04 and higher to invokepython3
usingpython
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 usingAlternatively, to add a new build system,
Go to Tools→Build System→New Build System
A new tab will be opened to create a new build system. Paste this
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 thisBuild your source using Ctrl+B
If the output is like
That means that code is built using
python3
.if you use python comes with anaconda, you probably need to use the following (it is the case for me at least)