Skip to content

Instantly share code, notes, and snippets.

@ceshine
Last active August 5, 2019 09:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ceshine/0054390380b9e341c66d36a8b5e2a48b to your computer and use it in GitHub Desktop.
Save ceshine/0054390380b9e341c66d36a8b5e2a48b to your computer and use it in GitHub Desktop.
How To Develop Python Programs in Visual Studio Code

How To Develop Python Programs in Visual Studio Code

Prerequisites

You have to already have these in your system:

Start a New Work Space

Simply run code . in the command line inside the root folder of the project.

Environment Settings

Select a Python interpreter (environment)

Conda and venv environments should already have been detected by Visual Studio Code. You just need to pick one to use. Three ways to do this:

  1. Use the Command Palette (Ctrl+Shift+O) —— Type Python: Select Interpreter.
  2. Click Select Python Environment option on the status bar.
  3. Configure the python.pythonPath key in the global/workspace settings.

Linter(s)

Setup:

  1. Use the Command Palette (Ctrl+Shift+O) —— Python: Select Linter
  2. Add "python.linting<linter>Enabled": true to the global/workspace settings.

Recommended linters:

  • pylint
  • mypy (an experimental optional static type checker for Python)
    • This is really new to me, but it is already awesome enough to be highly recommende.
    • One obvious benefit is the lower number of type-related bugs.
    • Another one is the better-informed autocomplete.

Formatter

Setup:

  • Add "python.formatting.provider": "<formatter>" to the global/workspace settings.

Formatters:

  • autopep8 - A tool that automatically formats Python code to conform to the PEP 8 style guide.
  • yapf - A formatter for Python files
  • black - The Uncompromising Code Formatter

Format on save:

  • Add "editor.formatOnSave": true to the global/workspace settings.

Unit Testing

It is recommended to set the flag value for all unittest frameworks:

  • "python.unitTest.unittestEnabled": true
  • "python.unitTest.pyTestEnabled": false
  • "python.unitTest.nosetestsEnabled": false

Frameworks:

  • unittest
  • (Recommended) pytest - helps you write better programs
  • nose - nose is nicer testing for python

Jupyter Integration

Useful Hotkeys

Viewing

When highlighting the variable/function/class of interest:

  • Go to Definition: F12
  • Peek Definition: Ctrl+Shift+F10
  • Find all References: Shift+F12
  • Rename Symbol: F2

Autocomplete / IntelliSense

  • Trigger IntelliSense: Ctrl+Space
  • Insert the selected member: Tab or Enter

Terminal

  • Pull up the terminal window: Ctrl+`

Extensions

  • Numbered Bookmark - Mark lines in the editor and easily jump to them. In Delphi style.
  • Gitlens - Git supercharged
  • autoDocstring - Generates python docstrings
    • Keyboard Shortcut: ctrl + shift + 2
  • neuron - An Interactive Programming Experience for Data Scientists
    • WARNING: current not working in my environment. (There is an issue reporting the same problem.)

References:

  1. VS Code Top-Ten Pro Tips
  2. How to use VS Code for your Python projects
  3. (Official Page) Python in Visual Studio Code
  4. Use Visual Studio Code for Python Development
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment