After trying hard to find a way to make VS code 1.92 (31-07-2024) run a code selection with the keyboard shortcut shift + enter, with a 3.12 python compiler, in windows 10, I gave up.
My solution was to use python 3.11 instead.
When selecting one line the code would open the terminal and run just that line fine. However, multiple lines were not possible, the terminal would do nothing. When using the option “Run Selected Text” the message “:2: SyntaxWarning: invalid escape sequence ‘O'” would appear when a two line if function was selected.
The expected behaviour was to select a multiple line code and after hitting shift + enter that selected code would run.
The working solution at this moment is to use python version 3.11 instead which works (and before that with the 3.9 version was working).
Tryed solutions:
- unistall and reinstall vs code and python
check the “run selection” command in the keyboard shortcuts menu available in the settings wheel. - create a virtual environment in the interpreter
- use python.experiments.optInto: [“pythonREPLSmartSend”] as indicated by Microsoft (https://devblogs.microsoft.com/python/python-in-visual-studio-code-november-2023-release/)
Other consulted links (which did not work):
- https://github.com/microsoft/vscode-python/issues/22539
- https://superuser.com/questions/1461655/vs-code-shiftenter-does-not-send-code-to-python-interactive-why
tips:
- use the create restoration utility available in windows before you do something drastic
- the final install for python and VS code was for one user only, which is the standard option (when you select all users some options change and I don’t know if this is important)
- use the create paths option for the environment variables for both python (important) and VS code (don’t know if it is important)
Ricardo C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.