Visual Studio Code, Python and pipenv

I’ve been doing some experimenting with pipenv to simplify my nascent Python programming workflows and also with Visual Studio Code as a cross-platform code editor. So naturally I want VS Code to use the python version from my pipenv-based virtual environment (as one does). I also want the compiled .pyc files to not show up in the explorer view. Fortunately, both of these things can be accomplished by overriding some default VS Code settings.

First, find out where pipenv has created your virtualenv setup and stashed the python executable you are using. From the command line in your project folder (where your Pipfile is), execute the following:

pipenv --py

This will give you the full path to your virtualenv python install. For my sample project it was /Users/pack/.local/share/virtualenvs/astra-AQkAm5fD/bin/python

Now, create a local settings file that VS Code will use for your project

mkdir .vscode && touch .vscode/settings.json

After executing that, you’ll want to open the settings.json file and paste in the following:

{
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/*.pyc": true,
        "**/__pycache__": true
    },
    "python.pythonPath": "<VIRTUALENV_PYTHON_PATH_HERE>"
}

The files.exclude block takes the existing VS Code settings for files not displayed and adds .pyc files and __pycache__ folders to the list. The python.pythonPath variable is where you need to include the virtualenv python location that you found earlier.

Now when you load up VS Code with your project, it will use the appropriate Python version. If you also have pylint installed in your virtualenv environment then it will use that version to display any linting problems. If not, you can do that with the following.

pipenv install --dev pylint