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. pipenv
was developed to integrate pip
and 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 pythonPath
explicitly 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 --where
or pipenv --venv
.
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 pythonPath
in the settings.json
. You can use VSCode’s interface to select Open Workspace Settings (JSON)
, or create folders .vscode
and files settings.json
manually. The contents of the file settings.json
should 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 pylint
when 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 🙂
When I try to run `pipenv –python` I get the error:
`Error: Option ‘–python’ requires an argument.`
sorry, my mistake, I got the wrong command. The correct one is pipenv –venv or pipenv –where