pythonuser-inputsublimereplsublimetext4

How can I avoid NameError from input() in Sublime Text 4, using SublimeREPL on Mac OS 12.1?


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:

  1. Install 'Package Control'
  2. Install 'SublimeREPL'
  3. Choose 'Tools > Build System > Build New System'
  4. Paste the following:
{
    "target":"run_existing_window_command",
    "id":"repl_python_run",
    "file":"config/python/Main.sublime-menu"
}
  1. Save as 'Python_Run' and close tab
  2. Open a new tab and save as 'test.py'
  3. Choose 'Tools > Build System > Python_Run'
  4. Paste the following text code:
name = input("Enter your name: ")

print(name)
  1. Choose 'Tools > Build' to run.

  2. 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.


Solution

  • 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: