Pipenv was first released as an experiment way back in January of 2017 by Kenneth Reitz. Even though pipenv is a package that attempts to marry the best of pip and virtualenv into one single toolchain and include a replacement for requirements.txt, it didn’t get much love. But this year, the Python community has welcomed pipenv as the far better way to create virtual environments, especially when it comes to dependency management.
For example, previously, in order to create virtual environments so you could run multiple projects on the same computer you’d need:
- A tool for creating a virtual environment (like virtualenv or venv)
- A utility for installing packages (like pip or easy_install)
- A tool/utility for managing virtual environments (like virtualenvwrapper or pyenv)
- All the commands associated with the libraries used
Pipenv includes all of the above, and more, out of the box. It essentially gives you package management and virtual environment support in a single tool. Let’s break it down.
Install, Load and Lock
You can simply
pip install pipenv to get started, and then
pipenv myvenv to create your virtual environment. From now on you want to use pipenv (rather than pip) to install all of your packages because pipenv:
- Allows you to specify into which environment you install the package (e.g. use
--devto install a package that will be used only in a dev environment);
- Integrates directly with PyPI, but can also be redirected to a local repository;
- Creates a single Pipfile with separate sections for each environment, a simpler implementation than previously having to create one requirements.txt per environment;
- Allows you to
pipenv lockyour virtual environment, which will create a Pipfile.lock file that resolves all dependencies required for a build AND uses hashes to ensure deterministic builds for specific environments.
Dependency Management for Free
One of the most exciting things about Pipenv is how it handles dependency management compared to requirements.txt and pip freeze. To be fair, Pipenv performs just like pip when it comes to installing all the required sub-dependencies for your packages. And just like pip, if you get a conflict, you’re still stuck manually trying to figure it all out (although you can issue
pipenv graph to view a full dependency tree, which should help).
But once you‘ve solved the issue, Pipfile.lock keeps track of all of your application’s interdependencies, including their versions, for each environment so you can basically forget about interdependencies.
In practice, this means you can continue working in development until you’ve got a set of packages/versions that work for you. Now you can simply issue
pipenv lock and Pipenv will lock all the dependencies/interdependencies your project requires, pinning their versions and hashing the results so you can deterministically replicate your build in production.
The Way Forward
While venv is still the official virtual environment tool that ships with the latest version of Python, Pipenv is gaining ground in the Python Community and with the Python Packaging Authority (PyPA). ActiveState has also included it in our latest ActivePython 3.6.6 release.
While Pipenv may not end up being the virtual environment tool of choice for all Python users, the fact that Pipenv means you no longer need to manually create and manage your virtual environments is a significant step forward.
Our Suggestion? Stop pip installing into your virtual environments and start pipenv installing. You won’t regret it.
Agree? Disagree? Join the discussion below.
Watch a 5-minute live-coding video that shows you how to use our Platform to work with Pipenv.