Common Pipenv Errors and How to Solve Them: Why Won’t it Lock?! | by Edward Krueger | Towards Data Science

By: Edward Krueger and Douglas Franklin.
This article is one of many covering Python package and environment management with Pipenv. Here, you’ll learn how to troubleshoot Pipenv installation and locking issues. For more information on Pipenv, or environments, check out this Medium article!
Pipenv combines package management and virtual environment control into one tool for installing, removing, tracking, and documenting your dependencies; and to create, use, and manage your virtual environments. Pipenv is essentially pip and virtualenv wrapped together into a single product.
When you begin a project with Pipenv, the tool automatically creates a virtual environment, a Pipfile, and a Pipfile.lock. The Pipfile is very similar to a requirements.txt file. When you install a package with pipenv install, the Pipfile is automatically updated with the new dependency.
Pipfile won’t lock when it tries to install packages that don’t exist, i.e., typos.
For Example:
pipenv install panadas
Pipenv looks for the package you specify, whether it exists or not. You see the following errors and many more in the terminal when the package does not exist or can’t be found for another reason. For now, we will focus on these two.
Locking Failed! Notice that ‘panadas’ is added to the Pipfile’s packages. This typo will be an issue because now our Pipfile contains an error, and anyone who uses this Pipfile will encounter locking and dependency errors. Be aware that the ‘Installation Succeeded’ alert is untrue; there is no Panadas to install. All that s occurred is the addition of ‘panadas’ to the Pipfile. Typos in the Pipfile are the most common reason that locking fails.
As long as this typo remains in the Pipfile, installing a package, existent or not, causes the locking to fail. You’ll need to fix the Pipfile in a text editor. I’m using nano. You can use whatever editor you are comfortable with; Sublime, VS Code, Atom, etc. Simply correct Panadas to Pandas then pipenv install.
Now let’s look at some other error messages that populated the console when locking failed.
Notice the suggestion pipenv install --skip-lock. This ought to be only used in extreme or causal programming circumstances as it bypasses the Pipfile .lock's primary purpose; locking package versions and creating the formula for installing the environment dependencies.
The second most common reason locking fails is that we attempt to install a package that is a pre-release version. You can resolve pre-release version conflicts by adding the--pre flag to the installation.
pipenv install --pre
After this command, your Pipfile has a new variable; allow_prereleases = true. Note that this will allow the installation of any pre-release package into your environment. Now is when pinning specific package versions is useful. Pinning versions is an excellent habit to possess and can prevent dependency issues in the future.
Notice in the Pipfile image above under the requires section; you see python_version = 3.7. The Pipfile specifies the Python version here.
To build a Pipfile or environment with a specific Python version, use the following syntax.
pipenv --python 3
Package versions can be also be specified when needed.
pipenv install django=1.11.10
With Pipenv and the Pipfile, you present to others a standardized way to install project dependencies and testing and development requirements. Anyone who has a copy of your Pipfile can recreate your environment with the following command.
pipenv install
This creates an environment specified by the Pipfile. Be sure the Pipfile is typo-free and that it specifies proper package and Python versions before sharing it!
If this wasn’t the pipenv solution you were looking for, check out this Medium article for help with other Pipenv errors!