Skip to content

Latest commit

 

History

History
192 lines (166 loc) · 7.58 KB

VSCode.md

File metadata and controls

192 lines (166 loc) · 7.58 KB

VS Code for Python on macOS

Here I will describe how do I use VS Code in my daily work for Python web development on macOS.

Install VS Code

Download and install VS Code from code.visualstudio.com and then follow instructions below about how to configure it to be really useful for you.

Install extensions

  • Python - adds features such as IntelliSense, linting, debugging, code navigation, code formatting, Jupyter notebook support, refactoring, variable explorer, test explorer, snippets, and more!
  • Project Manager - must have extension to have fully functional projects. When you open a project - all opened files are restored too, like you want
  • GitHub Pull Requests and Issues allows you to work with PRs within the VS Code. No need to open web browser anymore!
  • Prettify JSON - make a one-line unreadable JSON to be formatted for human. Just open command palette and select Prettify JSON command - it will do the magic for the currently open file
  • Code Spell Checker - checks your Python and Markdown files on typos. Nice to have
  • Docker - makes it easy to build, manage and deploy containerized applications from Visual Studio Code
  • DotENV - VSCode .env file syntax highlighting

How to use Visual Studio Code

The main feature that you need to use again and again is the Command Palette that is open with ⇧ ⌘ P combination. In the Command Palette you can do almost anything: open a project, transform selection to upper-case, prettify JSON, etc.

Before the first use, I recommend to read Visual Studio Code: User Interface.

Configure VS Code for Python

Basic setup of the editor

Here we will do next adjustments:

  • set text size to be more comfortable for your eyes
  • add ruler to show 80 and 100 characters border
  • wrap long lines of text to be limited by 99 characters. It especially useful in Markdown files
  • set python linter rules to notify you about long lines of code

Click on Cog Wheel and then click on the Settings option. Then click on the Open Settings (JSON) icon on the top right side of the window.

Change User settings to:

{
    "workbench.startupEditor": "newUntitledFile",
    "editor.fontSize": 14,
    "editor.wordWrap": "wordWrapColumn",
    "editor.wordWrapColumn": 80,
    "editor.rulers": [
        80,
        100
    ],
    "git.confirmSync": false,
    "git.autofetch": true,
    "files.trimTrailingWhitespace": true,
    "explorer.confirmDragAndDrop": false,
    "python.venvPath": "~/.virtualenvs",
    "python.venvFolders": [
        "~/.virtualenvs"
    ],
    "python.linting.pycodestyleEnabled": true,
    "python.linting.pycodestyleArgs": [
        "--max-line-length=99"
    ],
    "terminal.integrated.fontSize": 14,
    "window.zoomLevel": 0,
    "[markdown]": {
        "editor.wordWrap": "wordWrapColumn",
        "editor.quickSuggestions": false
    },
    // Sort imports on each file save. Do `pip install isort` to make it working
    "[python]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    },
    // Settings to be passed to the isort. All options are: https://pycqa.github.io/isort/docs/configuration/options.html
    "python.sortImports.args": [
        // In case if you use one import per line
        "--force-single-line-imports",
        "--atomic",
        "--line-length", "100",
        // In case if you have some folder that needs to be recognized as "local" one, but it's a symlink for example
        "--project", "common",
    ]
}

Change Workspace settings to (but replace project-name to your virtual env name):

{
    "python.pythonPath": "~/.virtualenvs/project-name/bin/python",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./src",
        "-p",
        "test_*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}

Debug code

You can debug code of the running web app, your celery task worker or even you can run unit tests with debug enabled.

Create a file in your project's folder .vscode/launch.json and change it's content to:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/src/manage.py",
            "args": [
                "runserver",
                "8077",
                "--noreload"
            ],
            "django": true,
            "env": {
                "ENV_NAME": "development",
            },
            // To debug external libs (e.g. django) - set to "false"
            "justMyCode": false,
            "cwd": "${workspaceFolder}/src"
        },
        {
            "name": "Celery",
            "type": "python",
            "request": "launch",
            "module": "celery",
            "args": [
                "-A",
                "project-name",
                "worker",
                "-l",
                "info",
                "-P",
                "solo"
            ],
            "django": true,
            "env": {
                "ENV_NAME": "development",
            },
            "cwd": "${workspaceFolder}/src"
        },
        {
            "name": "Tests",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/src/manage.py",
            "args": [
                "test",

                // To test only one app - specify it's name below:
                // "app-name",
 
                // We can keep DB between runs
                // "--keepdb",
                
                // And can stop when we find the first failing test, instead of waiting for all tests to be finished
                // "--failfast",

                // Use it always, it will answer "yes" to all prompts, like to destroy DB before each test run
                "--noinput"
            ],
            "django": true,
            "env": {
                "ENV_NAME": "testing",
            },
            "cwd": "${workspaceFolder}/src"
        }
    ]
}

Now click on the Run (Debug) icon on the left side of the VS Code window. And now you can run Django web server, Celery worker or unit tests in debug mode.

Problems and solutions

Cmd + T shows nothing

When you try to Go To Symbol in workspace command, or just press Cmd + T - the empty list appear (with just a message No symbols matching).

To fix this problem, you could open the Settings menu and find Jedi there (using the search bar on the top). Then uncheck option for Python: Jedi Enabled and restart the VS Code. Next time when you will press Cmd + T it will index all your files (it will take a while) and the list of all symbols across the workspace will appear in the list.