VSCode configures Python and extensions

Whether you're an experienced developer or just starting your coding adventure, getting VS Code set up correctly could be the secret to taking your skills to the next level. So let's delve into the art of creating a sublime coding haven within the virtual walls of Visual Studio Code!

Why choose VS Code?

VS Code has become one of the most popular code editors for Python development, and for several good reasons. Here are some compelling arguments for using VS Code in Python development:

  • The lightweight code editor starts quickly, making it ideal for developers who want a seamless coding experience without being resource-intensive.
  • Available on multiple platforms, including Windows, macOS, and Linux.
  • Has an extensive extension ecosystem.
  • Built-in debugger that works seamlessly with Python.
  • Various Python formatters and linters are supported.
  • Integrates perfectly with version control systems like Git.
  • Built-in terminal.
  • Intuitive user interface.
  • Large and active developer community.

In this article, we'll explore how to install VSCode and then customize it to be a powerful tool for Python development.

Install

In this section, we will discuss installing VS Code. Download and run the installer from the official website here . Check out our full guide here for system-specific instructions.

How to edit settings

Now, let's see how to modify the settings. This may be different from programs you've used before.

In VS Code, there are two main options for editing settings: Graphical User Interface (GUI) and Files settings.json. Each option has unique advantages and can satisfy different preferences and use cases.

Set up the .json file:

This settings.jsonfile is a plain text JSON file that stores all configuration settings for Visual Studio Code. It is the underlying configuration file for GUI settings modification. You can skip the GUI and edit this file directly.

To edit settings via file settings.json:

  1. Press Ctrl + Shift + P(Windows/Linux) or Cmd + Shift + P(Mac) to open the command palette.
  2. Type "user settings" and choose the JSON option from the list of commands.

Remember the Command Palette as we'll be using it a lot in this guide.

Graphical User Interface (GUI):

The GUI approach allows users to modify settings through the VS Code interface without directly interacting with the underlying JSON configuration file.

To access the GUI settings, click the gear icon in the lower left corner of the VS Code window. If you're a keyboard shortcut lover, use Shortcuts Ctrl + ,(Windows/Linux) or Cmd + ,(Mac).

This guide uses that settings.jsonapproach, but each item can be found in the GUI by searching for the key we provide. For example, to find rulers in the GUI, you can search for "editor.rulers".

theme

In this section, we'll learn about themes and how to tweak VS Code's visual styles. Choosing a visual theme for VS Code might seem like it's just about making your editor look nicer. In fact, it's an important decision that can have a big impact on the overall development experience and productivity. Here are a few reasons why choosing the right visual theme is crucial:

  1. Reduce eye strain: Well-designed visual themes with proper color choices, contrast, and syntax highlighting can reduce eye strain during long coding sessions.
  2. Improve code readability: Visual themes play a key role in enhancing code readability. Themes can clearly distinguish various code elements (such as keywords, functions, variables, and comments), making code easier to understand at a glance.
  3. Personalization and Motivation: Visual themes allow developers to create an environment that resonates with their personality and preferences. A visually appealing and personalized coding space can boost motivation and inspire creativity, making coding sessions more engaging and fun.
  4. Accessibility and inclusivity: Choosing an accessible theme that considers color contrast and readability is critical to ensuring inclusivity. Themes that cater to different needs, including those of developers with visual impairments or color blindness, foster an inclusive coding environment.

Browsing themes in VS Code can sometimes be frustrating as there is no library for quick browsing. Instead, you have to find each theme and open it in the extensions marketplace. We've found that VS Code themes provide a nifty way to quickly browse through the various color themes to choose from.

We love the One Dark Pro. However, it cannot be overstated how important it is for you to find a theme that suits your needs.

Once you've selected a theme, you can select it, then select the VS Code button. This will open the theme extension in the extensions marketplace.

Select Install from the marketplace page. After installation, the theme will be automatically enabled.

Some themes offer multiple options. Here you can see One Dark Pro has 4 different color themes to choose from.

You can also set it in settings.json.

Add that workbench.colorThemekey and VS Code will display a list of themes to choose from. If you want to choose a different theme that you have installed, you can do this from the command palette.

Open the command palette and enter "color theme". Choose Preferences: Color Theme Options. Of course, you can also use the settings.json.

We recommend making small tweaks to make your editor easier to understand. Feel free to use whatever you find useful. these options

window.zoomLevelSet the UI scale small enough to accommodate large amounts of content, but large enough to be clearly visible. You can zoom in and out using and on macOS Cmd +, or on Windows and Linux.Cmd -Ctl +Ctl -

workbench.tree.indentAdd more horizontal insets to the File Explorer tree. This makes viewing nested content easier.

editor.minimap.renderCharactersMake the minimap show shape lines instead of actual text. Let's be honest, no one is reading the minimap text.

