Setup VSCode for Python and GitHub

How to Setup VSCode for Python and GitHub Integration

For those DBAs using SQL for data discovery, the move to data science can involve a brand-new set of varied tools and technologies. This article is a walk-through of setting up the tooling to do some data discovery using Python. By setting up your workflow using GitHub, VSCode and Python, you will have the basic architecture set up to start your journey down the path to kicking the tires on data science on the Microsoft stack.

This demo uses GitHub and Windows; however other platforms can be used. I have just selected these as more common for the teams I work with.

This walk-through will show you how to set up;

  • Visual Studio Code
  • Git
  • Python
  • pip
  • Integrate GitHub with VSCode
  • Integrate Python with VSCode

Warning: This is try a your own risk walk-through!! Every machine is different and should not be used in a production environment. This is provided as an illustration and covers a base machine install. No guarantee or warrantee is provided or implied.

Visual Studio Code (VSCode)

Main Install

Using VSCode as your source code editor allows you to use the same editor across numerous platforms, Windows, Linux and macOS, while directly integrating with GIT (GitHub in our examples). For Python, this brings the ability to have a full code editor, which brings in functionality such as syntax highlighting, code completion, code refactoring, and snippets. Features such as changing the themes, keyboard shortcuts, and many configuration preferences allow you to create your own editing experience.

Note: This article assumes that this is not the first time installing an application. Some screens shots will be displayed, but any steps with simple choices are provided as a detail.

  1. Installing VSCode is as simple as going to the VSCode portal and clicking download on the main portal page.
A screenshot of a cell phone Description automatically generated
  1. This option will take you to the Getting Started page. If you get the following popup, which could be different depending on which browser you use, you can save a copy or run the installer directly after download.
VSCode install page
  1. Once the installation begins, the walk-through wizard, detailed steps below, will guide through the process.
  1. Welcome – Select Next to begin the installation.
  2. License AgreementAccept the License Agreement.
  3. Select Destination Location – Either Accept the suggested location for the install or browse to another location.
  4. Select Start Menu FolderAccept by hitting Next or Check the “Don’t Create” box if you do not want a startup folder.
    A screenshot of a cell phone
                Description automatically generated
  5. Select Additional Tasks – I normally select all these options as I use VSCode for most of my coding tasks. One item you may want to decide is the “Register Code as an editor for supported File types.” This makes VSCode the default editor for specific file types and will open when double-clicking on a supported file. There is a discussion on VSCode’s GitHub site that reviews this option in more detail. Select Next to continue.
    A screenshot of a cell phone
                Description automatically generated
  6. The final screen is then displayed. Select Install to begin the installation.
  7. The Completed notification will allow VSCode to launch and the setup to continue.
    A screenshot of a cell phone
                Description automatically generated

Configuration of VSCode

Rather than a full how-to on using VSCode, see the Getting Started page for more. This section will provide a brief interface walk-through in addition to reviewing the configuration of VSCode for our Python tutorial.

C:\Users\styoung\AppData\Local\Temp\SNAGHTML3817533e.PNG

Walkthrough of the Interface

#ItemDescription
1Menu BarMenu bar that also includes the Command Pallet which is activated by hitting CTRL-SHIFT-P.
2Code Edit PaneMulti tab edit window. Tag 11 shows the Split Pane which allows you to open multiple code windows.
3File ExplorerThis option will expand the left pane to display the files and directories in the current working folder.
4SearchIncludes Search and Replace.
5Source ControlShows the changes that have yet to be checked-in to Source Control.
6Debug WindowBring up the debug screen.
7Extension windowThis shows the Extension windows. Extensions are add-ins that provide additional functionality. Python is such an extension.
8VSCode SettingsVarious editor defaults and settings.
9Terminal and Output message toggleThis brings up the Terminal split screen. Problems, Output and debug console are in this section.
10Toast Style messagesVarious messages will be displayed depending on the context you are in.
11Open Tab WindowThis allows you to open up multiple tabs / code files in the same display.
12Ellipse menu (Callout)This brings up a callout menu that allows you to close all the currently open code windows or only those that are saved. (As an aside if the 3 dots were vertical they are called a Kabob, Who knew!!)

Linking to GitHub

