,

Get Started with Python using VS Code

Posted by

It’s important to know how to get started writing Python within VS Code.  This blog will help anyone get started writing Python within VS Code.  This blog is targeted to those developers that want to use a Windows based OS for development.   To get started with Python and VS Code, you’ll need to do the following on a local development environment:

  1. Download and install VS Code:   Download Visual Studio Code – Mac, Linux, Windows
  2. Download and Install Python:  Download Python | Python.org
  3. Install the Python Extension for VS Code
    Note: Two options to install the Python Extension within VS Code
  • Option 1: Click extensions icon and type Python, select it and install
  • Option 2: Ctrl+Shift+X, type Python, select it and install

Open a New Project

Now that the main components are installed you’re ready to open a new project. To do this, create an empty folder on your local file system that will store your project files. Next, Launch VS Code and select, File, Open Folder, and select the empty folder.

Setup Virtual Environment

Python Virtual environments are important to use as the dependencies required for the project/python code is isolated.   Another way of writing this is that without Python Virtual Environments, dependencies downloaded and shared are available to all Python projects on your local system.  You don’t really want libraries hanging around that you won’t be using so virtual environments is the clean recommended approach for each python project you setup. The steps are below:

  1. Within VS Code, select Terminal -> New Terminal
  2. Run: pip install virtualenv
  3. Run: python -m venv test1 (Note: test1 is the name of my virtual environment, you can name it whatever you want)
  4. Activate the virtual environment
    • Using Bash Shell: source test1/scripts/activate
    • Using PowerShell: .\test2\scripts\activate

Note: using PowerShell, you know it’s working when you see the virtual environment name in parentheses at the start of the line. Also, in explorer menu of VS Code, the folder appears that represents the virtual environment.

Select an Interpreter

Instruct VS Code which interpreter you want to use.  The interpreter is what allows VS Code to provide cool functionality like IntelliSense.  Two ways to select an interpreter within VS Code. Two ways to do this.

  • Option 1
    • open command palette (Ctrl + Shift + P)
    • type: Python: Select Interpreter (select your interpreter)
  • Option 2
    • From the status bar bottom right, select interpreter and choose your desired version of Python
    • I selected the recommended interpreter
Setup your first Project!

To create a simple hello world app that prints screen to the console, you can create a file when your project file and ensure the extension is .py. I called mine main.py  and filled out a simple hello world statement.

Run Python Code within VS Code

This is super easy.  You can either choose the VS Code menu options of (run with or without debugging) or use F5 key. Pressing F5 will try to attach a debugger to the running process so you’ll get prompted to select a debug configuration.

I selected python file. After execution, I can see the hello world statement within my terminal session.

Thank You and more python blogs coming soon!

Russ Maxwell

Resources
Managing Extensions in Visual Studio Code
Get Started Tutorial for Python in Visual Studio Code