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:
- Install 'Package Control'
- Install 'SublimeREPL'
- Choose 'Tools > Build System > Build New System'
- Paste the following:
{
"target":"run_existing_window_command",
"id":"repl_python_run",
"file":"config/python/Main.sublime-menu"
}
- Save as 'Python_Run' and close tab
- Open a new tab and save as 'test.py'
- Choose 'Tools > Build System > Python_Run'
- Paste the following text code:
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
NameErrorbecause 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 orSublime Text → Preferences → Browse Packages…on macOS. Your system's file browser will open to a folder calledPackagesthat contains at least 2 subfolders -SublimeREPLandUser(depending on how many plugins you've installed so far). Open theSublimeREPLfolder, thenconfig, thenPython. Drag the file calledMain.sublime-menuto your Sublime window and set its syntax to JSON (View → Syntax → JSON).Line 53 should be this:
Change
pythontopython3:Additionally, in case you use the REPL directly in the future, change lines 22 and 39 so they say
python3as well.Save the file, then switch back to your original Python file and use the
Python_Runbuild system. You should be able to use your original test code now and get the proper results.For reference, the Sublime Text 4
Packagesfolder is located in different places on different operating systems:~/.config/sublime-text/Packages~/Library/Application Support/Sublime Text/PackagesC:\Users\YourUserName\AppData\Roaming\Sublime Text\PackagesInstallationFolder\Sublime Text\Data\Packages