8

How do I execute run Shift + Enter only the selection of highlighted lines of code in vscode?

Edit: Maybe I should be clearer.

when hitting Shift + Enter the following error :

Unable to initialize device PRN

but when I right click > run python file in terminal the code works.

clover
  • 301
  • 1
  • 3
  • 13
  • After installing the Python Extension, I believe the shortcut is shift + enter. – Rahul P Dec 21 '19 at 07:41
  • What OS are you developing on? "Unable to initialize device PRN" suggests your pseudo-random number generator isn't up and running when you're doing development and that's causing the failure. – Brett Cannon Dec 24 '19 at 22:33
  • Does this answer your question? [VScode run code selection](https://stackoverflow.com/questions/49771589/vscode-run-code-selection) – starball Aug 31 '23 at 18:23

4 Answers4

8

Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.

That is what is on the VS Code documentation for Python and is what works for me. It is Shift + Enter not Ctrl

Note that this needs the Python 3 extension to be installed.

Rahul P
  • 2,493
  • 2
  • 17
  • 31
4

You can just select the lines and then right click > Run Selection/Line in Python Terminal.

Mac screenshot

stud3nt
  • 2,056
  • 1
  • 12
  • 21
2

Visual Studio Code supports working with Jupyter Notebooks natively, as well as through Python code files

read this article for any

https://code.visualstudio.com/docs/python/jupyter-support

hashem sheikhypour
  • 908
  • 12
  • 30
0

You could use the jupyter extension, then run cells together rather than whole files

https://code.visualstudio.com/docs/python/jupyter-support

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245