Visual Studio Code

You can run Python programs several ways, but the easiest method when getting started is to use an integrated development environment (IDE). IDEs help you organize files, write code, and run code. Visual Studio Code (VS Code, or just Code) is a free, cross-platform IDE made by Microsoft. Visual Studio Code can be used to edit hundreds of different file types and supports dozens of programming languages. It has a plugin architecture to expand its base feature set. (This website was built entirely in Visual Studio Code.) In this exercise, you will install Visual Studio Code in Windows and ensure that you can run Python programs.

Warning

The Python programming exercises will assume that you are familiar with creating and editing files in Visual Studio Code. When the exercises ask you to create a file, you should do so in Visual Studio Code.

Learning Objectives

In this exercise, you will learn to:

  • install the Python runtime,
  • install Visual Studio Code,
  • install Python extensions,
  • work with files and folders,
  • run Python interactively, and
  • run a Python file.

Video Walkthrough

Use this video to follow along with the steps in this lab.

Install Python

You must install the Python runtime to run Python code on your computer.

  • Go to python.org.
  • Click the Downloads link.
  • The website should detect that you are using Windows, and offer you the Windows download.
  • Download the latest version.
  • The file downloaded will be named something like python-3.12.4-amd64.exe. Do not worry if the numbers do not match exactly because new Python versions are constantly being released.
  • Run the downloaded executable.
  • IMPORTANT: On the first part of the installation, check the box to Add python.exe to PATH. This will allow you to run Python programs from the command line interface.

Add Python to PATH

  • After checking the box to add python.exe to your PATH, click Install Now.
  • After a brief installation, you should see a message that Setup was successful. Close the installer.
  • Start PowerShell and run python.
PS C:\Users\Administrator> python
  • You should see the Python interactive interpreter. This means that Python was installed correctly.
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
  • Run the following lines of Python code and evaluate the results.
print("hi")
1 + 1
1+1
2**3
"a" * 5
  • Run the exit() function to leave the interpreter. The quit() function also works.
exit()
  • You should be back in the normal PowerShell prompt.
  • Close PowerShell.

At this point, Python should be installed and working on your system. If Windows could not find Python when you ran the python command in PowerShell, re-run the Python installer and ensure that you checked the box to install python.exe to your PATH. Close any open PowerShell windows, start a new PowerShell window and try running python again. By restarting PowerShell, you load any updates to the PATH.

It is helpful to run snippets of Python code in the interactive interpreter. However, most people create Python programs by creating standalone files. Visual Studio Code is a great editor for working with Python source code files.

Where to install Visual Studio Code?

You can run Visual Studio Code on any operating system. These instructions are written using the Windows Server EC2 instance on AWS. If you want to run Visual Studio Code on your laptop--great. But if you find that you have software problems or difficulty running your code, you should install Visual Studio Code in your Windows EC2 instance.

Download the Visual Studio Code Installer

  • Open your web browser and go to the Visual Studio Code download page.
  • Download the version for your operating system.
  • Use the link the the System Installer to make it available to all users on your computer.
  • Windows Server may not be listed. Just download the version for the Windows Desktop.

Visual Studio Code Installation

The installation is straightforward. There is only one page of the installation wizard that I recommend that you modify, as described below.

  • Accept the license terms.
  • Accept the default installation folder.
  • Let it create a start menu folder.
  • In Select Additional Tasks, check the options to Add Open with Code action to Windows Explorer file context menu and Add Open with Code action to Windows Explorer directory context menu. Those features are nice to have, though Visual Studio Code will work fine without them.
  • Continue accepting the default to finalize the installation.
  • Launch Visual Studio Code when the installation completes.

First Launch

  • Select your theme. If you enjoy eye strain, choose a light theme. If you have good taste, choose one of the dark themes.
  • Close the Welcome tab.

alt text

  • Note the icons at the top left of Visual Studio Code.
    • Explorer lets you view files and folders that you have opened. Typically, you will open one folder that contains your code.
    • Search lets you search within the files and folders opened in the Explorer.
    • Source Control tracks changes to individual files.
    • Run and Debug lets you run your code and optionally pause your code to inspect what the code is doing.
    • Extensions let you customize Visual Studio Code.

Python Extensions

Visual Studio Code is a generic editor. Extensions give Visual Studio Code the ability to edit specific file types--such as Python source code files. Extensions are published in the Visual Studio Marketplace. Independent developers can publish plugins. Organizations can also publish plugins. It is important to make sure that you trust the source of the plugins because the plugins will have access to read and modify your data in Visual Studio Code.

  • Open the Extensions tab.
  • Search for Python. Install the Python extension published by Microsoft. The Python extension will automatically install two other extensions--Python Debugger and Pylance.

Visual Studio Code Python Extensions

First Python File

  • Create a folder for your Python code. Name it whatever you want. Put it wherever you want. For simplicity, I will create a folder named python on the desktop.
  • Start Visual Studio Code if it is not already running.
  • In the File menu, choose Open Folder.
  • Open the folder you just created. Visual Studio Code may ask you if you "trust" the folder. Click Yes.
  • In the Explorer tab, click the new file icon.

New File

  • Name the file first.py.
  • Add the following Python code.
print("It is working.")
  • In the Run menu, choose Start Debugging.

Start Debugging

  • Select the Python Debugger.
  • For the debug configuration, choose Python File.
  • The program will run, and the output will appear in the terminal tab at the bottom of Visual Studio Code.

Ran from VS Code

At this point, you should be able to create Python files and run them in Visual Studio code. Future exercises will dig much deeper into Python syntax. You can delete first.py if you want. You do not need it in any future lessons.

Practice

You should feel comfortable doing the following tasks.

Working in the Interpreter

  • Open a PowerShell prompt.
  • Launch Python.
  • Run the code print('hello world').
  • Exit the interpreter.
  • Close PowerShell.

Working in Visual Studio Code

  • Launch Visual Studio Code.
  • Open your Python source code folder if it is not already open.
  • Create a new file called second.py.
  • Add the code print('hello again') to the file.
  • Run the file and evaluate the output.

Challenge

  • Search the extensions for other types of files that you typically edit. Install them and test them out.
  • It is possible to synchronize VS Code settings across every installation. Log in and turn on settings synchronization.
  • If you do not like Visual Studio Code, you can try PyCharm from JetBrains. JetBrains gives students free licenses for its professional Python PyCharm IDE--you just have to verify your university email address.

Reflection

  • Why would you want to use an IDE instead of Notepad for editing source code?

Key Terms

  • IDE (Integrated Development Environment): A software application that provides comprehensive facilities to computer programmers for software development. An IDE typically includes a code editor, compiler or interpreter, debugger, and other tools that facilitate the development process. Examples of IDEs include Visual Studio Code, IntelliJ IDEA, and Eclipse.
  • Visual Studio Code: A free, open-source code editor developed by Microsoft. It is lightweight yet powerful, supporting a wide range of programming languages and development tasks. Visual Studio Code offers features such as syntax highlighting, code completion, debugging, and integrated Git support. It is highly extensible through a vast library of extensions available in the Visual Studio Code Marketplace.
  • IDE Extensions: Add-ons or plugins that enhance the functionality of an Integrated Development Environment (IDE). Extensions can provide additional features such as language support, code snippets, linters, debuggers, and tools for version control. In Visual Studio Code, extensions can be easily installed and managed through the built-in Extensions Marketplace.