Having the ability to place your Python files in source control is a major advantage to using VSCode. There are a couple of helper applications that we need to install, GIT services and I normally also install GitHub Desktop.

  1. Let’s install GIT Services first. In a browser, go to the GIT project site. If you visit the site using Windows, the Latest Source Link, highlighted below, should be visible.
    A screenshot of a cell phone
        Description automatically generated
  2. The download should start automatically, and your browser should ask for a next step, select RUN.
    A screenshot of a cell phone
        Description automatically generated
  3. The installation wizard should now start. The following tabs are listed below with options to select.
    1. User Account Control – Select YES to authorize the install.
    2. Information – Select Next to accept the License and continue.
    3. Select Destination Location – Select Next to install in the default location.
    4. Select Components – Review the selected options, pictured below, and select Next. These could change depending on what development you are doing; however, I normally select the defaults as this is really the service and drivers that other tools use. One option which is on by default is the Large File Support. I keep this on just in case. More information in the Git Documentation is available.
      A screenshot of a cell phone
                Description automatically generated
    5. Select Start Menu Folder – This creates a folder, select Next for the default and continue.
    6. Choosing the default editor used by Git – Choose Visual Studio Code as the default editor. Then select Next.
      A screenshot of a cell phone
                Description automatically generated
    7. Adjusting your PATH environment – Choose the GIT from the command line and also from 3rd-party software as the option.
      A screenshot of a cell phone
                Description automatically generated
    8. Choosing the HTTPS transport back end – Choose the Use the OpenSSL library option. Select Next to continue.
      A screenshot of a cell phone
                Description automatically generated
    9. Configuring the line-ending conversions – Tells GIT how to treat line endings. Since this install is on Windows, I choose the Checkout Windows-style; commit Unix Style line endings. Select Next to continue.
      A screenshot of a cell phone
                Description automatically generated
    10. Configuring the terminal emulator to use with Git Bash – I change this to Use Windows default console window. This is a Windows install, after all.
      A screenshot of a cell phone
                Description automatically generated
    11. Configuring extra options – I keep the defaults and click Install.
      A screenshot of a cell phone
                Description automatically generated

Several steps, but this completes the Git installation. There are many options and uses for GIT and we will only scratch enough of the surface to get our demo running.

Installing GitHub Desktop

Not that GitHub Desktop is required, but I use GitHub for my repository for many projects. Using the Desktop client for cloning and setting up projects can give you a view and control of your GitHub repository that a code editor cannot. Not required, but I do recommend if you use GitHub Desktop. We will walk through the install below.

  1. From the GitHub Desktop site, select the Download for Windows button.
    A screenshot of a computer
        Description automatically generated
    And then Run from your browser popup
    A screenshot of a cell phone
        Description automatically generated
  2. The installation wizard should start.
    1. The first step is to sign into your GitHub account. This screen also allows you to create a new free account if you do not have one.
    2. Sign into your account using your email and password. Select Sign in when ready.
      A screenshot of a cell phone
                Description automatically generated
    3. Two Factor – I use and recommend setting up two factor authentications with your account. See the documentation on GitHub for more information.
      A screenshot of a cell phone
                Description automatically generated
    4. Once you are signed in, Select Continue to move on.
    5. The final screen asks to give feedback while using the application back to GitHub, make your selection and select Finish to complete the installation.
    6. You should then see GitHub desktop when everything is competed. If you have repositories, they will be displayed on the left.
      A screenshot of a cell phone
                Description automatically generated

Setup and Connect VSCode to use GitHub

VSCode has numerous extensions, of which, integration with GitHub is one of them. By selecting the Extensions icon, you can search and browse various extensions. We will step through the GitHub installation.

A screenshot of a cell phone Description automatically generated

As shown above,

  1. Select the Extensions icon
  2. Enter “GitHub” in the search box
  3. And select the GitHub extension.
  4. Selecting Install will install the extension
  5. Clicking Repository will take you to the GitHub Extension repository website where more documentation can be found. Normally the documentation appears in the detail window below in Details.
  6. After the extension installs you will see the following message,
    A picture containing person
        Description automatically generated
    which informs you that you need a Personal Access Token. We will do this in a step below.

