Pipenv (Python)

Article Info
Last Updated
(010f10f)
Tags
Python

Why use pipenv?

The first question you might ask is “why bother?”, Surely I can just use pip to install and manage my packages like I always do.

You’re right, this will work great for you but what if you want to share your code? How can you be sure that the person you are giving the code to has all the required libraries, and at the right versions? You could provide a list of required packages, but how do you know if you’ve missed something? After all, if everything is installed locally on your machine your code might be using the library and not declare that it needs it.

Virtual environments

This is one of the problems that virtual enviroments are designed to solve. Virtual envrioments are their own collections of python packages installed in a special directory stucture. Rather than using the libraries on your system, they keep track of their own dependencies and versions, if you forget to include something in the requirements the virtual environment won’t list it and therefore the code will not run correctly.

Virtual enviroments can be managed manually, by keeping a requirements.txt file in sync with a python virtual environment and trying to make sure that they don’t go out of sync. Giving someone the steps to setup and manage a virtual environment is more complicated than needed. There are tools to create an manage these virtual environments on the user’s behalf, but there is an easier way: pipenv.

Pipenv

Pipenv is a ‘friendly’ wrapper around a virtual environment that allows for easy management of python packages using fairly standard approaches (if you’ve used npm, you can think of pipenv as npm for python). It an also be used to install packages globally for production/containers, but we’ll not be covering that use case for this tutorial. It also features security auditing features for dependencies, but again, this is outside the scope of this module.

So to summarise, pipenv:

  • allows us to easily define the requirements for our projects
  • allows other people to easily run our solutions
  • makes it more difficault to get versions of packages wrong, or miss packages from our requirements

Let’s look at how we can use it.

Installing pipenv

Assuming you have a python environment set-up already, you can install pipenv using pip on the command line:

pip install --user pipenv

note: If you are or windows, you may need to use pip.exe rather than pip

Running code

pipenv run ./main.py

Getting a shell

Behind the scenes, pipenv is creating an managing a virtual enviroment for you. If you wish, you can directly run things inside this virtual environment. This means you don’t need to use the run command to run python code that depends on the libraries spesified in the requirements file.

pipenv shell
./main.py

using pipenv for our own projects

Let’s assume we have a project that we want to manage using piepnv, how do we go about starting that project?

Simple, we simply execute the pipenv install command in the root of our project. This will create two files (pipfile and pipfile.lock) which will tell pipenv that this is a project which are managed with pip. Both of these files should be stored in the repository along side your code.

Graduation Cap Book Open book GitHub Info chevron-right Sticky Note chevron-left Puzzle Piece Lightbulb Video Exclamation Triangle Globe