Dependency bugs can be a developer's worst nightmare. You can consume a lot of time upgrading and downgrading dependencies in a vain attempt to resolve issues.

Pipenv is an effective dependency manager for Python projects. It sets up and manages virtual environment dependencies in both development and production.

Pipenv handles the addition and removal of dependency packages reducing conflicts. It also improves Pip with increased security measures, versioning options, and package updates.

Why Use Pipenv Over Virtualenv?

Virtualenv is the most common Python virtual environment tool, and there are other alternatives. The advantages of using Pipenv instead of these tools include the following:

  • You can install packages without versions. Non-versioning reduces conflict when working with different versions of dependencies.
  • Pipenv combines Pip and Virtualvenv; you no longer use them separately.
  • Pipenv eliminates the requirements.txt file. Pipenv creates Pipfile to track dependencies and Pipfile.lock to build the application.
  • It supports the quick and easy setup of virtual environments.

Pipenv adds more than extra files to Pip. In the next section, learn about some additional features that make PIpenv a comprehensive package.

Upgrade Installed Packages

You may want to upgrade installed packages if there are new releases. Updated versions will likely have new features to optimize your application.

To find out new updates to dependencies, run the following command:

 pipenv update --outdated 

If new updates are available, you may need to upgrade the packages. There are two ways to upgrade packages. You can upgrade everything or you can choose to upgrade only certain packages.

To upgrade everything, run the following update command:

 pipenv update 

To upgrade only specific packages, run the code as illustrated below:

 pipenv update <name of package> # upgrades a specified package 

Importing Packages From requirements.txt

Pipenv will import the content of a requirements.txt file if installed in the same project. When you run pipenv install, it imports and creates a Pipfile.

Pipenv also allows you to import a requirements.txt file. You need to specify the location of the requirements.txt file when installing, as shown:

 pipenv install -r path/to/requirements.txt # imports a requirements file. 

The requirements.txt file will likely come with packages with version numbers. You can remove and install new packages. If you intend to keep the versioned packages, run the following command:

 pipenv lock --keep-outdated 

Pipenv Improved Security Features

The Pipfile.lock in Pipenv optimizes the new security improvements in Pip. The Pipfile.lock generates the sha256 hashes of every downloaded package.

Pipenv ensures you don't download harmful files when on a compromised network.

Specifying Python Versions

With Pipenv, you can choose a preferred version of Python for your project. You may want to specify if you have installed several versions of Python.

To use Python3

 pipenv --python 3 # Pipenv will use any version 3 installed 
 pipenv --python 3.8 # Pipenv will use version 3.8 

When you specify a Python version, Pipenv scans the system for that Python version. If you do not specify, Pipenv will pick the latest version of Python installed on your machine. Learn more from the Pipenv official documentation.

Installing and Using Pipenv in a Practical Example

Next, proceed to install and learn how to use Pipenv practically.

1. Install Pipenv

Before installing Pipenv, you first need to update all installed dependencies. Run updates with the following command:

 sudo apt-get update 

Next, Install Pipenv using the following command:

 pip3 install pipenv 

If there are no errors, it means the installation was successful. To confirm installation check the Pipenv version using the following command:

 pipenv --version #output pipenv,version 2022.8.30 

If you have previously used Pip, Pipenv is compatible with Pip syntax. For example, if you want to install a package, you may use pipenv install instead of pip install.

2. Create Virtual Environment

You have to activate your project's virtual environment in the project folder. Pipenv manages dependencies per-project basis.

On the command line, navigate your project folder with the following command.

 cd myproject 

Next, open the folder in a code editor. Then install Pipenv dependencies with the following command:

 pipenv install requests 

A successful installation will be as shown below:

A console showing a pipenv virtual environment created successfully
Screenshot by Sandra Dindi

Pipenv dependencies create a virtual environment for the project. In the project folder, you will notice that Pipenv made the Pipfiles. Next, activate the virtual environment.

3. Activate Virtual Environment

You must activate the virtual environment under which the application will run. Run the pipenv shell command to enter the Pipenv virtual environment.

 pipenv shell 

Notice the changes on the command line after running the pipenv shell. A virtual environment, named after the folder, appears on the left in brackets.

 (installpipenv) sandra@sandra-Lenovo:~/MUOfiles/installpipenv$ 

Your Pipenv environment is now active!

You can now begin installing your preferred packages and dependencies. Let's install the Django library. Run the following command on the terminal to install Django.

 pipenv install django 

A successful installation will look as shown in the picture below.

A console showing successful installation of django
Screenshot by Sandra Dindi

When you click on the Pipfile, you will see the Django library installed without the version number.

 [[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
 
[packages]
requests = "*"
django = "*"
 
[dev-packages]
 
[requires]
python_version = "3.10"

That is how Pipenv will manage all your dependencies.

Specifying Package Versions

Although Pipenv installs version-less packages, you can install a preferred version. For example, if you want to install Django 4.1.1, you can do so like this:

 pipenv install django==4.1.1 # installs Django 4.1.1 

However, this will lock the version; for a more flexible approach, use the ~= operator:

 pipenv install django~=4.1.1 # installs Django 4.1.1, 4.1.2 if available, but not 4.2.0 

You can request any version that's equal to, or greater than, a specific version using >=:

 pipenv install django>=3.1.1 # installs Django 3.1.1 and greater 

Pipenv is Best for Package Management

Pipenv is an innovative tool to install and manage project dependencies. Its ability to handle version-less dependencies prevents conflict between differing versions.

Pipenv combines the power of Pip and Venv in one single command. Pipfiles replace the requirements.txt file that handles dependencies in older Python versions. Its security measures protect your app from harmful downloads.

You can now use Pipenv to optimize and improve your development experience. With new updates, Pipenv continues to be a fantastic tool for managing Python projects.