This extension will allow us to setup VSCode to have authentication and permissions to write back and forth to your GitHub account. The most important step is getting the Personal Access Token setup. The ‘GitHub: Set Personal Access Token’ is available from your GitHub settings.

  1. Log into Github.com, under your account, select Settings.
    A screenshot of a cell phone
        Description automatically generated
  2. After Selecting Settings, at the bottom of the next page is a tab for Developer Settings, select this.
    A screenshot of a cell phone
        Description automatically generated
  3. On the Developer Settings menu, select Generate New Token.
    A screenshot of a cell phone
        Description automatically generated
  4. The screen now displays and allows us to provide the access VSCode needs by using this token. Select the repo check box. Also put in a name for this token as you can edit or delete this token later in addition to having multiple tokens.
    A screenshot of a social media post
        Description automatically generated
  5. At the bottom of the list, you can then select Generate Token.
    A screenshot of a cell phone
        Description automatically generated
  6. You will now see the token. Copy this token as you cannot view it once you leave the page.
    A screenshot of a social media post
        Description automatically generated

Now that we have a token, lets finish setting up our VSCode extension

  1. To use this token, we have to execute the ‘GitHub: Set Personal Access Token’ command in the VSCode Command Prompt. Type Ctrl+Shift+P in VSCode to open the command prompt window and type ‘GitHub: Set Personal Access Token’. You will then be prompted to enter the token generated from GitHub.
    A screenshot of a flat screen television
        Description automatically generated
  2. Enter the token in the prompt, Hit Enter.
A screenshot of a cell phone Description automatically generated

Open a Project

Now that we have VSCode connected, we can connect to a repository and begin. We will go to your GitHub account and create a repository.

  1. Go to your GitHub repository listing.
  2. Create a new repository using the New button.
    A screenshot of a computer
        Description automatically generated
  3. Set up your details for the repository by filling in the screen as shown in the below example. Select the Create Repository button to continue.
    A screenshot of a cell phone
        Description automatically generated
  4. Once you have the repository, get the clone link, by selecting the drop down shown below.
    A screenshot of a cell phone
        Description automatically generated
  5. This is the code that was supplied for my example.
    https://github.com/steveyoungca/PythonTutorial.git
  6. Open up GitHub Desktop which will bring up the following screen. Select the Clone a repository from the Internet option.
    A screenshot of a cell phone
        Description automatically generated
  7. This will bring up the following Clone a Repository screen. Select the URL tab and paste in the GIT Repository URL from step 4 . Notice the default directory created in the Local Path text box. Select the Clone button to begin.
    A screenshot of a cell phone
        Description automatically generated
  8. This brings up the GitHub Desktop menu which will show the repository. You also see the Open in Visual Studio Code option, as we set that as default in an earlier step. Select this option.
    A screenshot of a cell phone
        Description automatically generated
  9. VSCode opens the project in the local directory. The files are displayed in the Explorer and the GitHub status is highlight on the bottom.
    C:\Users\styoung\AppData\Local\Temp\SNAGHTML3e7e9e7c.PNG

NOTE: It is important to note that the example we are running through is very simple. I use this in a single developer workflow and use this process to keep code versioning and the ability to save the project off the computer in GitHub. If you work with a team of developers, you may check out and create your own branch. When you are finished your changes, you would merge your changes back into the Master branch. Proper code hygiene and working with Git is beyond the scope of this article, however using this process you can certainly keep your own code in check, if you will. A more detailed set of documentation is available on GIT in the article Distributed Git – Distributed workflows. This link is part of a full set of documentation on GIT.

Installing Python and pylint on Your Machine

Before we install the Python extension for VSCode, we need to install Python on your local machine. This will also install an application called PIP which is a Python package manager that we will use to install various packages on your machine. These packages provide functionality to Python though modules, such as NumPy which provides scientific computing functions.

  1. Download Python – the download link takes us to the downloadable version of Python from the Python Software Foundation. Select the Download Python link highlighted below.
    A screenshot of a cell phone
        Description automatically generated

     

    Select Run from the browser popup if displayed.

    A screenshot of a cell phone
            Description automatically generated

  2. A popup will display once the installation begins. Make sure the Add Python to PATH is selected. Click on the Install Now to continue. The install will ask permission, say OK.
    A screenshot of a cell phone
        Description automatically generated
  3. Once Complete, you will see links to documentation for that specific version and an online tutorial. Select Close when ready.
  4. Installing pylint. If you’re using a global environment, you need to run the commands at an elevated command prompt.
    A screenshot of a cell phone
        Description automatically generated

