Setting up Python workspace in Visual Studio Code (vscode)
Tags: beginners, python, vscode
Published: 2020-03-10 12:16:16

---
title: Setting up Python workspace in Visual Studio Code (vscode)
published: true
description: Whenever as a programmer we start a new project (in any language), we are required to set up an environment for our project to run. The environment may include things such as editor, packages supporting our project, language-specific linter and formatter, etc. A stable environment helps you to focus on the main aspects of implementation and leaving everything else with the setup to handle. Let's get started setting up a Python environment in Visual Studio Code editor.
tags: beginner, python, vscode
cover_image: https://res.cloudinary.com/idr/image/upload/fl_lossy,q_50/dev.to/vscode-python-extension_wloqzn.png
---
Whenever as a programmer we start a new project (in any language), we are required to set up an environment for our project to run. The environment may include things such as editor, packages supporting our project, language-specific linter and formatter, etc. A stable environment helps you to focus on the main aspects of implementation and leaving everything else with the setup to handle. Let's get started setting up a Python environment in vscode.

Before we being, please note that I have chosen vscode because I am used to working with it and you might have a different choice. It is totally fine as long as it helps you to enjoy your coding experience.

Setting up an environment for any language can be prescribed as the following points:
1. [Installing language-specific compiler/interpreter](#installing-interpreter)
2. [Installing a package manager](#installing-a-package-manager)
3. [Setting up Virtual Environment](#setting-up-virtual-environment)
4. [Setting up the code editor](#setting-up-the-code-editor)

## <a name="installing-interpreter"></a> 1. Installing language-specific compiler/interpreter
For python, you will have to install a python Operating system specific interpreter to be able to execute your code. Just visit this [link](https://www.python.org/downloads/) and install the appropriate version of python in your machine. Also, make sure you have correctly installed it on your system by the following command:
```sh
$ python --version
Python 3.7.2
```

## <a name="installing-a-package-manager"></a> 2. Installing a package manager
`pip` is a very popular python package installer. It helps you to manage your python packages. You can visit this [link](https://pip.pypa.io/en/stable/installing/) to install `pip`. Again, just verify if you already have it installed on your system
```sh
$ pip --version
pip 20.0.2
```

## <a name="setting-up-virtual-environment"></a> 3. Setting up Virtual Environment
Python applications will often use packages and modules that don't come as part of the standard library (i.e. by the above step). Applications will sometimes need a specific version of a library. It means that there might be multiple applications with different versions of python and/or modules required to run the application. Having one global version being used by all application will not suffice the needs.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

There are many python packages available for you to create virtual environment python such as [virtualenv](https://virtualenv.pypa.io/en/latest/), [pyenv](https://github.com/pyenv/pyenv#basic-github-checkout), etc. I will be using `virtualenv` for this post.
```sh
# installing virtual environment
$ pip install virtualenv
$ virtualenv --version
virtualenv 20.0.8
```

Until now, we have been installing everything globally. From now on we will be creating a virtual environment that will restrict the installation to that specific environment (folder).
```sh
# creating a project folder
$ mkdir python-demo
# creating a virtual environment for this project
$ virtualenv python-demo
# this will create a virtual environment folder <python-demo> in the current folder
```
Once we have created a virtual environment, we need to make sure we install all other python packages for this project inside this project. This is done via activating virtual environment with the following command:
```sh
# for ubuntu
$ source <virtual-env-folder-path>/bin/activate
$ source python-demo/bin/activate

# for windows
$ <virtual-env-folder-path>\Scripts\activate
$ python-demo\Scripts\activate

# After this, your command prompt/terminal will change the path with the virtual environment name
(python-demo) $

# to deactivate the virtual environment, just type the command deactivate
(python-demo) $ deactivate
$
```

## <a name="setting-up-the-code-editor"></a> 4. Setting up the code editor
Now, let's move to set up a python environment in the vscode code editor. There are possibly 2 tasks a code editor should perform whenever you write code in it - Linting and Formatting the code. Vscode supports multiple linters and formatters to help you out in your workspace. 

Before we create a workspace setting for our python environment, let's install a linter and a formatter for python. I will be using `autopep8` and `pylint` for the same. You can choose any other python package of your choice, just follow this [link](https://code.visualstudio.com/docs/python/linting)
```sh
# Make sure you have activated your virtual environment
(python-demo) $ pip install autopep8 pylint
```

I have created a `settings.json` for the python environment in vscode.
{% gist https://gist.github.com/idris-rampurawala/1a993702e53522787caac292a4cdd522 file=vscode-python-workspace-settings.json %}

Follow this gif to update your vscode editor `settings.json`

![Python workspace helper gif](https://res.cloudinary.com/idr/image/upload/v1583664520/dev.to/python-workspace_vugned.gif)

Do not forget to replace your virtual environment path with `<your-env-path>`

Lastly, install [this vscode python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) to enable python support in vscode.

![vscode-python-extension](https://res.cloudinary.com/idr/image/upload/v1583665365/dev.to/vscode-python-extension_wloqzn.png)

## Bonus :fire:
You can also add debugger configuration in your vscode workspace by following this [link](https://code.visualstudio.com/docs/python/debugging).

---

If you find this helpful or have any suggestions, feel free to comment. Also, do not forget to hit :heart: or :unicorn: if you like my post.

See ya! until my next post :yum: