Using VS Code for Python/Django Development

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

Configuring Python

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 go to preference>settings and open workspace settings:

Here configure the virtual environment like this:

{
    "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  

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
    },

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,

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}/../virtualenv",  //virtualenv pat
            "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.

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

Useful plugins:

  1. Use Sync to synchronize your settings in between VS Code instances over multiple machines.
  2. You can use Git Blame to see Git blames.
  3. IntellijIdea Keybindings allows you to use Idea's shortcut keys in VSCode.
  4. Use Icons to beautify VS Code's icons.

Side Notes:

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 configuring it really easy. 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.

Thanks for reading. Cheers!!

comments powered by Disqus