How was Visual Studio Code created

Visual Studio Code and the Python Console

Overview

Teaching: 30 min
Exercises: 0 min
Questions
  • What is an IDE?

  • How can you run Python code?

Objectives
  • Start VS Code and open a project folder

  • Dealing with the Python console

Start Visual Studio Code

An “Integrated Development Environment” (IDE) like VS Code is basically a text editor with many extra functions that make working with source code easier. A Python program will typically only consist of a single source code file at the beginning, but as it becomes more complex, the functionality will be distributed across several source code files. It also makes sense for the course to save the source texts together in folders. For example, create a subfolder in your user folder and a subfolder below it. You can freely choose the location of the project folder and, depending on the operating system, the user folder is located elsewhere.

Start Visual Studio Code as described in the setup steps. You should then be able to see a sidebar on the left in addition to the welcome dialog in the center.

There are various sub-areas in the sidebar, between which you can switch by clicking on the respective icon. First you should open the “Explorer” sub-area by clicking on the first icon at the top left. Then open the newly created project folder in VS Code by selecting “Open Folder” in the Explorer sidebar.

Alternatively, you can also open a project folder using the “File -> Open Folder…” menu item.

At the moment the folder is empty, so no files are displayed in Explorer either. If you move the mouse over the area with the folder name, two small icons appear with which you can either create a new file (first icon) or create a subfolder (second icon).

Create a text file with the name by clicking on the icon for the new file, entering the file name and pressing the Enter key.

You can now write any text in this text file. As with other text editors, VS Code will indicate that the file has been modified. If there are unsaved changes, a round circle will appear at the top next to the file name. Save the changes with the keyboard shortcut + or the menu item “File -> Save”. Then the circle becomes a cross with which you can close the text editor window. You can open the file again by clicking on the file in the Explorer sidebar. When you open the project folder in your system's file manager, you should be able to see the new file.

Python console

So far we haven't done anything with VS Code that is specific to Python. Python programs are executed via the program that was installed by Miniconda. To start the program, you first have to open a terminal. You could start the system terminal, but VS Code already has a terminal built in. To start a new terminal in VS Code, click on “Terminal -> New Terminal” in the menu.

The terminal is then displayed in the lower area. Depending on the operating system, the terminal looks a little different, in MacOS and Linux a Unix shell is used (e.g. “bash” or “sh”). If you choose the command renew, a new terminal will be started, but the old one will still be there. You can switch back and forth between the different terminals using the “dropdown” box.

If you have covered the terminal, you can also display this view again using the “View -> Terminal” menu item.

Windows terminal selection

Windows has the “Command Prompt” or “Powershell” to choose from. It can happen that VS Code starts the Powershell. The “Command Prompt” is more suitable for this course. In the same selection in which you can switch between the open terminals, there is also an entry “Select Default Shell”. Select this entry and click on “Command Prompt” in the selection that follows.

Python is also just a program that can be started via a terminal command. In order for the correct version of Python to be found, you must first enable the Miniconda environment. To do this, run the following command:

After executing the command, it will appear at the beginning of the command prompt. Then you can run the program “python3” to start the interactive Python console:

In the Python console you can individual Python commands executed and tested. For example, you can type the following strings into the console and press Enter

Every single command that is completed with the Enter key can produce output as text that is displayed in the console as soon as the command is completed. For example, here is the output.

exercise

Enter a command that will display the string in the console.

solution

To exit the Python console again, enter the command

a. You are then back to the normal system terminal.

If you want to run again now, you do not have to activate the Conda environment again, as the prompt indicates that Conda is still active. If you exit VS Code or the Terminal, you may need to reactivate the Conda environment. Depending on how you have installed Miniconda, it can also be that the Conda environment is already activated when you start any terminal.

Source code files

However, Python programs typically consist of many instructions that are bundled in one or more source code files. A typical workflow can therefore be to try out a sequence of commands in the console and then copy them into the source code file.

Create a new file with the name and open it in VS Code's text editor. The file extension indicates that this is a Python source code file. Then add the following two lines to the text file and save the file:

You can now use the program again to run the Python code. Instead of typing each line interactively, enter the relative path to the file as an argument of the program call.

Congratulations, you ran your first own Python program! 🎉

If a file is passed as an argument, this instruction will be executed after instruction and all output, e.g. made with the Python command.

core items

  • Simple expressions can be executed through interactive input in the Python console.

  • Sequences of instructions can be stored in source text files with the extension and executed with the program.