Pipenv - First Impressions

5 min read ⋅ 3934 views


Pipenv is an experimental tool with the purpose of bringing together different packaging tools in one place. It has been dubbed the “Python Development Workflow for Humans” and was developed by Kenneth Reitz. Created just over a year ago, it has already become the official Python-recommended resource for packaging dependencies.

The reason for this is because it solves a number of problems that have existed in the Python world for years. In order to get an understanding of these issues we need to look at the most popular packaging tools from before Pipenv and their pros and cons.

What is a packaging tool?

Packaging tools are used to manipulate packages in the packaging ecosystem. For years there have been two tools at the top of almost every Python developer's list for such tasks. These are pip and Virtualenv.

pip is a tool that lets you install and manage software packages. Some of its benefits are:

  • Uninstallation of packages.
  • Keeping track of why a package was required.
  • Packages can be installed flat, rather than as egg archives.
  • Native support for Git.
  • All packages are downloaded before installation.

Although pip has some great advantages and features, it isn’t perfect. The biggest problem with pip is that it doesn’t isolate packages from each other. This is important if you are working on programs that use different versions of the same library. The solution to this was to add another tool to the mix, Virtualenv.

Virtualenv is short for "virtual environment". It acts as a working copy of Python and allows you to work on a project in a separate environment. The main benefit of this is that you can use isolated project environments. This gives you the ability to work on a specific project without affecting any other projects.

While Virtualenv is a pretty solid tool by itself, there are problems with the setup overall. The pip + Virtualenv method means using a lot of different libraries and this means there is a lot to manage. On top of that, you have to deal with all the commands associated with those libraries. This can create quite a headache for developers as they could end up spending a significant amount of time managing these libraries rather than actually programming.

Luckily, there is now an alternative.

Enter Pipenv

Pipenv ships with package management and virtual environment support straight out of the box, which means you are able to use one tool to install, uninstall and track packages as well as create, use and organise virtual environments. To add to its simplicity, Pipenv will automatically create a virtual environment for you when you are working on a project, if you aren’t already using one.

The way Pipenv accomplishes all of this by abandoning the requirements.txt file so many developers will be familiar with. It has replaced that file with a new one called pipfile . Now when you install a library using Pipenv a pipfile is automatically updated with details of that installation. These details include the version information, Git repository location and file path.

Another benefit of Pipenv is its ability to manage complex interdependencies. Let's say for example that you have a program that depends on a specific library. Then let's say that library also depends on another specific library. In this situation, things can get complicated as you then have two libraries with conflicting dependencies. Pipenv keeps track of the programs interdependencies with a file called pipfile.lock. This file has another useful feature as well. It is able to verify that the right versions of the dependencies are being used in production.

Pipenv also comes in handy when you are working with other developers. The standard method in this situation is to use a pip workflow, but using a pip workflow means potentially having many requirements files for different environments, and this can quickly get confusing for your peers. By using a pipfile, you are able to overcome this by letting it do the work for you, using the auto-update feature mentioned above.

Installing Pipenv

As it stands Pipenv does not ship with Python. There are a number of reasons for this but in general Python only ships with minimal tooling. The preferred method is to simply install whatever you need through pip. The isn’t really an issue but some developers would prefer certain packages to be available straight away.

To install Pipenv using pip simply enter the following code into the command line like this.

pip install pipenv

Once you have done that you are good to go! Head over to the official documentation to help you learn to use Pipenv. 

Comparisons

To get a better idea of the benefits of Pipenv here are some comparisons.

First of all, we will run through how to set up a virtual environment using Virtualenv. You will want to start by changing directory into the root of your project directory and running the following command.

$ cd ~/code/this_project/ 
$ virtualenv env

env is the name of the directory you want to create your virtual environment inside. It has become commonplace to call this directory env and place it inside the project directory. For example, if the project directory is ~/code/this_project/, then the environment will be at ~/code/this_project/env. Although this is standard, you can call the environment whatever you want and place it wherever you want.

To activate the environment you will need to run one more command.

source env/bin/activate

This temporarily adjusts a few variables in your shell so that when you type python you actually get the Python binary inside the Virtualenv. You can then use pip install requests to install the library directly to the environment.

If you want to work on a different project, you can use deactivate to stop one and run the activate command again on the new one.

Here is how to set up a virtual environment using Pipenv.

$ cd this_project
$ pipenv install

These two commands have actually done quite a lot. You have created both a pipfile and and pipfile.lock and set up a virtual environment provided the project didn’t have an existing one. To activate the environment you just need to enter:

pipenv shell

You can then run your code in the virtual environment by entering:

pipenv run python my_project.py

As you can see from this comparison, the simplicity of Pipenv is clear. Virtualenv isn’t complicated by any means but it can't compare with Pipenv by itself and once you begin using other tools to compensate it begins to make a lot of sense to try Pipenv instead. 

The Future Looks Bright

By bringing the benefits of pip and Virtualenv to one tool and making it simple and efficient, Pipenv has become a useful tool for Python developers, which explains its rise in popularity. There is a very real chance that Pipenv could become the standard packaging tool in future versions of Python but it’s way too soon to be sure. So far it’s ticking the right boxes but as is the case with most new technologies, time needs to be spent ironing out all the creases.



Python



MORE ARTICLES

How To Structure a Python Project

Writing programs isn’t all about the code. Sure when it comes down to it, the code is what makes the functioning program...

5614 views

How To Write Tests For Python

An important part of development in any Python project is testing. Testing is used to help define the intent of your cod...

2877 views

Creating games with JavaScript - Part 1

In recent years, JavaScript has become one of the most popular programming languages there is. Thanks to developments in...

1886 views