VSCode, pipenv, python

vorsprung

vorsprung

Posted on December 16, 2021

VSCode, pipenv, python

Python squeezes until the pipenv squeeks

The problem I have

I don't use python a lot and although I have all the VSCode plugins for python installed, when I load a project up, there are lots of "Problems" about missing python libraries. "unable to import"
..and the problem is
The project works fine in the shell, I can run tests and all the things. All of the projects use pipenv

What I'd like

I want the missing python library warnings to disappear so I can use the "Problems" pane to find...er...real problems with the code

Problem with some online info

It seems (and I vaguely remember doing this in the past) it used to be the case that setting the pythonpath in the settings would fix this issue to a great extent. But now this does not work and the setting is marked as "deprecated"

VSCode official docs

The official docs for "Using Python environments in VS Code" are pretty good but they don't give an exact method for using pipenv. There are some good clues however. It seems that the key is to make a venv in the file folder .venv in the Project.

pipenv work around

This work around is good for Linux or a Mac.

  • go to the project directory in a shell. This can be any shell on the host, including the vscode terminal
  • ensure that no virtual environment is in effect. The deactivate command can stop a virtual environment
  • PIPENV_VENV_IN_PROJECT=1 pipenv shell
  • say yes when VSCode asks "add this environment?" We noticed a new virtual environment...
  • probably at this point you need to do pipenv update For projects with lots of dependencies, this will take a while
  • do a ⇧ ⌘ P "Developer: Reload Window"

If you still have missing libraries....

I found that the project - as well as having a Pipfile - had a library in a "vendor" folder! To fix this make a file in the project directory called .env with the contents PYTHONPATH=vendor

If the folder has a different name to "vendor" set that in pythonpath

💖 💪 🙅 🚩
vorsprung
vorsprung

Posted on December 16, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

VSCode, pipenv, python
python VSCode, pipenv, python

December 16, 2021