Python Dependency Management with pipenv

Ryan Brooke Payne
Jul 16, 2018 · 4 min read
Photo by Kelly Sikkema on Unsplash

Summary: A guide to managing Python dependencies with pipenv.



This article assumes the following:

  • You’re on a MacOS
  • You’ve installed Python 3

What is pipenv?

pipenv works in conjunction with pip and virtualenv to manage project dependencies. If you’ve used composer (PHP), yarn (Node), or bundler (Ruby), pipenv will feel very familiar.

Installing pipenv

To install pipenv, run the pip3 install command with the -U flag:

pip3 install -U pipenv

Note: The -U flag instructs pip to only install pipenv for your user (e.g. /Users/john.doe). This is considered best practice. See User Installs for more information.

Setting Up

Now that pipenv is installed, create and enter a new project directory:

mkdir ~/projects/myproject
cd ~/projects/myproject

Next, run the pipenv install command.

pipenv install

You should see the following output:

Creating a virtualenv for this project...
Virtualenv location: /Users/my.user/.local/share/virtualenvs/my-project-Qcp8YOYf
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (a65489)!
Installing dependencies from Pipfile.lock (a65489)...
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

When you run pipenv install, pipenv does the following:

  1. Creates a virtual environment
  2. Creates a Pipfile
  3. Creates a Pipfile.lock
  4. Installs dependencies

Virtual Environment

A virtual environment is a unique folder (environment) on your machine that contains the specific dependencies (and their versions) for your project. This enables you to maintain different sets of dependencies with different versions on a per-project basis.

Without a virtualenv, your project would be required to use the global dependencies on your machine. This would be fine if you only ever worked on one project. But that’s not realistic.

To find the path to your virtualenv, run:

pipenv --venv


A Pipfile logs the dependencies your project depends on. Your Pipfile should look something like:

name = "pypi"
url = ""
verify_ssl = true
python_version = "3.7"


A Pipfile.lock is a JSON file that maintains the consistency of your project’s builds. Your Pipfile.lock should look like:

See Deterministic builds for more info on maintaining consistent builds.

Running Commands

Now that we’re finished setting up, let’s run some commands.

Adding Dependencies

To add dependencies to your project, run the install command with the dependency name as the first argument. The following example installs the requests dependency.

pipenv install requests

If you only need the dependency for local development, run the command with the -d flag.

pipenv install requests -d

Removing Dependencies

pipenv uninstall requests

Creating a Script

Now that we’ve installed the requests dependency, let’s create a script to run. Create a file in the project directory you created earlier.


Add the following code to the file.

import requestsresponse = requests.get("")
date = response.json()["date"]
print("Today's date is {}".format(date))

Running Scripts

There are two ways to run scripts:

  1. pipenv run
  2. pipenv shell

pipenv run

To run scripts using the pipenv run command, simply append "pipenv run" to the Python script command.

pipenv run python

You should get the following output (with the current date):

Today's date is 07-16-2018

pipenv shell

To run commands inside a shell, run the pipenv shell command.

pipenv shell

Once inside the new shell, run your script.


You should get the following output (with the current date):

Today's date is 07-16-2018


Thanks to the following people for providing feedback on this article:

Any suggestions? If you have any thoughts or feedback on this article, please leave a note. Your thoughts will help me improve and serve you better. If I incorporate your feedback, I will give you credit.

Ryan Brooke Payne

Written by

Software developer and technical writer at

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade