VSCode

VSCode is the Integrated Development Environment (IDE) we will use to write and run our Python code. However, VSCode can do much more than that, which you can read more about on their website.

If you have not worked with an IDE before, it is easy to get overwhelmed. But once you get the hang of it, it can provide a lot of beneficial features. Here we will briefly go over how to navigate the user interface, work with version control, and run Python scripts interactively. Finally, you will create and run your first Python script.

User Interface

The user interface is divided into five different main components:

  • Activity Bar (A) - For switching between views in the Side Bar. Has context-specific indicators, such as the number of changes made since the last commit
  • Side Bar (B) - Contains different views, such as the Explorer (the projects files and folder) and Source Control (Git version control).
  • Editor (C): The main area for editing files. Editors open side by side vertically and/or horizontally, and can be grouped
  • Panels (D) - Panels can show output or debug information, errors and warnings, or the integrated terminal. Panels can also be moved to the right for more vertical space.
  • Status Bar (E) - Holds information about the active project and the files being edited

VSCode Interface

Source and more information.

Version/Source Control (Git)

Version control tracks changes in files over time. Here we use the Git version control system, which comes integrated with the VSCode IDE. It can be accessed in the Source Control view of the side bar. Additional functionality can be acquired by also installing the GitLens Extension. Importantly, Git is how we "save" our files when we are working in a cloud environment, as any changes not committed and synced with the remote repository will be lost if the Gitpod workspace is destroyed.

The most basic workflow for using Git in your project is:

  1. Work on your project, making changes to your files
  2. Inspect the changes in the Source Control view of the side bar
  3. Stage changes

Git Stage Changes

  1. Write a short commit message

Git Commit Message

  1. Commit changes
  2. Sync (push) the new commit to the remote repository

Source and more information.

Python Interactive Window

VSCode allows for running Python code interactively in the Python Interactive Window in a notebook-style format that is still compatible with regular python. The code can then be run in blocks, called code cells, defined by the special comment # %%.

hello.py
# %%msg = "Hello World"print(msg)# %%msg = "Hello again"print(msg)

Running the code cells can be done by clicking Run Cell in the editor, or pressing CTRL-ENTER/SHIFT-ENTER with the cursor inside the code block. The code is then run and the output displayed in the interactive window.

Python Interactive Window

Source and more information.

Create and Run a Python Script

This assumes that you are using the project template from the previous section.

  1. Create a new file in the src directory called my_first_script.py
  2. Add the following to the file:
my_first_script.py
# %%msg = "Python is Fun!"print(msg)
  1. Press CTRL-ENTER or click Run Cell in the editor

My First Script Code

  1. The interactive window will open automatically
  2. After an initial delay (on first open), you can see your output

My First Script Output