Advanced Visual Studio Code For Python Developers

Table of Contents

  • Mastering the Visual Studio Code User Interface
    • Keyboard Shortcuts
    • Customizing the User Interface
    • Setting Up Your Terminal
    • Team Settings vs Personal Settings
  • Linting and Formatting
    • Setting Up Pylance
    • Setting Format and Lint on Save
  • Testing Your Python Code in Visual Studio Code
    • Configuring Test Integration
    • Executing Tests
  • Using the Visual Studio Code Tasks System
    • Using Tasks to Compile Wheels
    • Using Tasks for Django
    • Chaining Tasks
    • Using Tasks to Run Tox
    • Using the Task Explorer Extension
  • Debugging Your Python Scripts in Visual Studio Code
    • Setting Watches
    • Configuring a Launch File
  • Mastering Remote Development
    • Remote Development With Containers
    • Remote Development With SSH
    • Remote Development With WSL
  • Working With Data Science Tools
    • Installing the Jupyter Notebook Extension
    • Getting Started With Jupyter Notebooks in VS Code
    • Using the Data Viewer
    • Using the Rainbow CSV Extension
  • Adding Bonus Extensions to Visual Studio Code
    • Code Spell Checker
    • Docker
    • Thunder Client
    • VS Code Pets
  • Conclusion
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:

VS Code Keyboard Shortcut Window

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':

Assigned keyboard shortcut

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:

Assigning a shortcut from the command palette

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:

VS Code activity bar

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:

Dragging an item to the activity bar

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 needing to switch between two or more files, use the split panel functionality to show many editor regions at once. You can access this functionality through the Command Palette with the command View: Split Editor.

Alternatively, you can use the associated keyboard shortcut:

  • macOS: Cmd+\
  • Windows or Linux: Ctrl+\

You can also open the split editor functionality by going to View → Editor Layout or by right-clicking on a file’s tab:

VS Code splitting a panel

This setup is really useful when writing unit tests with your test code on the left and the module you’re testing on the right.

Some file types, like Markdown, have a preview region that you can use instead. Use the Markdown: Open Preview to the Side command to open the preview editor.

Using Zen Mode for Focused Work

If you need your absolute focus on a single file or task in VS Code, then use Zen Mode by going to View → Appearance → Zen Mode to show a full-screen window with just the editor:

VS Code in Zen Mode

This feature is especially useful for blocking out noisy distractions and notifications so that you can just focus on getting your current task done.

Theming

VS Code has a huge library of themes in the VS Code Marketplace. Some of the most popular themes out there are Material Theme, Winter is Coming, Dracula, Noctis, and Monokai Pro.

Let’s focus on the Material Theme as it comes with a broad set of color schemes to suit many tastes. You can install the extension from the Extensions view. Once installed, you can select a color theme by running Preferences: Color Theme in the Command Palette.

The Material Theme looks best with a custom icon theme. Icon themes are separate extensions from the color themes, so you have to install a second extension. Search for pkief.material-icon-theme in the Extensions view to find the most popular Material icon pack for the theme. After installing this, VS Code will prompt you to switch your icon pack.

This is the Material Theme with the Palenight color theme and the Material icon pack:

VS Code with the pale night color theme

You can change the icon theme at any time from the Command Palette by using the command Preferences: File Icon Theme and then selecting one of the icon themes from the list.

Installing a Better Programming Font

Programming fonts are a special group of typefaces, typically with fixed-width characters called monospace. There’s a long list of available fonts to browse at ProgrammingFonts.org:

Selecting a programming font

Once you’ve selected a font, go to Nerd Fonts to download it and install the font on your operating system. The downloads at Nerd Fonts are copies of the monospace fonts with support for glyphs, otherwise known as icons, which you can use inside the terminal prompt.

After installing your selected font, you need to change the editor.fontFamily setting to the name of the new font. You can do that by navigating to Code → Preferences → Settings and then selecting Text Editor → Font in the navigation dropdown. You can change the font your editor will use by adding the name of your new font in the first field of the Font Family setting.

Remove ads

Setting Up Your Terminal

You’ll spend much of your Python development time inside a shell terminal. You can use a separate application, like iTerm2 or Windows Terminal, but VS Code already has a powerful built-in terminal window.

Because VS Code does need a bit of configuration to make it as powerful as a dedicated terminal application, you’ll set that up now.

Changing the Default Shell Provider

You can reconfigure the terminal window to have multiple profiles. These profiles form the dropdown list when you spawn a new terminal.

VS Code allows you to configure multiple terminal profiles. You can’t edit terminal profiles in the settings UI, so you need to use the Preferences: Open Settings (JSON) command in the Command Palette to open the settings.json file. You’ll notice that key-value pairs make up the settings file. Each key represents the configuration setting for VS Code or one of its extensions.

There are three terminal profile settings, which have the following names:

Operating System Terminal Profile Settings
Windows terminal.integrated.profiles.windows
Linux terminal.integrated.profiles.linux
macOS terminal.integrated.profiles.osx

Once you type in the name, the editor autocompletes the default profiles into the JSON file.

Note that there’s no Python REPL profile in the default settings. You can add a profile with the key "python3-repl" to the integrated profiles JSON file so that VS Code makes it available as a profile option and drops you straight onto the REPL prompt:

JSON "python3-repl":{ "path":"python3", "args":["-q"] }

Từ khóa » Visual Studio Code Run Python Module