DEV Community

Cover image for Setting up Python workspace in Visual Studio Code (vscode)
Idris Rampurawala
Idris Rampurawala

Posted on

Setting up Python workspace in Visual Studio Code (vscode)

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
  2. Installing a package manager
  3. Setting up Virtual Environment
  4. Setting up the code editor

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



$ python --version
Python 3.7.2


Enter fullscreen mode Exit fullscreen mode

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 to install pip. Again, just verify if you already have it installed on your system



$ pip --version
pip 20.0.2


Enter fullscreen mode Exit fullscreen mode

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, pyenv, etc. I will be using virtualenv for this post.



# installing virtual environment
$ pip install virtualenv
$ virtualenv --version
virtualenv 20.0.8


Enter fullscreen mode Exit fullscreen mode

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).



# 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


Enter fullscreen mode Exit fullscreen mode

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:



# 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
$


Enter fullscreen mode Exit fullscreen mode

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



# Make sure you have activated your virtual environment
(python-demo) $ pip install autopep8 pylint


Enter fullscreen mode Exit fullscreen mode

I have created a settings.json for the python environment in vscode.

Follow this gif to update your vscode editor settings.json

Python workspace helper gif

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

Lastly, install this vscode python extension to enable python support in vscode.

vscode-python-extension

Bonus 🔥

You can also add debugger configuration in your vscode workspace by following this link.


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

See ya! until my next post 😋

Top comments (7)

Collapse
 
sobolevn profile image
Nikita Sobolev

There's also one more thing that you can add to this amazing setup: linting!

VSCode can automatically find bugs, refactoring opportunities, and things to improve inside your code. I recommend to use flake8 + wemake-python-styleguide. Here's how to set things up: code.visualstudio.com/docs/python/...

And don't forget to check out wemake-python-styleguide on its own. It is the strictest Python linter out there:

GitHub logo wemake-services / wemake-python-styleguide

The strictest and most opinionated python linter ever!

wemake-python-styleguide

wemake.services Supporters Build Status codecov Python Version wemake-python-styleguide


Welcome to the strictest and most opinionated Python linter ever.

wemake-python-styleguide logo

wemake-python-styleguide is actually a flake8 plugin with some other plugins as dependencies.

Quickstart

pip install wemake-python-styleguide
Enter fullscreen mode Exit fullscreen mode

You will also need to create a setup.cfg file with the configuration.

Try it online!

We highly recommend to also use:

  • flakeheaven for easy integration into a legacy codebase
  • nitpick for sharing and validating configuration across multiple projects

Running

flake8 your_module.py
Enter fullscreen mode Exit fullscreen mode

This app is still just good old flake8 And it won't change your existing workflow.

invocation results

See "Usage" section in the docs for examples and integrations.

We also support GitHub Actions as first class-citizens Try it out!

Strict is the new cool

Strict linting offers the following benefits to developers and companies:

  1. Ensures consistency - no matter who works on it, the end product will always be the same dependable code
  2. Helps avoid potential bugs - strict rules make…
Collapse
 
idrisrampurawala profile image
Idris Rampurawala

I have already added pylint for code linting. Choosing formatter and linter is I believe a personal choice. This post is just about how to get started and not a discussion around python linters.

On a personal note, thanks for sharing this, I will definitely give it a try for one of my side projects :)

Collapse
 
tarifa10 profile image
tarifa-man • Edited

Hi there good day dear Idris -many thanks for this great article - i am going through it all - since i now want to stick with VSCode (note i am on MX-Linux so i just picked VScodium )

hopefully i can do all the things you did - on vscodium too - this would be great!!

i like your ideas and thoughts that you share here - i will go this pathway - and install all the packages you suggest.

  • great text - keep up your project here. it rocks+
Collapse
 
zyzmoz profile image
Daniel Cunha (he/him)

Cool! I did it, like, two days ago! However, I didn't know if it was right... Thanks anyway!

Collapse
 
idrisrampurawala profile image
Idris Rampurawala

Cool. Share your thoughts if you tried something different for the community.

Collapse
 
muhimen123 profile image
Muhimen

Ahhh!!! I wanted to write exactly the same topic. 😤

Collapse
 
idrisrampurawala profile image
Idris Rampurawala

Haha... I would recommend you to still write a post for the same.

Everyone has a different style of expressing and even different style of understanding. Maybe the style in which you write will help other community members.

Also, you might have different settings in vscode which we might learn from you ☺️

Always write what you feel! 😉