Running Python in VSCode

 

This is a short guide on how to use Visual Studio Code (VSCode). Our recommendation is that you for larger projects write your functions and classes as modules in VSCode, and only use Jupyter notebooks for presenting the results. You can also run Python directly in VSCode

 

1. Short-cuts


Use short-cuts for changing view:

  • New window: Ctrl+N

  • Open window: Ctrl+O

  • Close window: Ctrl+F4

  • Toggle window: Ctrl+tab

  • Locate symbol: Ctrl+Shift+O

  • Go to symbol: F12 (just peak Alt+F12)

  • Go to line: Ctrl+G

  • Zoom: Ctrl++ / Ctrl+-

  • Zen mode: Ctrl+K,Z


Use smart short cuts for editing:

  • Move current line: Alt+ /

  • Copy current line: Alt+Shift+ /

  • Cut current line: Ctrl+X

  • Indent line: Tab

  • Deindent line: Shift+Tab

  • Fold/unfold:

    • single block: Ctrl+Shift+´ / Ctrl+Shift+å

    • all: Ctrl+K, Ctrl+1 / Ctrl+K, Ctrl+J

  • Toggle commment: Ctrl+'

  • Select multiple occurances: Ctrl+D (undo: Ctrl+U)

  • Select columns: Ctrl+Shift+Alt


Change settings Ctrl+,

Change short cuts? Ctrl+K, Ctrl+S

 

2. Execute Python file


The following step-by-step guide show you how to create a file, write some Python code, and execture it.

  1. Open folder to work in: Ctrl+K, Ctrl+O

  2. Create new file: Ctrl+N

  3. Save new file: Ctrl+S (write e.g. test.py)

  4. Write code: Write the following lines of code:

    message = 'hello world'
    print(message)

  5. Exectue file: Ctrl+Shift+P + Python: Run Python File in Terminal


Note: The bottom-left corner should say Python 3.x 64-bit {'base': conda} when you have a .py file open. Else click on what ever else it says and choose this.

 

3. Interactive session

 

  1. Selecet first line of code and press Shift+Enter (interactive session launched)

  2. Select second line of code and press Shift+Enter (code is executed)

  3. In the interactive window write print(message) and run with Shift+Enter

    Note: You can also select multiple lines to run.

 

4. Find errors in your code

 

  1. Write the following lines of code:

    message = 'hello world'
    print(message)      
    print(messages) 

  2. Save file: Ctrl+S

  3. Show all errors: Ctrl+Shift+M

  4. Move the cursor to a line in the python file

  5. Find next error: F8

 

More infromation