Hacks by Ruddra

Use VS Code for Python and Django Development

Use VS Code for Python and Django Development

Visual Studio Code is an editor developed by Microsoft. I have been using this editor for Python development for a few months now. Previously I have been using Pycharm Community Edition for development, but I had to switch to an editor which was less resource consuming than Pycharm, hence I found VS Code.

It was initially suggested to my by one of my colleagues. My first impression was, what is this? Is it really usable? Is it as bad as Atom?(I have a dreadful experience with Atom, although Atom is maybe as good as VS Code). But instead, I found that it is really useful, user-friendly and has lots of useful features.

Let us check out, how we can use VS Code to develop Production grade Python applications.

Configuring VS Code

Go to Settings

You can check VS code’s documentation on how to go to User and Workspace Settings. I prefer to use Command Palette for them.

To go to Workspace Settings, press CTRL+SHIFT+P(CMD+SHIFT+P for MacOS), it will open up Command Palette, then type Preferences: Open Workspace Settings. After pressing Enter(or click), you will go into Workspace settings(JSON).

And to go to User settings,press CTRL+SHIFT+P(CMD+SHIFT+P for MacOS) and type Preferences: Open Settings(JSON).

Configuring Python

For python related configurations, we are going to use Workspace Settings.

Install Python Plugin

For using pythonic features, you need to install plugin Python VS Code To do that, go to Extensions Section of VS Code(marked blue in the image given below), in search section, type python; and install the red marked package called Python by Don Jayamanne(Now Maintained by Microsoft).

Python

Add Virtual Environment

Here is how to configure the virtual environment:

{
  "python.pythonPath": "/path/to/virtualenv/bin/python"
}

That should be enough to let you use the virtual environment for development.

After that, let us add some more features which are useful to develop python codes.

Using PEP8 and Lint

To add them to vs code, add the following key values to above dictionary:

{
  "python.linting.pep8Enabled": true,
  "python.linting.pylintPath": "/path/to/virtualenv/bin/pylint",
  "python.linting.pylintArgs": ["--load-plugins", "pylint_django"],
  "python.linting.pylintEnabled": true
}

To use the above features, editor will prompt you to install pylint and autopep8, or you can install them directly in virtual environment:

pip install autopep8
pip install pylint

Generic Configurations

For generic related configurations, we are going to use User Settings.

Format on Save

Add this like in dict: "editor.formatOnSave": true It will auto format code (language does not matter).

Add Ruler in Editor

Adding rulers in editor gives you a better idea of how many words will you put on a single line, in Pep8 Standard, it’s 79. So let’s add the following key and values in the settings dictionary:

{
  "editor.rulers": [80, 120]
}

Ignoring Unnecessary Files

To ignore unnecessary files, add this following lines:

{
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    ".vscode": true,
    "**/*.pyc": true,
    "**/__pycache__/": true
  }
}

Disable Preview

When you open a file using an import file or try to go back to the daclaration of the code, vs code intends to open it in a preview window, which sometimes is annoying when you want to do it multiple steps/times. To disable it, add this:

{
  "workbench.editor.enablePreview": false
}

Increase Zoom Level

You can increase the zoom level of the IDE via adding the following settings:

{
  "window.zoomLevel": 0.15
}

Customizing Sidebar and Topbar(Or Any Other Visual Customization)

By default the font size for side bar and top bar for vscode is not that big. So, you can use Custom CSS and JS, which allow you to override css classes for VS code itself. For example, I use the following styling:

.monaco-tree-row.has-children {
  color: #eee;
  font-size: 15px;
}

.monaco-tree-row {
  font-weight: 100;
  font-size: 13px;
  color: rgb(223, 223, 223);
}

.monaco-workbench .tabs-container .tab * {
  font-size: 15px;
}

.explorer-viewlet {
  font-size: 15px;
  color: rgb(223, 223, 223);
  overflow: auto;
}

And to add this css in Custom CSS and JS, you need to add the following settings:

{
  "vscode_custom_css.imports": ["file:///path/to/css/styling.css"]
}

Please check their documentation on how to add CSS and JS paths to settings in details.

Debugging

It is really cool to have debugging feature built-in VS Code. Although as far as I tested, it works perfectly fine on Ubuntu, but not in OSX. So please check before you configure it.

Anyways, the best way to configure it for Django is to add the following lines in launch.js:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Django",
      "type": "python",
      "request": "launch",
      "stopOnEntry": true,
      "pythonPath": "${config:python.pythonPath}",
      "program": "${workspaceRoot}/manage.py",
      "cwd": "${workspaceRoot}",
      "args": ["runserver", "--noreload", "--nothreading"],
      "env": {},
      "envFile": "${workspaceRoot}/path/to/virtualenv",
      "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput",
        "DjangoDebugging"
      ]
    }
  ]
}

Or go to Debug section(Marked green in the screenshot), click the section marked as yellow and then click add configuration.

3

Then, click on Django settings to add new Django settings for debugging.

4

Useful plugins

  1. Market place is really rich lots of themes, plugins and so on.
  2. Use Sync to synchronize your settings in between VS Code instances over multiple machines.
  3. You can use Git Blame to see Git blames.
  4. IntellijIdea Keybindings allows you to use Idea’s shortcut keys in VSCode.
  5. Use Icons to beautify VS Code’s icons.

Conclusion

It goes without saying that Pycharm is the best IDE for Python development. It supports refactoring, which makes life a lot easier; has advanced debugging, and its easy to configure. Still, VS code has its own charm. I prefer VS code’s UI, its configurations(which is really dynamic and lots of options) and most importantly, I can do front end development better in VS Code, as Pycharm community edition does not support JS. Also its lightweight(not resource hungry as Pycharm).

Thanks for reading. Cheers!!

Share Your Thoughts
M ↓   Markdown
callpri
Tuesday, Feb 12, 2019

I prefer to use Codelobster IDE for it: http://www.codelobster.com

Pavel Řihošek
Tuesday, Sep 5, 2017

I have mypy enabled in my VSCode. It is more useful than pylint IMHO.

Ruddra
Saturday, Sep 30, 2017

Thanks, I did not know about mypy. Will try it out.

qiwihui
Monday, Nov 13, 2017

in section IGNORING UNNECESSARY FILES, is should be “**/*.pyc” not “**/*,pyc”

Ruddra
Tuesday, Nov 14, 2017

Thanks for noticing. Fixing it :)

Pirmin Schneider
Saturday, Nov 25, 2017

I think you should also do `pip install pylint-django` when using

jamie.william
Wednesday, Mar 14, 2018

Nice article.

noisemakers.com.ng
Sunday, Mar 17, 2019

nice information keep sharing

Lian
Wednesday, Apr 4, 2018

For me, I had the key “settings” already inside the workspace folder. I had to add the key-values under settings instead of the root.

Kokila Soral
Wednesday, May 9, 2018

This really has covered a great insight on Python. I found myself lucky to visit your page and came across this insightful read on Python tutorial. Please allow me to share similar work on Python training course . Watch and gain knowledge today.https://www.youtube.com/watch?v=e9p…

Arun ksamal
Wednesday, Sep 5, 2018

Hi Rudra ,i have try to configure django in vs code but unable to configure .so can you share your number or skaype id so that i will communicate..

Ruddra
Friday, Sep 7, 2018

Hi Arun, please feel free to share your problem in here. If its too much complicated, then please ask in stackoverflow :). Thanks