How to use pipenv in VSCode

You are probably familiar with pip - the de-facto Python package manager. Compared to its counterpart in JavaScript (npm, yarn) or Rust (cargo), pip still lags behind on a few features such as virtual environment management, version locking, etc. pipenvwas developed to integrate pipand virtualenv functionality and supercharge pip with many more advanced features.

pipenv and VSCode

Once you have installed the Python extension (official version of Microsoft) in VSCode, by default VSCode can automatically recognize the virtual environments created on your machine. However, at the moment this extension still does not support paths generated by pipenv. The only way is to pythonPathexplicitly declare it in .vscode/settings.json.

To get the path of the virtual environment created by pipenv, you first need to go into the root directory of your project (on the same level as the Pipfile) and run the command pipenv --python. For example, my machine (MacOS) will have the following path:/Users/kettle/.local/share/virtualenvs/example-project-M27OqWV-/bin/python

Next you need to edit pythonPathin the settings.json. You can use VSCode's interface to select Open Workspace Settings (JSON), or create folders .vscodeand files settings.jsonmanually. The contents of the file settings.jsonshould be as follows:

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

In addition to make VSCode run pylintwhen coding, you can install this library as dev dependencies as follows: pipenv install --dev pylint.

That's it, features like autocomplete or terminal with virtualenv in VSCode have been integrated well with pipenv. Hope this little guide can help you 🙂

Click to rate this post!
[Total: 1 Average: 5]

Leave a Comment