editor.minimap.sizeResizes the minimap so it's usable but doesn't take up a lot of space.

files.autoSaveYour most recently modified files are automatically saved. You can set it to save after changing tabs, changing windows, or after a delay. If you choose the delay option, it must be set files.autoSaveDelayto a number of seconds.

editor.cursorStyleand editor.cursorBlinkingcontrol the type of cursor you use. We prefer the old-fashioned block and block outline options.

Ruler

Rulers act as subtle guides, preventing code from becoming too large, thereby encouraging simplicity and readability. Embracing the power of the ruler within the VS Code environment enables you to write clean, well-organized code that not only complies with industry standards, but makes working with you and your fellow developers a joy. So let your code dance gracefully along the ruler and witness how this seemingly small addition can have a big impact on the overall elegance and professionalism of your project.

Adding rulers to VS Code is a simple process. Open it settings.jsonand add the following lines:

Here, we're using 88 because that's consistent with the black formatter we'll discuss later in this article. Use this value if your linter or formatter prefers different line lengths. You can also do this if you want multiple scales:

The result is a vertical line of the selected line length.

linter

A linter is an important tool that plays a vital role in improving the overall quality and maintainability of your code. It helps you comply with Python coding standards and best practices by analyzing your code for potential errors, style inconsistencies, and syntax issues. By highlighting these issues in real-time as code is written, linters can help you identify and correct errors early in the development process, reducing the chance of bugs being introduced and enhancing the readability of your codebase. Using a linter not only promotes clearer, more standardized coding practices across your team, it also helps improve collaboration, making it an indispensable tool for any Django developer using Python in a VS Code environment.

The number of options for linters is constantly growing. Here are some of the most popular:

Flake 8

We're partial to Raff. It is 100 times faster than other linters, supports the latest versions of Python, and has almost the same rules as Flake8. Of course, if you prefer another linter, use it. Most configurations are similar.

Setting up Ruff couldn't be easier. Just install the Ruff extension from the marketplace here . It starts working immediately. Whichever linter you choose, add the following to your settings.json.

These two options will enable linting when you save the file. Through our testing, we discovered some Ruff-specific configuration options that can improve our productivity. Let's take a look at these.

Import automatic sorting

The PEP8 style guide details how to organize imports. This helps keep the code readable and maintainable. Automatic sorting will first build your Python imports by source (e.g. standard library, third-party, native application) and then sort them alphabetically within those sections.

editor.codeActionsOnSaveEnable it by adding the section to the Python scope in settings.json.

Here you can see an example of the previous import sort:

after:

Automatically fix violations

Violations in the code can only be discovered if they are fixed. You must act on each one. Ruff provides a way to automatically fix violations. source.fixAllEnable it by adding the key to editor.codeActionsOnSavethe Python scoped section.

Here you can see an example of automatically fixing violations. Ruff recognizes that the class Postis imported but not used.

Ruff Postsolves this by removing the import.

format

A code formatter is a tool that analyzes and automatically reorganizes source code according to predefined coding styles and conventions. Its purpose is to achieve consistency in code layout, indentation, line breaks, and other formatting aspects without changing the functionality of the code. By enforcing a consistent code style throughout a project, formatters enhance the readability of code, making it easier to understand and minimizing potential errors caused by inconsistent formatting.

Just like linters, there are many formatters to choose from. Here are some of the most popular:

We prefer the black style option. Black's approach follows the principles of an "uncompromising code formatter" and is widely recognized for its ability to produce highly readable, consistent code.

For example, Black uses a line length of 88 characters. This is in contrast to other formatters which use 80 characters. The decision to use a 10% longer limit can have a huge impact on file length. It also allows for fewer line breaks, resulting in more readable code. Small but impactful choices like this make black an easy choice for our development. As always, choose a formatter that fits your needs.

Black extensions can be found here . Install it like any other extension. Once installed, turn on settings.jsonboth options and add it to your python scope.

These lines set the default formatter to black and enable formatting on save. This means that Black will only format your code when you save the file.

git

VS Code integrates very well with version control systems like git. Simply installing git is enough for the editor to work its magic. If you need to install git, check out the guide provided by git itself. Once set up, your main sidebar will have a source control button.

Source control button for the main sidebar.

in conclusion

You now have a fully configured VS Code with gorgeous color themes, linting, formatting, and some useful extensions installed to make your coding time more efficient and enjoyable! By harnessing the power of Visual Studio Code and its versatile configuration, developers can realize their full potential and take Python development to new heights. As development environments continue to evolve, VS Code's adaptability and rich functionality will undoubtedly keep it at the forefront of modern coding environments, enabling developers to easily write clean, efficient, and robust Python applications.

Guess you like

Origin blog.csdn.net/qq_41929396/article/details/132580335