How to use pipenv in your python project

How to use pipenv in your python project

“goodbye requirements.txt, hello Pipfile”

In this article, I will introduce you to pipenv and show you how easy it is to use. pipenv removes the awkwardness of working with virtual environments and module dependencies. Once you spend 15 minutes with pipenv, you will realize the clarity and time savings it will bring to your python project.

What is pipenv?

pipenv was created by Kenneth Writze, the author of the extremely popular Requests package for python.  In his own words…

Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases. – The Hitchhiker’s Guide to Python

The Python Software Foundation says pipenv and Pipfile are the recommended (if not Official) dependency management tools for python.

How pipenv works

  • pipenv keeps all virtual environment files in ~/.virtualenvs
  • pipenv keeps track of module dependencies in Pipfile (stored in the root of your project)
  • pipenv’s Pipfile replaces requirements.txt
  • pipenv keeps track of what’s been installed in the file Pipfile.lock


Here is an example Pipfile from one of my projects

How to install pipenv

pip install pipenv

Note: Depending on how python3 is installed you may need to type pip3 install pipenv


How to use pipenv

Create a new virtual environment

pipenv --python 3.6

Activate a virtual environment

pipenv shell

Executing code without activating your virtual environment

pipenv run python

No need to activate your virtual environment each time, just type pipenv run followed by the command you would normally run after activating your venv.

Install a Package

pipenv install requests

pipenv install pandas

Traditionally, you would have to activate your virtualenv and then run pip install requests. Or if you were using a requirements.txt file you would add the desired package to requirements.txt, activate your virualenv, and run pip install -r requirements.txt

Now, with just one command, we add the package to our Pipfile and pipenv installs it immediately. We didn’t have to activate the virtualenv, just run the command from inside our project directory.

Migrate from requirements.txt to Pipfile

pipenv install -r requirements.txt

This command will read in an existing requirements.txt file and populate your Pipfile for you. Simple!

Module Dependency Graph

pipenv graph

This will print all of your project’s installed module versions as well as the pip modules they depend on.

Update pip packages

After some time has passed we will want to upgrade to the latest versions of our installed pip packages. This is painless with pipenv, just type…

pipenv update

Here is what it looks like when you use pipenv to upgrade all of your pip packages.

In Closing

I hope you enjoyed following along with this introduction to pipenv. Maybe it will save you some time and pain on your next python project.

Don’t forget, pipenv is the new way to manage package dependencies for python.
Goodbye requirements.txt, Hello Pipfile


More Python Goodness

Comments are closed.