Handling User Input in Sublime Text during Program Execution
Unable to send input to a running Python program within Sublime Text? This is a common issue due to the limitations of Sublime Text itself. It doesn't support handling input from functions like raw_input() or input().
Solutions:
1. SublimeREPL Plugin:
Install the SublimeREPL plugin to transfer or execute code sections through a running REPL. Configure Main.sublime-menu files to set up your preferred interpreter.
2. Custom Build System:
Create a custom build system for Windows, macOS, or Linux. For Windows:
{ "cmd": ["start", "cmd", "/k", "c:/python38/python.exe", "$file"], "selector": "source.python", "shell": true, "working_dir": "$file_dir", "env": {"PYTHONIOENCODING": "utf-8"} }
For macOS:
{ "shell_cmd": "osascript -e 'tell app \\"Terminal\\" to do script \\"cd $file_path &\& python3 -u $file\\"'"", "working_dir": "$file_path", "selector": "source.python", "env": {"PYTHONIOENCODING": "utf-8"} }
For Linux:
{ "shell_cmd": "gnome-terminal --working-directory=$file_path -- bash -c 'python3 -u \\"$file\\" &\& read -n 1 -s -r'"", "working_dir": "$file_path", "selector": "source.python", "env": {"PYTHONIOENCODING": "utf-8"} }
3. Terminus Plugin (Recommended):
Install the Terminus plugin and create the following build system:
{ "target": "terminus_exec", "cancel": "terminus_cancel_build", "cmd": [ "/path/to/python", "-u", "$file" ], "working_dir": "$file_path", "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)"" }
Terminus provides a convenient way to interact with your program in the build panel below your code.
The above is the detailed content of How Can I Handle User Input in Sublime Text While Running a Python Program?. For more information, please follow other related articles on the PHP Chinese website!