Advanced Visual Studio Code for Python Developers – Real Python
Advanced Visual Studio Code for Python Developers by Anthony Shaw Updated Nov 10, 2021 Reading time estimate 52m advanced editors tools Table of Contents Remove ads Visual Studio Code, or VS Code for short, is a free and open source code editor by Microsoft. You can use VS Code as a lightweight code editor to make quick changes, or you can configure it as an integrated development environment (IDE) through the use of third-party extensions .
In this tutorial, you’re going to look at how to get the most out of VS Code for Python development. During this tutorial, you’ll learn how you can configure , extend , and optimize VS Code for a more effective and productive Python development environment. After finishing this tutorial, you’ll have a variety of tools to help you be more productive using VS Code. It can be a powerful tool for rapid Python development.
In this tutorial, you’ll learn how to: Customize your user interface Run and monitor Python tests Lint and format your code automatically Leverage type annotations and Pylance to write code faster with higher accuracy Configure and utilize both local and remote debugging Set up data science tools As usual, this tutorial is full of links, tips, and tricks to help you on your way. If you haven’t already installed Visual Studio Code or the Python extension , you’ll need both for this tutorial.
If you’re just getting started with VS Code, you might want to check out Jon Fincher’s tutorial on Python development in Visual Studio Code before continuing with this one. Free Bonus: 5 Thoughts On Python Mastery , a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level. Mastering the Visual Studio Code User Interface By default, VS Code’s user interface is designed for everyone—whether you’re writing C++, doing front-end development with JavaScript and CSS, or using data science tools like Jupyter Notebooks.
In this first section, you’ll explore how to tailor VS Code to support how you work. Remove ads Keyboard Shortcuts For almost everything that you do in VS Code, you can do it directly from the keyboard. You can map all activities in VS Code, whether they’re built-in or provided via extensions, to a keyboard shortcut. A small subset of built-in commands is already mapped in Keyboard Shortcuts . If you want to learn these default shortcuts, print out the PDF for Windows , macOS , or Linux and pin it next to your monitor.
As a Python developer, a lot of the commands you’ll use in Visual Studio Code are provided from extensions, similar to the Python extension that you already installed. These don’t come with shortcuts mapped by default, but you can configure them using VS Code’s Keyboard Shortcuts editor . Using the Keyboard Shortcuts Editor Open the Keyboard Shortcuts editor by going to File → Preferences → Keyboard Shortcuts or Code → Preferences → Keyboard Shortcuts on macOS: In this editor, you can see a list of all the existing shortcuts along with their details: Command : The command or action to be run Keybinding : The sequence of keys to press to trigger this command, with a blank indicating that there’s no mapping When : Conditions that need to be met for this keybinding to work, with a minus sign ( - ) indicating that there are no requirements Source : Where this binding was configured, either defined by Default , User , or Extension , the last one indicating that you provided it by installing a keymap extension To override an existing binding, right-click on the existing action and click Change Keybinding .
To assign a keybinding to a command that has no binding, double-click on it. If the key sequence you press is already assigned to something else, VS Code will warn you with a link to see which commands are mapped to this keybinding. As an example, type python create terminal in the search box at the top. If no results come up, make sure you have the Python extension installed. Press Enter to assign a keybinding, like Ctrl + Alt + T , and then hit Enter again.
To assign that shortcut to only work when you’re editing Python code, right-click on the shortcut and select Change When Expression . Enter the expression editorLangId == 'python' : There are more conditional operators to choose from if you need something more specific. Here are some examples to get you started: Use resourceFilename == '__init__.py' when the file you’re editing is __init__.py Use editorLangId == 'python' when working with Python.
If needed, you can also replace 'python' with another language identifier . Use editorHasSelection for when you’re inside the editor. Once you’ve configured this extension, open up a Python file in the editor to try it out by pressing your newly assigned keyboard shortcut Ctrl + Alt + T to open up a Python terminal. Setting Up Keyboard Shortcuts for Commands From the Command Palette You’ll refer to the Command Palette many times in this tutorial.
Most of the functionality in VS Code is accessible through the UI from context menus , but you won’t find everything there. Anything that can be done in the core editor or via an extension is available in the Command Palette. You can get to the Command Palette by pressing one of these keyboard shortcuts: macOS: F1 or Cmd + Shift + P Windows or Linux: F1 or Ctrl + Shift + P To run a command, type its descriptive name, like Python: Run All Tests .
Once the Command Palette is up and the command you want is at the top of the menu, click the cog on the right to assign a keyboard shortcut: Once you’ve started using the Command Palette regularly, you may find that there are five to ten commands you run really often. Assign some shortcuts to those commands to save yourself a few extra keystrokes. Installing Keyboard Mappings From Other Editors If you’ve been working in another editor or IDE for some time, you may have your usual keyboard shortcuts committed to muscle memory.
You’ll find helpful keyboard mappings in extensions for Vim , IntelliJ and PyCharm , Sublime Text , Notepad++ , Emacs , and Atom . If your previous editor isn’t on this list, you may find that someone else has created a mapping extension for you. Keymaps is a helpful category on the VS Code extension marketplace . Once you’ve installed a keymap extension, you may also find additional options to customize it beyond the defaults.
For example, you can use the Vim keymap extension to configure Vim features such as whether to start in insert mode. Remove ads Customizing the User Interface The first things you should do when jumping into the driving seat of a car are to adjust the seat, adjust the rear-view mirror, and set the steering column to the right height. Code editors are no different. They come with a default layout that works okay for everyone but not especially well for anyone.
You don’t want to stretch to reach the pedals. Let’s set up your VS Code environment to suit you and make it look great on the way. First, the Activity Bar on the left-hand side of VS Code is the main navigation tool to switch between the Explorer view used to see your files, the Source Control view, the Search view, the Run and Debug view, and the Extensions view. You’re not limited to these options. Many extensions come with views that are hidden by default.
Right-click on the Activity Bar to control which views you see: You can also use this menu to hide any views that you never use. Keep in mind that you’re not limited to the views on the list. If there are some panels within a main view—for example, you can find some additional panels nested inside the Explorer view—you can drag them to the Activity Bar to create a permanent shortcut: Use this feature for views that you use all the time.
Using Split Screens Python’s style guide, PEP 8 , leans toward short line lengths. For wide screens, this leaves a lot of unused screen space. When you find yourself n
The rest of this page is on the live site.