How to Provide Input to Running Programs in Sublime Text
While Sublime Text offers robust code editing capabilities, it lacks the inherent ability to handle user input via functions like raw_input() or input(). Users attempting to run Python scripts that require user input may encounter challenges.
Short-Term Solutions:
Build System Solutions:
If SublimeREPL is unsuitable or if you prefer to run code independently, consider creating custom build systems tailored to specific platforms:
Windows:
{ "cmd": ["start", "cmd", "/k", "c:/python38/python.exe", "$file"], "selector": "source.python", "shell": true, "working_dir": "$file_dir", "env": {"PYTHONIOENCODING": "utf-8"} }
Replace the path to the Python executable as necessary. After creating the build system, press Ctrl B to build and execute your program in a separate cmd window.
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"} }
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"} }
Universal Solution:
The Terminus plugin provides a platform-independent solution. Install it via Package Control 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]*)"" }
Adjust the Python executable path accordingly.
By following these approaches, you can effectively handle user input in Sublime Text, enabling you to create and run interactive code as required.
The above is the detailed content of How to Get User Input When Running Python Code in Sublime Text?. For more information, please follow other related articles on the PHP Chinese website!