× Join My Newsletter For Free!      Sure!


Let's Do Tech Together!

Since 2016!


JSM
JSM
Jan. 18, 2020, 6 a.m.


How To Setup Visual Studio Code For Python | A Beginners Guide To Success


Short Link: http://www.jsmtech.org/setup-vs-code-for-beginners
Read Time: 11 min


My brother and I are teaching a programming class at our local homeschool Co-op. My brother wrote a guide for them on how to properly set up VS Code. We thought it would be easier to share on my blog, however, hence this guest post by him — enjoy! :)

 

Now that you have become seasoned programmers, it is time that you learn how to use professional tools for code development. In this post, I want to properly go over how to install, setup, and use VS Code for your everyday programming.

This post is meant to be a guide, so bookmark it, and refer back to it when needed.

What is VS Code?

Description

Visual Studio Code is a source-code editor developed by Microsoft for Windows, Linux and macOS. It includes support for debugging, embedded Git control and GitHub, syntax highlighting, intelligent code completion, snippets, and code refactoring. — Wikipedia

In other words, it is a professional code editor with supercharged features. No more IDLE. Now you are ready to develop code professionally, using an Integrated Development Editor (IDE).

One of the things that make VS Code so powerful it that it is open source and that people have made thousands of extensions for it. We will be installing and using several of these extensions.

Note: no need to worry about installing extensions. The VS Code extension store (called Marketplace) always scans extensions for anything malicious. Also, all installed extensions are run sandboxed, so they cannot interact with your system, they only run inside VS Code.

Installing VS Code

Download and install VS Code from here: https://code.visualstudio.com/

Setup VS Code

After VS Code is installed, open it. This is what you will get when you open it up for the first time:

Color Theme

Yikes… Doesn’t look like IDLE, right? Well, we’re going to make it look and feel more like IDLE, for those of us (like me) who like that color theme. (Note: You can install whatever color theme that you want… This is about making you more productive.)

  1. Click on the “Extensions” icon on the left sidebar.

2. Search for IDLE Theme and click install.

3. You will be prompted to change you color theme. Select IDLE Theme

 

Ah.. That’s better. But in this installed theme, the comments aren’t red, they are gray! If you (like me) cannot tolerate gray comments, this is how you make comments red again:

  1. Open View > Command Palette. A dialog will open. This is a quick way to get to or do anything in VS Code. The keyboard shortcut for this is: Ctrl+Shift+P.

  2. Click on Open Settings (JSON).

  3. A window will now open called settings.json.

  4. Now, copy and paste the following. Save the file. It should look like this in the end:

{

    "workbench.colorTheme": "IDLE",

    "editor.tokenColorCustomizations": {

        "comments": "#FF0000",

        "textMateRules": [

            {

                "scope": "string.quoted.docstring.multi",

                "settings": {

                    "foreground": "#FF0000"

                }

            },

            {

                "scope": "string.quoted.double.block.python",

                "settings": {

                    "foreground": "#FF0000"

                }

            },

        ]

    },

}

 

Great, now comments are red.

Extensions

Now, let’s set VS Code up for Python development & collaboration by installing the following extensions:

  1. Install the Python Extension Pack:

  2.  Install the Live Share Extension Pack.  

This extension is used when one or more people want to collaborate and create code together. A Microsoft or Github account is required to use it.  

Using VS Code

VS Code should now be installed and fully setup. Now let’s see how you can use VS Code supercharge your programming.

Choose a Home Folder

Choose a folder in which you will put all your python files. You may have already created this folder. Then add it to VS Code, like this:

  1. On the Explorer tab, click open folder.

  2. Navigate to the folder of your choice, then click select.

Now all your files can be created from inside VS Code.

Enable Auto Save

Sometimes it gets tiring to save your python file after every edit. Why not use VS Code’s Auto Save Feature? To enable it click go to File > Auto Save, and click on it. It will be checked when it’s activated.

Running Your Python Files

Now let’s see how we can create python files from within VS Code and run them.

Using VS Code to create folders and files
  1. On the Explorer tab, click on the Add Folder icon to add a folder. Call it vscode_test. Then use the Add File icon to create a python file called vscode.py. Make sure you have the .py for python files.

Here’s how it looks after I added the folder and created the file.

Select Python Interpreter and enable Intellicode and pylint.
  1. Make sure VS Code knows where your Python is installed. If everything is good, then the bottom left corner of the screen will display your python information and version, like in picture below. If not, that means VS Code cannot find your python, that means you need to add Python to Path.

  2. Enable Intellicode and pylint. These are supercharged tools that will warn you of errors in your program, before you run it.