Type in pip install pylint in the prompt and hit Enter.
A screenshot of a cell phone Description automatically generated
When complete you should see the following details.

A screen shot of a computer Description automatically generated

Setting up Python for VSCode

Now that we have integrated VSCode with GitHub, a repository with a project and installed Python, lets continue to set up the Python extensions.

  1. Now, let’s add a document and make some changes. Simply Right click on the Explorer window and select New File. Name this file Apple.py.
    A screenshot of a cell phone
        Description automatically generated
  2. We now have the first Python files in the work area(1), the Change file (2) showing the new files that we have not checked in, and a Toast(3) message saying that the Python extension is recommended for this file type. As you add different file types, VSCode will display a message if there is an extension that can be used with that file type. Select the Install button. This will bring up some items we want to review.
    A screenshot of a computer screen
        Description automatically generated
  3. A Reload is required to activate. If you select this, VSCode will reopen with the same project entered.
    A screenshot of a cell phone
        Description automatically generated

     

    After installation, you will see the following detail on the Extension. Selecting Repository, will bring you to the documentation.
    A screenshot of a cell phone
            Description automatically generated

  4. We can to select a Linter which is a tool that will analyze source code. This is handy as it will flag various errors, bugs and other coding issues as you type. While in a document from the explorer, Open the Command Palette (Ctrl+Shift+P) and select the Python: Select Linter command by starting to type this into the Command Palette.
    A screenshot of a computer screen
        Description automatically generated
  5. In the drop-down list, select pylint which we installed in a previous step.
    A screenshot of a cell phone
        Description automatically generated

Running our first script

We should now have everything installed and setup for our first test.

  1. Enter the following code segment into the Apple.py window.
    msg = “Hello World”
    print(msg)

Right click on the code window and select Run Python File in Terminal.
A screenshot of a cell phone Description automatically generated

If all goes well, you should see the following results in the terminal window.

A screenshot of a computer screen Description automatically generated

You may see at the bottom of the screen, depending on your setup, a message to select a Python Interpreter
A screenshot of a video game Description automatically generated

To select an Interpreter, which may happen if you have multiple Python applications installed, while in a document in the explorer, Open the Command Palette (Ctrl+Shift+P) and select the Python: Select Interpreter command palette by starting to type this into the Command Palette.
A screenshot of a cell phone Description automatically generated

Select the one you wish to run,
A screenshot of a computer screen Description automatically generated

Writing your files to GitHub

Now that we have some changes, lets commit and write the changes back to GitHub.

  1. Make sure all your files are saved. File / Save All from the tool bar.
  2. The Source Control (1) icon shows 3 files changed and ready to commit. Enter a note to be saved about the Commit (2) . Once ready, hit CTRL – Enter to commit.
    A screenshot of a cell phone
        Description automatically generated

     

    You will get a message that there are no staged changes on your first try, I choose Yes.

A screenshot of a cell phone Description automatically generated
  1. The changes will commit, and the status bar will show that there is a commit that has not been synced. Select the highlighted SYNC button to synchronize your changes to your GitHub repository.
    A close up of a sign
        Description automatically generated
    This will bring up a message that you are doing a synchronization from a specific branch, select OK.
    A screenshot of a social media post
        Description automatically generated
  2. On your first time, it will ask you to log into your GitHub Repository. You will also be asked for your 2 factor authentication if you set that up.
  3. We should now see our files with the commit message.
    A screenshot of a social media post
        Description automatically generated

Finally

Now that we have all the parts installed, we can use Python in VSCode. More tutorials will be added as time goes on. The next tutorial will show how to use Python in data discovery and initial profiling.

References

Python in Visual Studio Code – https://code.visualstudio.com/docs/languages/python

Python – Visual Studio Marketplace – https://marketplace.visualstudio.com/items?itemName=ms-python.python

Linting Python in Visual Studio Code – https://code.visualstudio.com/docs/python/linting

Get Started Tutorial for Python in Visual Studio Code – https://code.visualstudio.com/docs/python/python-tutorial

SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. – https://www.scipy.org/install.html

Similar Posts