Visual Studio Code – code editor for Python

by Alex
Visual Studio Code - code editor for Python

To write a real project in Python, no ordinary text editor or standard IDLE will do. To simplify the process of writing code, avoid silly mistakes, and get all the tools you need in one “box,” programmers use special code editors or professional IDEs.

What is Visual Studio Code

VS Code is a code editor that supports most popular programming languages. Its advantages include:

  • Open source code.
  • Large number of plugins.
  • Small size and low requirements for computer resources.
  • Support for most operating systems.
  • Automatic use of conda and virtual environments.
  • Code editing in Jupyter.

What is Visual Studio CodeDespite its small size, VS Code supports all the tools needed to develop a complete project, such as: refactoring, integration with Git, debugging, and others.

Installation

VS Code is a cross-platform application and can be installed on any popular operating system.

Windows

To install on Windows, you need:

  1. Download the installer from the official website.
  2. Run the installer and follow the instructions.
  3. Verify that the application is available for use. By default it is installed in “…AppDataLocalProgramsMicrosoft VS Code”.

Linux

Often VS Code is available in the repositories. In this case, just execute the command in the terminal: “sudo apt install vscode“. The following installation option is also possible:

  1. Download the .deb file from the official site.
  2. Run the terminal from the directory where the file is located (or use the “cd” command).
  3. Execute the command: “sudo apt install <file>.deb“, where <file> is the name of the downloaded file.

MacOS

To install on MacOS you need:

  1. Download the installer for MacOS from the official website.
  2. Double-click the archive to open its contents.
  3. Drag and drop “Visual Studio Code.app” into Applications to make the icon available from the launcher.
  4. Add VS Code to the Dock by right-clicking the icon and choosing “Preferences” – “Save to Dock” in the context menu.

Setup for Python: useful plugins

To install the plugin you need:

  1. Click on the dice icon on the left panel – “Extensions” item.
  2. Enter the name of the necessary extension in the search field.
  3. Open the extension page by clicking on the found result.
  4. Read the information about the extension and press the green “Install” button.

Setup for Python: useful pluginsOn the screen above, the installed extension is selected. It can be uninstalled (Uninstall) or disabled without uninstalling (Disable).

Python (by Microsoft)

The “Python” extension from Microsoft is the first and most important extension for the Python programmer. It not only adds support for the language, but also implements features such as syntax highlighting, debugging, testing, formatting and smart code completion. This extension is usually enough for development, but you can look at others if you need to.

autoDocstring (by Nils Werner)

The plugin allows you to quickly create documentation for functions. Several documentation formats are available: standard, Google, NumPy and others. Writing documentation for functions is very important because it helps other programmers understand what the function does and why it is needed. For a large project there is a huge amount of documentation to be written, the autoDocstring extension greatly simplifies this process and saves time for programmers.

Code Runner (by Jun Han)

The name of the extension fully describes its purpose. It allows you to run code (not only in Python) and display results in a special built-in window. You don’t need to switch between editor and terminal because the results are printed in a special built-in window “OUTPUT”.

Trailing Spaces by Shardul Mahadik

Code in Python directly depends on trailing spaces, which define how blocks of code are nested. Incorrect indents (extra space, a mix of spaces and tabs) often cause errors, a plugin solves this problem. It highlights end spaces and allows you to remove them with one click.

Better Comments (by Aaron Bond)

The plugin allows you to create more advanced comments that are highlighted in different colors depending on their type:

  • Warning.
  • Question.
  • “TODO” – to do something.
  • Features.

Djaneiro (by Scott Barkman)

A collection of snippets (code snippets) for Django templates, models and forms. Imported into VS Code from Sublime Text.

flask-snippets (by cstrap)

Similarly Djaneiro plugin imported from other editors, it allows to use a lot of snippets for the Flask framework.

Code Linting

The plugin allows to find syntactic and stylistic problems in Python code. Problem areas of the code are highlighted with a red squiggly line, thanks to the plugin the programmer can detect the error without even running the code. In order to enable Linting in VS Code, it must be installed through the package manager: “pip install pylint

Running the code

You can run Python code without leaving the program, the process is quite convenient, but it may vary depending on the installed plugins.

Standard running code

It is necessary to right-click anywhere in the file and select “Run Python File In Terminal” in the context menu (you can use the green play button in the upper right corner). Then, the VS Code built-in terminal will open at the bottom of the screen, displaying the result of the file’s execution as well as its directory.

Standard running codeRunning Code with Code Runner

If the Code Runner extension is added to VS Code, you can run the code:

  • By clicking on the “Run Code” button in the upper right corner (which will replace the standard run button there).
  • By opening the context menu with the right mouse button on the file you want to run and selecting “Run Code”.
  • Using the keyboard shortcut “Ctrl+Alt+N”.

Unlike the standard method, Code Runner displays the results in a special Output window rather than in the terminal. Besides, it also displays the information about the error code and the script execution time.

Working with projects

VS Code makes it easy to work with a large number of files and folders, create, delete and organize them. In addition, the code editor includes things like testing, debugging, and Git integration.

Testing

VS Code is capable of automatically recognizing tests written with unittest, pytest, or Nose frameworks. To run an existing test, you need to open it in the editor, right-click anywhere in the file and select “Run Current Test File”. The program will prompt you to define the structure and location of the tests in the project, as well as create a template for the tests. All the specified data is saved in the workspace settings, in the settings.json file. The tests included in the project can be found by clicking on the bottom icon in the bottom menu (chemical vessel). From there, you can also use the green double “play” button to run all the tests.

Debugging

VS Code supports all the code debugging features you need:

  • Automatic tracking of variables.
  • Viewing of expressions.
  • Breakpoints.
  • Control of the call stack.

To start debugging mode, press the “F5” key or use one of the menus (at the top or left side of the screen). After that it will be possible to select a debugging object:

  • Script.
  • Module.
  • A django web application.
  • Flask web application, etc.

The programmer can set breakpoints, pause debugging, and “step” through the code. Important menus such as variables, view, call stack and breakpoints appear on the left side of the screen.

Integration with Git

VS Code has built-in support for version control systems such as Git and GitHub. In addition, with the help of plugins, the programmer can add support for other systems that can be shared. The version control system is used in any large project because it allows you to avoid bugs and errors that occur in new versions of the project (the project is simply rolled back to an earlier version where these errors do not exist). To use the Git features, you need to open the folder with the project. After that, select the “Source Control” button in the side menu. In it select “Initialize Repjsitory”.

Then you can do the following:

  • Writing changes to a repository.
  • Add and retrieve changes from deleted repositories.
  • Create new branches and tags or check to see if they exist.
  • View and resolve merge conflicts;
  • View differences between versions.

Working with a virtual environment (virtualenv)

The virtual environment is the context (set of dependencies) in which the project is executed. Any environment first of all consists of an interpreter and a number of installed packages. VS Code allows many useful features for working with different environments. By default, Python uses the interpreter via the path in the system variable. If you want to use a specific environment, you have to open the command palette by pressing “Ctrl+Shift+P”. Then enter there the command “Python: Select Interpreter”. You can also do this by clicking at the bottom of the screen on the current interpreter listed there. You will also get to the selection menu and you can add a new one from the virtual environment if needed.

Working with a virtual environment (virtualenv)You can switch between virtual environments at any time. This is very useful when you want to test the behavior of a project with different interpreter and library versions.

Conclusion

VS Code is a lightweight code editor that boasts powerful functionality for writing Python code. It is suitable not only for writing scripts, but also for creating serious projects. With the help of various plugins, VS Code can be adapted to perform any task in Python or any other language.

Related Posts

LEAVE A COMMENT