Course Practicalities

Visual Studio Code Basics


Visual Studio Code (VSCode) is an open-source code editor developed by Microsoft, which is available for the common operating systems. You can download it from https://code.visualstudio.com/.

To start VSCode, you can either click on the icon in the start menu or on the desktop, or you can open a terminal and type code. If you have not installed the command line tools, you can do so by opening VSCode and pressing Ctrl+Shift+P (or Cmd+Shift+P on Mac) and typing shell command and selecting Shell Command: Install 'code' command in PATH.

Starting VSCode

When you start VSCode, you will see a welcome screen. You can close it by clicking on the X in the top right corner. You will then see the main window of VSCode. It should look something like the following — in the following image, we have clicked the File Explorer icon (top leftmost icon) and opened VSCode in a folder called python-projects. Folders are created like nmormal folders in the operating system.

Fig 1. -- VSCode has been opened. In VSCode, the File Explorer icon has been selected, and the explorer shows an empty folder called 'PYTHON-PROJECTS'. Fig 1. — VSCode has been opened. In VSCode, the File Explorer icon has been selected, and the explorer shows an empty folder called ‘PYTHON-PROJECTS’.

Python support

To add Python support to VSCode, click on the Extensions icon (an icon with three joined boxes and a fourth distjoint box in the left sidebar) and type python in the search bar of the tab that opens up. You should see the Python extensions by Microsoft showing up. Click the Python extensions option and click the Install button to install them.

The following image shows a situation where the Python extension has been installed.

Fig 2. -- VSCode extensions icon has been selected, and python has been typed into the search bar. The found Python extension from Microsoft has been selected, and it has been installed. Fig 2. — VSCode extensions icon has been selected, and python has been typed into the search bar. The found Python extension from Microsoft has been selected, and it has been installed.

First Python program

Now that you have Python support installed to VSCode, you can create your first Python program. To do so, click on the File Explorer icon (top leftmost icon) and click on the New File button (next to the name of the folder — here python-projects). Name the file hello.py.

The figure below shows a situation, where the file has been created. The file is still empty.

Fig 3. -- A new file called 'hello.py' has been created to the folder 'PYTHON-PROJECTS' and the file has been opened for editing. Fig 3. — A new file called ‘hello.py’ has been created to the folder ‘PYTHON-PROJECTS’ and the file has been opened for editing.

Type in the following “Hello world!” program to the file.

print("Hello world!")

To run the program, click on the Run icon (a play button) that is in the right upper corner on the same level as hello.py file.

When you press the run icon — or “run the program” — for the first time, you will be asked to select the environment where you want to run the program. Select the Python version that is available (preferably at least 3.10). If Python has not been installed (or if the version is too old), you will be asked to install it. Follow the instructions to install Python.

Running the program will open a terminal window in the bottom of the VSCode window. Depending on the operating system and the chosen Python version, the output window will either just show the program output or also show the command used to run the program in addition to the output. In the figure below, we see the command and the output in the terminal window.

Fig 4. -- The contents of the file 'hello.py' have been run with Python. The output (and the Python command) are shown in the terminal at the bottom of VSCode. Fig 4. — The contents of the file ‘hello.py’ have been run with Python. The output (and the Python command) are shown in the terminal at the bottom of VSCode.