Using VS Code to code
  1. Now type this into the python file you just created.

# A simple python script to demo vscode's features



# Create variables

a = 1

b = "hello"

c = 10



print ("I am developing code in vscode!")



a = 1 * 2 * 3 * 4 * 5



for i in range(10):

    b = b + i * i



c = input("What will you do with your new programming skills?: ")

print ( "You said:", c )

 

Running a Python file in VS Code: Using Terminal

Now we will run this file by manually invoking python though VS Code’s built-in terminal. This will work for either Windows or Mac.

  1. Open Terminal > New Terminal

  2. A new terminal window will appear at the bottom. Type in the command: python vscode_test/vscode.py This will run the program. Here’s my output:

Running a Python file in VS Code: Using Right Click Context Menu

But we can also run our python file by right clicking on our code and selecting the Run Python File in Terminal option.

  1. Right click anywhere in the active code window and click Run Python File in Terminal.

  2. VS Code will now automatically open a new terminal window for you. Here is what I got:

This option should work if the first didn’t.

Running a Python file in VS Code: Using F5 Keyboard shortcut.

Now you know how to run a python file from the terminal, and by right clicking and running. But I miss IDLE’s F5 run command, do you? Let’s setup VS Code to run our python file on F5, or any other keyboard combination.

  1. Click on the settings icon on the bottom of the sidebar, in the lower left corner. Click and select: Keyboard Shortcuts

  2. A new window will open with a list of keyboard shortcuts for commands. Type and search for run python file in terminal. Double click on the first one.

  3. A new pop out will appear. Press the F5 key on your computer now, or whatever combination of keys you want. Then press Enter.

Now try running your program using F5!

Using Python Debugging Tools

Okay, here’s the last thing I want to show you. Sometimes you need powerful tools when your debugging code. It can save you hours…

Executing a Python program in debugging mode

To execute a python program in debugging mode:

  1. Click on the Debug tab, then click Debug with Python.

  2. A popout will appear. Select Python File.

  3. The program will now be run in debugging mode. You will see this on the top while it’s running:

This is the debugging toolbar. The buttons in order mean:

  • Pause execution

  • Step Over: This will execute the next line when it encounters a breakpoint.

  • Step Into: This will step into the function or definition, and continue execution inside it.

  • Step Out: This will step out of a function of definition.

Playing with breakpoints

What are breakpoints? Breakpoints are lines in the program you want to pause execution. One reason you would do this is for finding or solving bugs.

To set a breakpoint in VS Code, click just before the line number. Like this:

My mouse was hovering over line 6 when this screenshot was taken. Because I didn’t click on it, it is not a breakpoint.

Now run the program in debugging mode like we did earlier, with the debugging toolbar showing.

You know VS Code is in Debugging mode because the bottom has turned orange. Look, now there’s an red arrow where the red dot was on line 10. What does that mean? It means that the debugger has paused right before that line. It has not yet executed that line. On the debugging toolbar above, press continue. This will continue the debugger until it hits another breakpoint.

Now, the arrow is on line 12. Notice the panel on the left with all our variables. Think of how useful this is! Suppose I needed to know the value of the variable in line 10. All I need to do is hover over it (like in the picture above) or view it in the variables panel.

Now, I want you to try clicking the Step Over button a few times, watching where the red arrow goes. Notice it goes from line 12, to line 13, and back to line 12, in a loop. It is following our program. You can also keep track of the value of the variable i.

Final Thoughts

VS Code is a powerful debugger, editor, and IDE. Please make sure that you know how to do everything discussed in this tutorial. As we get to harder programs and projects throughout the rest of the year, these tools will let you excel in your programming knowledge, and teach you how to write code fast, efficiently, and effectively.

If you have any questions about anything, please don’t hesitate to leave a comment. Sometimes it is better to ask for help than struggle for hours. Also, if you liked this post, feel free to subscribe to Justin’s newsletter to receive updates for when posts like this come out.

Happy Coding,

Martin

 


JSM

About Me — JSM!

I am a programmer who blogs, an entrepreneur who writes.

Read My About Page!

Like this post? Don't miss any more! Sign-up for my newsletter!


Thank you for letting me visit your inbox weekly!

Post Your Comment Now!



Comments Are Closed — Please Try Again Later