awesome-python-vs-code icon indicating copy to clipboard operation
awesome-python-vs-code copied to clipboard

My Visual Studio Code Setup with Python

Awesome Python VS Code

I've been using Visual Studio Code since before the python extension was supported by Microsoft. Now a Microsoft project, the Python Extension for Visual Studio Code has become even more featureful featuring their own remote debugging tool and their own auto-completion and analysis engine through the new Language Server Protocol.

My Extensions

My configuration

{
    "python.formatting.provider": "black",
    "workbench.colorTheme": "Enki - Night Owl",
    "python.unitTest.pyTestEnabled": true,
    "editor.minimap.enabled": false,
    "editor.formatOnSave": true,
    "editor.formatOnPaste": false,
    "editor.formatOnType": false,
    "[html]": {
        "editor.formatOnSave": false
    },
    "python.autoComplete.showAdvancedMembers": true,
    "python.autoComplete.addBrackets": true,
    "workbench.iconTheme": "enki",
    "editor.cursorBlinking": "expand",
    "editor.renderWhitespace": "boundary",
    "editor.formatOnSaveTimeout": 1500,
    "python.formatting.blackPath": "/Users/erin/.pyenv/versions/3.7.0/bin/black",
    "python.formatting.blackArgs": [
        "--skip-string-normalization",
        "--line-length",
        "99"
    ],
    "python.jediEnabled": false,
    "prettier.tabWidth": 4,
    "docker.attachShellCommand.linuxContainer": "/bin/bash"
}

I usually have project specific configurations too. These are generally things like linting, the python path that pipenv created, and sometimes black arguments. My configuration file used to be a lot longer, but the python extension has made more settings I had specified deafaults, a feature I like. It works out of the box, these things just make it better.

The Python Langauge Server

The Python Language Server is a new alternative to packages like Jedi. So far I am having a positive experience with this. I just recently switched from Jedi to the languge server. To do that you have to disable Jedi by setting python.jedienabled to false. The language server will be downloaded and run. You can enable or disable this per a project. I've seen some speed improvments in autocomplete, and better detection for inherited properties and methods when working with Django models.

Pipenv Support

Out of the box the Python extension supports conda, direnv, pipenv, pyenv, venv, virtualenv. Pipenv is the one I choose to use. When I open a project that has a pipfile, visual studio code automatically picks up on the virutalenv to use when running Jedi, or it's own analysis engine against my code base. If you install any dependecies like rope for refactoring, ptvsd for remote debugging, mypy, pylint for linting, or black for formatting through visual studio code, they automatically get added to your Pipfile. When you run code, it uses the virtualenv created by pipenv to do all of it's business. It just knows about it, it's magical!

Black Support

I love using the code formatter Black. Visual studio code supports black out of the box, along with autopep8, and yapf. You can see above that I have some black arguments applied by default, with the editor set to format on save. This keeps all of my code nice a tidy, while minimizing diffs.

Debugging Support

Visual Studio Code supports debugging for a lot of different project types. Django, Flask, gevent, Jinja, Pyramid, PySpark, Scrapy, Watson are all supported. I can really only speak to the Django and Flask debugging which are FANTASTIC. The debugger will automatically run the server using manage.py or flask run and attach itself letting you set breakboints and logpoints, even from the jinja or html templates! I'm working on another post where I detail how to do remote debugging using ptvsd and docker for Django apps.

PyTest Support

When you open up a file with pytest tests in it, you will be able to run individual tests right from the editor. Sure you could run pipenv run pytest -k NAME_OF_TEST but that's so boring, just have Visual Studio Code do it for you!

Docker Support

I am sure that I am not using this to it's full potential. It's great to get a quick overview of my containers, which ones are stopped and which are running. It's also great that I can right click on any running container and attatch a shell to it. You'll notice in my configuration that I have my default command as /bin/bash instead of the default of /bin/sh when attaching a shell.

Built in terminal emulator

As Kenneth Reitz mentioned in his post, the built in terminal emualtor is fantastic. It uses the same engine as Hyper. I love not having to open another window to run commands.

Git Support

There is fantasic git support built into the editor. Besides a gitignore plugin, and a gitblame plugin, all of the stuff you'd expect to be in a plugin like git gutter are built into the editor. You can also stage, commit, push, pull, checkout, etc all through the git tab.

Key Bindings for pretty much any editor ever

If you don't want to move away from your editor because you don't want to learn a bunch of new keyboard shortcuts and bindings, fear not; there are key binding extensions for pretty much every editor out there. Want to use vim? Go for it. How about spacemacs? Yup. Coming from Sublime Text? Well you'll probably want this. Switching has never been easier!

In Conclusion

VS Code with the Python extension has been my daily driver for over two years. It's only gotten better with time. If you were thinking of making the switch, there has never been a better time! If you already use it, I hope you have enjoyed my setup.