# Introduction and installation of code editor VSCode

One of the coolest code editors available to programmers, [Visual Studio Code](https://code.visualstudio.com/docs), is an open-source, extensible, light-weight editor available on all platforms. It’s these qualities that make Visual Studio Code from [Microsoft](https://azure.com/python) very popular, and a great platform for Python development.

**In this article, you’ll learn about Python development in Visual Studio Code, including how to:**

* **Install** Visual Studio Code
* Discover and install **extensions** that make Python development easy
* Write a straightforward **Python application**
* Learn how to **run** and **debug** existing Python programs in VS Code

It is the most popular code editor. It allows users with different types of in-app installed extensions for the different types of their supported languages. It allows the programmers to write the code with ease with the help of these extensions. Also, Visual Studio Code has a great vibrant software UI with amazing night mode features. It suggests auto-complete code to the users which suggests the users complete their code with full ease.

## Installing Visual Studio Code on Windows

Follow the below steps to install **Visual Studio Code** on Windows:

**Step 1:** Visit the [official website](https://code.visualstudio.com/docs/?dv=win) of the **Visual Studio Code** using any web browser like Google Chrome, Microsoft Edge, etc.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FK3rQoXOYMbRKkSaonILa%2F1.png?alt=media&#x26;token=3fa3b1a2-d259-4211-97c0-18c30fc6bc4a" alt=""><figcaption><p>Download VSCode</p></figcaption></figure>

**Step 2:** Press the “**Download for Windows**” button on the website to start the download of the Visual Studio Code Application.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FK3rQoXOYMbRKkSaonILa%2F1.png?alt=media&#x26;token=3fa3b1a2-d259-4211-97c0-18c30fc6bc4a" alt=""><figcaption><p>Download VSCode For Windows</p></figcaption></figure>

**Step 3:** When the download finishes, then the Visual Studio Code icon appears in the downloads folder.

**Step 4:** Click on the installer icon to start the installation process of the Visual Studio Code.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FGNjWobEcYUXsA83XDqEr%2F2.png?alt=media&#x26;token=da4734ae-0f2b-4fef-b316-414243cb283c" alt=""><figcaption><p>VSCode Installer</p></figcaption></figure>

**Step 5:** After the Installer opens, it will ask you for accepting the terms and conditions of the Visual Studio Code. Click on **I accept the agreement** and then click the **Next** button.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F3fegoIYqbr4mz5DAW43G%2F3.png?alt=media&#x26;token=11cd0294-4a53-4310-9439-c7c878c31de9" alt=""><figcaption><p>VSCode Setup</p></figcaption></figure>

**Step 6:** Choose the location data for running the Visual Studio Code. It will then ask you for browsing the location. Then click on **Next** button.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FZAgiQU9zQh4cGipqcLNi%2F5.png?alt=media&#x26;token=0d2eccaf-bdcf-4d02-afe6-207a7f4b125c" alt=""><figcaption><p>VSCode</p></figcaption></figure>

**Step 7:** Then it will ask for beginning the installing setup. Click on the **Install** button.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F1Nft2ZOdSsXDSmbJmVAE%2F6.png?alt=media&#x26;token=3140700f-223a-4805-a80a-40f3b153a53d" alt=""><figcaption><p>VSCode ready to install</p></figcaption></figure>

**Step 8:** After clicking on Install, it will take about 1 minute to install the Visual Studio Code on your device.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F8hkuC0t1XdoguGv7G6Rm%2F7.png?alt=media&#x26;token=6db084e4-3934-4c96-a1c6-28654c551d69" alt=""><figcaption><p>Installing VSCode</p></figcaption></figure>

**Step 9:** After the Installation setup for Visual Studio Code is finished, it will show a window like this below. Tick the “**Launch Visual Studio Code**” checkbox and then click **Next**.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FKUxl9r2uDOP71GL0VKZj%2F8.png?alt=media&#x26;token=4b5798c5-53f4-471e-9d55-360fc88c7192" alt=""><figcaption><p>Completing the VSCode Setup Wizard</p></figcaption></figure>

**Step 10:** After the previous step, the **Visual Studio Code window** opens successfully. Now you can create a new file in the Visual Studio Code window and choose a language of yours to begin your programming journey!

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F2Bl1S2vLecmaQD1HM87p%2F9.png?alt=media&#x26;token=d7d72e15-d537-45c3-b1b7-2b810bdd6d85" alt=""><figcaption><p> Get Started - Visual Studio Code</p></figcaption></figure>

## Extensions for Python Development <a href="#extensions-for-python-development" id="extensions-for-python-development"></a>

As stated above, VS Code supports development in multiple programming languages through a well-documented [extension model](https://code.visualstudio.com/docs/editor/extension-gallery). The [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) enables Python development in Visual Studio Code, with the following features:

* Support for Python 3.4 and higher, as well as Python 2.7
* Code completion with [IntelliSense](https://code.visualstudio.com/docs/editor/intellisense)
* [Linting](https://code.visualstudio.com/docs/python/linting)
* [Debugging support](https://code.visualstudio.com/docs/python/debugging)
* [Code snippets](https://code.visualstudio.com/docs/editor/userdefinedsnippets)
* [Unit testing support](https://code.visualstudio.com/docs/python/unit-testing)
* Automatic use of [conda and virtual environments](https://code.visualstudio.com/docs/python/environments)
* Code editing in [Jupyter environments](https://code.visualstudio.com/docs/python/editing#_jupyter-code-cells) and [Jupyter Notebooks](https://code.visualstudio.com/docs/python/editing#_open-jupyter-notebooks)

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F8kpYPKOtYTex3hKsIZFj%2Fpython-extension-webpage.webp?alt=media&#x26;token=ed2556f1-0b6b-4989-9bc4-396432af92a8" alt=""><figcaption><p>Visual Studio Marketplace</p></figcaption></figure>

Visual Studio Code extensions cover more than just programming language capabilities:

* [Keymaps](https://marketplace.visualstudio.com/search?target=VSCode\&category=Keymaps\&sortBy=Downloads) allow users already familiar with Atom, [Sublime Text](https://realpython.com/setting-up-sublime-text-3-for-full-stack-python-development/), [Emacs](https://realpython.com/emacs-the-best-python-editor/), [Vim](https://realpython.com/vim-and-python-a-match-made-in-heaven/), [PyCharm](https://realpython.com/pycharm-guide/), or other environments to feel at home.
* [Themes](https://marketplace.visualstudio.com/search?target=VSCode\&category=Themes\&sortBy=Downloads) customize the UI whether you like coding in the light, dark, or something more colorful.
* [Language packs](https://marketplace.visualstudio.com/search?target=VSCode\&category=Language%20Packs\&sortBy=Downloads) provide a localized experience.

Here are some other extensions and settings I find useful:

* [GitLens](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens) provides tons of useful Git features directly in your editing window, including blame annotations and repository exploration features.
* Auto save is easily turned on by selecting `File, Auto Save` from the menu. The default delay time is 1000 milliseconds, which is also [configurable](https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save).
* [Settings Sync](https://marketplace.visualstudio.com/items?itemName=Shan.code-settings-sync) allows you to synchronize your VS Code settings across different installations using GitHub. If you work on different machines, this helps keep your environment consistent across them.
* [Docker](https://code.visualstudio.com/docs/azure/docker) lets you quickly and easily work with Docker, helping author `Dockerfile` and `docker-compose.yml`, package and deploy your projects, and even generate the proper Docker files for your project.

Discovering and installing new extensions and themes is accessible by clicking on the *Extensions* icon on the Activity Bar. You can search for extensions using keywords, sort the results numerous ways, and install extensions quickly and easily. For this article, install the Python extension by typing `python` in the *Extensions* item on the Activity Bar, and clicking *`Install`*:

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FQKh53NAj6FPkfctrJXsn%2Fvscode-marketplace.gif?alt=media&#x26;token=718c6375-46a9-4a13-8e8a-e2f2795be17a" alt=""><figcaption><p>Install VSCode extensions</p></figcaption></figure>

## Start a New Python Program <a href="#start-a-new-python-program" id="start-a-new-python-program"></a>

Let’s start our exploration of Python development in Visual Studio Code with a new Python program. In VS Code, type `Ctrl`+`N` to open a new File. (You can also select *`File`, New* from the menu.)

> ***Note:** The Visual Studio Code UI provides the* [*Command Palette*](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette)*, from which you can search and execute any command without leaving the keyboard. Open the Command Palette using Ctrl+Shift+P, type `File: New File`, and hit Enter to open a new file.*

No matter how you get there, you should see a VS Code window that looks similar to the following:

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FNLv3522hf4Es9LUk93nv%2Fvscode-new-file.webp?alt=media&#x26;token=cdeafcf0-8152-4cc0-8f39-15fbb0008205" alt=""><figcaption><p>A new file in VSCode</p></figcaption></figure>

Once a new file is opened, you can begin entering code.

## Entering Python Code <a href="#entering-python-code" id="entering-python-code"></a>

For our test code, let’s quickly code up the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) (which finds all primes less than a given number). Begin typing the following code in the new tab you just opened:

```python
sieve = [True] * 101
for i in range(2, 100):
```

You should see something similar to this:

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FDCuXFYjzyhRCbp7gV3BP%2Fvscode-unformatted-code.webp?alt=media&#x26;token=44c41dc1-ccd5-42ac-927f-078de60de54f" alt=""><figcaption><p>Entering Python code in VSCode</p></figcaption></figure>

Wait, what’s going on? Why isn’t Visual Studio Code doing any keyword highlighting, any auto-formatting, or anything really helpful? What gives?

The answer is that, right now, VS Code doesn’t know what kind of file it’s dealing with. The buffer is called `Untitled-1`, and if you look in the lower right corner of the window, you’ll see the words *Plain Text*.

To activate the Python extension, save the file (by selecting *File, Save* from the menu, *`File:Save` File* from the Command Palette, or just using `Ctrl`+`S`) as `sieve.py`. VS Code will see the `.py` extension and correctly interpret the file as Python code. Now your window should look like this:

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FarH3k2CsAuDr1OL5MRiD%2Fvscode-formatted-code.webp?alt=media&#x26;token=e6eda766-0f02-4d1b-bedb-e6ae04fb73e7" alt=""><figcaption><p>VSCode formatted code</p></figcaption></figure>

That’s much better! VS Code automatically reformats the file as Python, which you can verify by inspecting the language mode in the lower left corner.

If you have multiple Python installations (like Python `2.7`, Python `3.x`, or Anaconda), you can change which Python interpreter VS Code uses by clicking the language mode indicator, or selecting *Python: Select Interpreter* from the Command Palette. VS Code supports [formatting](https://code.visualstudio.com/docs/python/editing#_formatterspecific-settings) using `pep8` by default, but you can select `black` or `yapf` if you wish.

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F1iXriyJEEmQmTepRlsJw%2Fezgif.com-video-to-gif.gif?alt=media&#x26;token=e42639a1-75e6-4e06-becf-3960da922042" alt=""><figcaption><p>Change the python interpreter</p></figcaption></figure>

Let’s add the rest of the Sieve code now. To see IntelliSense at work, type this code directly rather than cut and paste, and you should see something like this:

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2F4bj3mxLwjSqM2RFNjlma%2Ftyping-sieve-code.3d2ea083d917.gif?alt=media&#x26;token=7de7dc47-0fe3-4528-a9f4-f33022817b87" alt=""><figcaption><p>Typing sieve code</p></figcaption></figure>

Here’s the full code for a basic Sieve of Eratosthenes:

```python
sieve = [True] * 101
for i in range(2, 100):
    if sieve[i]:
        print(i)
        for j in range(i*i, 100, i):
            sieve[j] = False
```

As you type this code, VS Code automatically indents the lines under `for` and `if` statements for you properly, adds closing parentheses, and makes suggestions for you. That’s the power of IntelliSense working for you.

## Running Python Code

Now that the code is complete, you can run it. There is no need to leave the editor to do this: Visual Studio Code can run this program directly in the editor. Save the file (using Ctrl+S), then right-click in the editor window and select *Run Python File in Terminal*:

<figure><img src="https://847702634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6i4tTDVzxDUhKzBX7GT3%2Fuploads%2FPWLZHisokMGKZE8zHCxN%2Fezgif.com-video-to-gif%20(1).gif?alt=media&#x26;token=8985340d-eddc-46e6-9f5a-31113aa83b80" alt=""><figcaption><p>Run Python code in VSCode</p></figcaption></figure>

You should see the Terminal pane appear at the bottom of the window, with your code output showing.

## Conclusion

Visual Studio Code is one of the coolest general purpose editors and a great candidate for Python development. In this article, you learned:

* How to install VS Code
* How to find and install extensions to enable Python-specific features
* How VS Code makes writing a simple Python application easier

Visual Studio Code has become my default editor for Python and other tasks, and I hope you give it a chance to become yours as well.
