Musings from the Scribes

Data Science via VS Code. Part 1: install, extensions, virtual env.

Written by Samuel Parsons | Jul 22, 2024 1:00:00 AM

Welcome to the mini-blog series on data science in Visual Studio (VS) Code!

In this first article, we will complete the following steps:

  1. Install VS Code

  2. Explore and install VS Code Extensions

  3. Install a Supported Version of Python on your system

  4. Create a Virtual Environment to begin exploring

To complete these steps - you will need access to a device with local administrator (you will need the permissions to install applications to your computer). Please note this series is written foremost for Microsoft PC users, although I will provide some Mac guidance here or there from lessons learned. Like most Microsoft products, there are many ways to complete the same activity - I’ll just show you one. Let’s get to it!

Step 1: Install VSCode

Head over to Download Visual Studio Code - Mac, Linux, Windows and pick the version that is right for your system:

While this may differ for you, I have selected the Windows 11 x64 system installer version (second from top on the left). Download the installer and complete the installation process.

Once installed, you will be met by the Welcome Screen on start up.

Don’t worry if yours looks a little different - I had to blur some of the extensions installed on the left pane, recent files and suggested next steps in extensions - didn’t want to spoil the surprise.

Step 2: Explore and install VS Code Extensions

Part 1. Install Python Extension

Where to from here? Well, lets see if we can find anything about Python.

  1. Click on 1. Extensions icon (left hand menu)

  2. Search for ‘python’ in the Extensions Marketplace

  3. Select Python ‘Python language support…’ 129.5M extension by Microsoft

  4. Read about the Extension in the main window, before Installing it by pressing 4. Install

A few things to note here - the publisher is listed below the extension name in the Marketplace. The Python extension we are searching for an installing is created by Microsoft. As you can see down the list, there are a number of Python-related extensions that are available from third party providers.

Extensions have ratings and counts of downloads - this extension has 129,519,307 downloads (at time of screenshot) with 595 ratings of 4 stars.

Part 2. Install Data Wrangler Extension

Another useful extension for working with data in VS Code is the Data Wrangler extension by Microsoft. Search for ‘Data Wrangler’ in the Marketplace extensions, and install the Microsoft release.

I don’t want to get too ahead here - but scroll down on the Data Wrangler description page and check out the features and functionality. The Editing mode interface alone is amazing.., data previews, real-time data editing including generating the python code? Yes please!

Part 3. Install Jupyter

The last extension we will install (for now) is Jupyter.

Search for ‘Jupyter’ in the Marketplace extensions, and install the Microsoft release.

Jupyter notebooks allow for a notebook environment where you can write code, run functions and review the outputs in one pane. You can also add commentary via markdown cells as you go - this is where most of our python coding will take place.

Step 3: Install a Supported Version of Python on your system

Now we have notebooks and the python extension we are good to go, right? Not yet!
We still need a supported python interpreter on your system. You can find your way via Google or Bing Search, otherwise head back to the Python extension and scroll down on the details until you see the 1. Quick start guide section below:

Click on 2. Step 1. Install a supported version of Python on your system. This will take you to the Get Started Tutorial for Python in Visual Studio Code page, which will then instruct you to visit the Download Python | Python.org page. For most users, the latest stable release (1) is appropriate - but check your system requirements:

Install Python by downloading and running the installer. There will be a checkbox to ‘add to Path’ check this.

There is some conjecture about ‘the best place to install python’. Rather than join the masses (or put my neck on the line!) I asked Bing (so I wouldn’t have to):

Personally I went with option 2 - as it’s accessible to all users, and doesn’t have issues with file path length if it’s hidden away in an odd path somewhere. You do you! (or if in doubt, #2).

At this point I must advise that AI-generated content may be incorrect…

Step 4: Create a Virtual Environment to begin exploring

Part 1. Creating the Project Folder

Ready to kick down the door to the dungeon?

We need a project folder to begin work - lets not complicate this with repositories and source control just yet.

In VS Code complete the following steps:

  1. Select the ‘Data Explorer’ tab

  2. Select 'Open Folder'

  3. Navigate to and create a Folder structure to house your projects

    1. In my example I have created C:\Python Coding\Data Science Exploration

  4. Select the base folder ‘Data Science Exploration’

  5. Select ‘Select Folder’

You should be prompted with the following:

Select ‘Yes, I trust the authors’. You can also select the parent folder 'Python Coding' checkbox to avoid this dialogue again for your future projects in this folder.

We now have an open folder in our Explorer pane: ‘DATA SCIENCE EXPLORATION’ (or the name you selected for your folder above):

Part 2. Creating the Virtual Environment

Hold Ctrl+Shift+P to bring up the Command Palette and type 1. ‘Python’ then select 2. Create Environment…

 

Select Venv ‘Creates a '.venv’ virtual environment in the current workspace.

Select the Python interpreter you installed earlier - if you followed Step 3 it should prompt as it is on path. If not, you can enter the interpreter path… and navigate to where you installed python earlier.

When you select the Python interpreter, the system will automatically Create the environment.

Confirm the environment is created by:

  1. Reviewing the pop-up 1. The following environment is selected… and

  2. Sighting and clicking the green .venv folder within the DATA SCIENCE EXPLORATION folder.

To start the environment load the command palette by holding Ctrl-Shift-P and typing ‘Terminal’ - start a Python terminal. Check the file path matches the virtual environment / project path and type: .venv\scripts\activate

 

The green (.venv) return on the following line tells us the virtual environment is now active.

Congratulations! That’s the end of the first activity.

Recap: The steps to create a VS Code Virtual Environment

If this is your first time working with VS Code and you completed the steps fault free - well done!

If you ran into issues, try revisiting the previous steps. You can always uninstall and then reinstall various components until you get it to work.

In this tutorial we have:

  1. Installed VS Code

  2. Installed VS Code extensions: Python, Data Wrangler, Jupyter

  3. Installed Python decoder

  4. Created a Virtual Environment

  5. Loaded the Virtual Environment

Still to come

We will explore why you should use a Virtual Environment rather than the System for installations, explore the Python libraries and packages and take the next steps!

Continue to Part 2: Initial Libraries and Data Import