Upgrading Python while using pipenv

I recently purchased a new laptop and while setting up the machine, python 3.9 was installed. I cloned my github repositories - projects started on an older machine using Python 3.8 and pipenv. pipenv install command failed with errors. The error was Python 3.8 (which was used to create the original project) was not found.

Pip, Venv, and Pipenv

Pip is the package manager for Python. It manages (install, update, uninstall) all the required packages. Virtualenv manages virtual environments required for different projects on the same machine. For example, you might be using Django 3.2 for Project A and Django 4.1 for Project B which requires you to install both versions of django. This is achieved through Virtual environments. An environment per project is created which contains Python installation as well as the packages. The default version of python installed on your machine is used to create the environment.

Pipenv combines Pip and Venv into a single module. It installs the packages in a virtual environment and maintains the list of the packages and dependencies in a Pipfile. Pipfile also maintains the information on which version of Python was used. The below image shows python_version = 3.9

pipfile

Moving to a new machine with different version of python

After cloing your repo to a new machine with a different (3.9 in my case) version of python, when we do a pipenv install, the expectation is pip installs the required version of Python and dependencies. But this is not true. Pipenv only installs the packages, assumming that the version of python mentioned in the pipfile is available on the target machine. If you have installed a newer version of Python, the requirement will not be met and Pipenv will return an error - expected version of python is not found

Solution

Pipfile has to be manually edited. The python version mentioned in pipfile has to be changed to the newer version that you have installed on the new machine. In the image I changed it from 3.8 to 3.9

When you try pipenv install with the new pipfile, the condition that the mention python version should be instaled is already satisfied and pipenv installs the rest of the packages and dependencies. New piplock file is autogenerated.