I followed the instructions on the following short video to allow Python scripts to be run in Sublime Text.
https://www.youtube.com/watch?v=-d27eACbjnk
Steps were:
{
"target":"run_existing_window_command",
"id":"repl_python_run",
"file":"config/python/Main.sublime-menu"
}
name = input("Enter your name: ")
print(name)
Choose 'Tools > Build' to run.
Enter name as prompted.
However, when I enter a name, I receive the following NameError message:
Enter your name: Sean
Traceback (most recent call last):
File "test.py", line 1, in <module>
name = input("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'Sean' is not defined
I have found the only way around this is to enter the name in inverted commas. BUT the linked video demonstrates the above working without the need to use inverted commas for the input.
I'm new to Python and Sublime so hoping for an easy-to-understand fix.
This answer is assuming you're either running Linux or macOS and already have Python 3 installed on your system.
You're getting the NameError
because SublimeREPL is using Python 2, an old version that passed its end-of-life support over 2 years ago and was deprecated long before that. Not everyone has ported their codebases over to the current Python 3, however, which is why you still find it on some systems. If you're just starting to learn Python, you absolutely should start by using Python 3, which is the present and future of the language, then after you are proficient you can learn the differences between it and Python 2.
The good news is that you likely already have Python 3 installed on your system if you're using a recent release of either Linux or macOS, and it's relatively straightforward to point SublimeREPL to it. To start, open Sublime and select Preferences → Browse Packages…
on Linux or Sublime Text → Preferences → Browse Packages…
on macOS. Your system's file browser will open to a folder called Packages
that contains at least 2 subfolders - SublimeREPL
and User
(depending on how many plugins you've installed so far). Open the SublimeREPL
folder, then config
, then Python
. Drag the file called Main.sublime-menu
to your Sublime window and set its syntax to JSON (View → Syntax → JSON
).
Line 53 should be this:
"cmd": ["python", "-u", "$file_basename"],
Change python
to python3
:
"cmd": ["python3", "-u", "$file_basename"],
Additionally, in case you use the REPL directly in the future, change lines 22 and 39 so they say python3
as well.
Save the file, then switch back to your original Python file and use the Python_Run
build system. You should be able to use your original test code now and get the proper results.
For reference, the Sublime Text 4 Packages
folder is located in different places on different operating systems:
~/.config/sublime-text/Packages
~/Library/Application Support/Sublime Text/Packages
C:\Users\YourUserName\AppData\Roaming\Sublime Text\Packages
InstallationFolder\Sublime Text\Data\Packages