Python Friday #23: Visual Studio Code for Python

As I try to do more useful things with Python, I need a more supportive environment for writing code than the REPL. Visual Studio Code offers a lot of nice features without the slowness one expects of a powerful IDE.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

Download and installation

Visual Studio Code (VS Code) works on Windows, Linux and Mac. You find the installers on code.visualstudio.com:

There are installers for all plattforms

Download the installer for your platform, run it and answer the questions in the wizard. When everything is done, VS Code starts.

 

Add the Python extension

Microsoft created an official Python extension for VS Code. You find a detailed guide to setup your computer and VS Code for Python here. I already had a working Python installation and could skip many steps and directly install the Python extension from the Extension Marketplace:

Install the official Python Plugin from Microsoft

 

Write your code

You can add a new file (with the *.py extension) and VS Code will give you syntax highlighting for Python. As soon as you type, it will suggest you install the Python linter, that will help you to check your code for valid Python syntax and shows a warning if you missed something.

Write your code with syntax highlighting

 

Run your code

You can run your code by clicking the green play icon on the top right corner:

Run your code with the play icon in the right corner

However, I find it much simpler to hit CTRL+F5. You can see the output of your code in the terminal window at the bottom of VS Code:

The output is in the terminal at the bottom

 

Debug your code

VS Code comes with a built-in debugger. You can debug your code by hitting F5 (as you do it in Visual Studio). The debugger controls show up in the middle of the top bar of VS Code and allows you to enter methods or skip over them:

The debugger controls look like the ones in Visual Studio

To stop the debugger at the right place you need to add a break point. You can do this by clicking on the space between the left border and the line numbers:

Set breakpoints left of the line numbers

The debugger now runs to that break point and lets you inspect the state of your code:

Inspect your code in the debugger

 

More things to explore

VS Code offers not only a nice integration for Python, but many other things you often use are easy to reach. Git is integrated as a first-class citizen and you can see what files are changed and on which branch you are.

If something is too tiny or is not easy to see, you can zoom the whole VS Code up to a size which works for you. The same is true for the colours. If you like another one, you can choose from various built-in templates or add more using the extension.

 

Conclusion

I think VS Code has a lot of potential and offers a nice integration of all the tools you need. So far, I do not have much experience, but I like what I see and I am eager to learn more.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.