First Python Program in VSC

Published by CWS Admin on

In Enabling Python in VS Code, we customized Visual Studio to include support for Python programming.  In this post, we complete the installation of Python-supporting features and use our newly-customized, Python-enabled Visual Studio Code to write our very first Python program.

The easiest way to complete the installation of all Python-supporting features in VSC is to write a simple Python program. Visual Studio Code is smart enough to realize that there are still some Python-related features missing and will help us to install them. Python interpreter

If you’ve used Visual Studio before, you’ll quickly realize that VSC is different. Unlike older versions of Visual Studio, VS Code does not support solutions and projects. Instead, files are organized in directories and VS Code will create workspaces on the fly, when a directory is opened.

Let us now write our first Python program in VSC!

  1. Go to File > Open Folder …
    1. In the Open Folder dialog, navigate to your Documents folder
    2. Create a new folder called  “CodeWithSara”
    3. Open the “CodeWithSara” folder and inside it create another folder called “VSCPython”.
    4. Open the “VSCPython” folder and inside it create another folder called “Hello”. This will be the folder for your workspace.
    5. Select “Hello” folder; i.e. click on it but do not open it.
    6. Click the Select Folder button.
  2. In the File Explorer toolbar, you now have an empty Hello folder/workspace. When you hover over it, you should see small icons – New File, New Folder, Refresh, and Collapse All.
    New Source File
  3. Click on the New File icon and create a file named “hello.py”.
    1. Up until this moment, VS Code had no idea what type of workspace it should create. So it created a generic workspace expecting more info later. As soon as you add the hello.py file to the Hello folder/workspace, VSC tries to change the generic workspace into a Python workspace with all the bells and whistles that Python needs. If, during that process, VS Code finds anything missing (or ambiguous), it will prompt you to fix the issue.
    2. For example, when VSC finds more than one Python interpreter installed on your computer, it will prompt you to choose which one you want to use with VS Code.
      [Image]
      If that’s the case, just click “Select Python Interpreter” and pick any interpreter you like. The newer the version, the better.
      [Image]
    3. You may also be asked to install a linter.
      [Image]
      If so, click “Install”. We will discuss linting later; it is a visual tool that highlights any style or syntax errors. If you did not get the pop-up, you most likely have a linter already installed.
      Install PyLint
    4. The installation of the required packages for PyLint opened a Terminal window named “Python”. You can ignore the warning (yellow) messages from the Terminal for now.
  4. The file “hello.py” should already be opened in the main window. If not, double-click it. Type in the text below:
    print('Hello, Sara!')
    You could also just copy-and-paste it. That said, we do not recommend copy-pasting. We believe that there is a value in typing the text yourself and you will learn things better this way.
    1. Notice how nicely colorful the text is.

      This feature is called syntax highlighting and it makes reading code so much easier.
  5. We are finally ready to run the “hello.py” program! Right-click anywhere in the main window and select  “Run Python File in Terminal”. Your Python Terminal window should look similar to this:

CONGRATULATIONS! Your Visual Studio Code is now fully set up to support coding in Python language. And you have already written your very first Python program in VSC!